Parsing brackets with sed using regex

I am looking for a command in sedthat converts this input stream:

dummy
(key1)
(key2)dummy(key3)
dummy(key4)dummy
dummy(key5)dummy))))dummy
dummy(key6)dummy))(key7)dummy))))

in that:

key1
key2
key3
key4
key5
key6
key7

where there dummycan be any line without parentheses. So I basically would like to extract the lines between the brackets and output one line per line. An optional closing bracket is possible ).

I have done many tests sedusing regex, but I cannot figure out how to solve this problem. Although I am sure that this is possible. (I am open to alternative tools like Perl or Python for example)

EDIT: The line between the brackets (key1, key2 .. key7) can be any line without parentheses.

+3
3

lookbehind grep -oP:

grep -oP '(?<=\()[^)]+' file
key1
key2
key3
key4
key5
key6
key7

awk:

awk -F '[()]' 'NF>1{for(i=2; i<=NF; i+=2) if ($i) print $i}' file
key1
key2
key3
key4
key5
key6
key7
+1

, :

my @all_keys; 

while ( <DATA> ) {
   push ( @all_keys, m/\((.+?)\)/g  );
}
print join ("\n",@all_keys);


__DATA__
dummy
(key1)
(key2)dummy(key3)
dummy(key4)dummy
dummy(key5)dummy))))dummy
dummy(key6)dummy))(key7)dummy))))

, "" \w perlre (- "_",)

( perl, <DATA> <STDIN> script - @all_keys)

+2

Perl Marpa, BNF - .

BNF, , , . Parens , .

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1777709/


All Articles