之間的字符串我有這些行打印兩個支架
G8 = P(G1,G3)
G9 = P(G3,G4)
G12 = P(G2,G9)
G15 = P(G9,G5)
G16 = P(G8,G12)
G17 = P(G12,G15)
文件,我需要我如何使用sed/grep命令或用perl做輸出作爲
G1,G3
G3,G4
.....
?
之間的字符串我有這些行打印兩個支架
G8 = P(G1,G3)
G9 = P(G3,G4)
G12 = P(G2,G9)
G15 = P(G9,G5)
G16 = P(G8,G12)
G17 = P(G12,G15)
文件,我需要我如何使用sed/grep命令或用perl做輸出作爲
G1,G3
G3,G4
.....
?
sed -e 's/.*(//' -e 's/).*//' filename
或者更簡潔:
sed 's/.*(//;s/)//' filename
cat your-file-name.txt | cut -d'(' -f2 | cut -d')' -f1
你甚至不需要的grep/sed的!
你甚至不需要'貓':) – toolic
有必要幫助他理解這個概念......;)無論如何謝謝! –
[無用的'cat']的概念(http://partmaps.org/era/unix/award.html)? – ThisSuitIsBlackNot
perl -nE 'say /\((.+?) \)/x' file
這可能會爲你工作(GNU SED):
sed 's/.*(\(.*\)).*/\1/' file
使用awk
awk -F"[()]" '{print $2}' file
G1,G3
G3,G4
G2,G9
G9,G5
G8,G12
G12,G15
兩個可能的解決方案使用PERL
# Assuming a relation between the Char letter
$str =~ s/(G)\d+?\ *?=\ P\((\1\d+?,\ *?\1\d+?)\)/$2/;
# A greedy solution. Just capturing the content of the paranthesis.
$str =~ s/.*?\((.*?)\).*?/$1/;
,如果你有紅寶石
# ruby -ne 'puts $_.scan(/\((.*)\)/)' file
G1,G3
G3,G4
G2,G9
G9,G5
G8,G12
G12,G15
你試過了什麼'grep' /'perl'命令? – zero298