2014-01-07 65 views
-4

之間的字符串我有這些行打印兩個支架

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 
..... 

+4

你試過了什麼'grep' /'perl'命令? – zero298

回答

1
sed -e 's/.*(//' -e 's/).*//' filename 

或者更簡潔:

sed 's/.*(//;s/)//' filename 
0
cat your-file-name.txt | cut -d'(' -f2 | cut -d')' -f1 

你甚至不需要的grep/sed的!

+2

你甚至不需要'貓':) – toolic

+0

有必要幫助他理解這個概念......;)無論如何謝謝! –

+1

[無用的'cat']的概念(http://partmaps.org/era/unix/award.html)? – ThisSuitIsBlackNot

1
perl -nE 'say /\((.+?) \)/x' file 
0

這可能會爲你工作(GNU SED):

sed 's/.*(\(.*\)).*/\1/' file 
0

使用awk

awk -F"[()]" '{print $2}' file 
G1,G3 
G3,G4 
G2,G9 
G9,G5 
G8,G12 
G12,G15 
0

兩個可能的解決方案使用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/; 
0

,如果你有紅寶石

# ruby -ne 'puts $_.scan(/\((.*)\)/)' file 
G1,G3 
G3,G4 
G2,G9 
G9,G5 
G8,G12 
G12,G15