web-dev-qa-db-fra.com

Imprimer la chaîne entre deux parenthèses

J'ai fichier avec ces lignes

G8 = P(G1,G3)
G9 = P(G3,G4)
G12 = P(G2,G9)
G15 = P(G9,G5)
G16 = P(G8,G12)
G17 = P(G12,G15)

J'ai besoin de la sortie comme

G1,G3
G3,G4
.....

Comment puis-je le faire avec la commande SED/GREP ou utiliser Perl?

15
user56153

Il y a plus d'une façon de le faire:

Perl -nle 'print $1 if /\((.*)\)/' file

ou:

awk 'NR > 1 {print $1}' RS='(' FS=')' file
7
cuonglm
grep -oP '\(\K[^)]+' file

Qui cherche la parenthèse d'ouverture, l'ignore, puis imprime tous les caractères non proches de parenthèses qui suivent.

Nécessite GNU grep

5
glenn jackman

sed 's/^.*(//;s/)$//' /path/to/file

Pour casser cela:

sed est le s tream ed itor. 's/^.*(//;s/)$//' est le script envoyé à sed, ce qui décompose comme suit:

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
5
DopeGhoti

Une solution de coupe simple:

$ CAT Test01 | Cut -D "(" -F2 | Cut -D ")" -F1

1
Sanket
awk -F'(' '{print $NF}' file | sed 's/)//g'
0
Raid_Master