我有这些行的文件
G8 = P(G1,G3)
G9 = P(G3,G4)
G12 = P(G2,G9)
G15 = P(G9,G5)
G16 = P(G8,G12)
G17 = P(G12,G15)
Run Code Online (Sandbox Code Playgroud)
我需要输出为
G1,G3
G3,G4
.....
Run Code Online (Sandbox Code Playgroud)
如何使用 sed/grep 命令或使用 perl 来完成?
ter*_*don 27
其他几种方式:
sed
sed 's/.*(\(.*\))/\1/' file
Run Code Online (Sandbox Code Playgroud)perl
perl -pe 's/.*\((.*)\)/$1/' file
Run Code Online (Sandbox Code Playgroud)
或者
perl -lanF"[()]" -e 'print $F[1]' file
Run Code Online (Sandbox Code Playgroud)
或者
perl -pe 's/.*\((.+?)\).*/$1/;' file
Run Code Online (Sandbox Code Playgroud)awk
awk -F"[()]" '{print $2}' file
Run Code Online (Sandbox Code Playgroud)贝壳
while IFS="()" read a b; do echo "$b"; done < file
Run Code Online (Sandbox Code Playgroud)有不止一种方法可以做到:
perl -nle 'print $1 if /\((.*)\)/' file
Run Code Online (Sandbox Code Playgroud)
或者:
awk 'NR > 1 {print $1}' RS='(' FS=')' file
Run Code Online (Sandbox Code Playgroud)
grep -oP '\(\K[^)]+' file
Run Code Online (Sandbox Code Playgroud)
寻找左括号,忽略它,然后打印所有跟随的非右括号字符。
需要 GNU grep
sed 's/^.*(//;s/)$//' /path/to/file
要分解这个:
sed是流sitor ed。 's/^.*(//;s/)$//'是发送到 的脚本sed,分解如下:
s/^.*(// substitute nothing for the beginning of any line (`^`) followed by anything up until an open-paren (`(`)
s/)$// substitute nothing for a close-paren (`)`) followed immediately by the end of a line
Run Code Online (Sandbox Code Playgroud)