Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing of parenthesis with sed using regex

I am looking for a command in sed which transforms this input stream:

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

into this one:

key1
key2
key3
key4
key5
key6
key7

where dummy can be any string without parenthesis. So I basically would like to extract the strings in-between the parenthesis and output one string per line. There can be extra closing parenthesis ).

I ran many tests with sed using regex, but I can't figure out how to solve this problem. Though I am sure it is possible. (I am open to alternative tools like Perl or Python for instance)

EDIT : The string between parenthesis (key1, key2 .. key7) can be any string without parenthesis.

like image 917
vinzvanilla Avatar asked Dec 10 '25 09:12

vinzvanilla


1 Answers

Perlishly I'd do:

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

This assumes that 'keys' match the \w in perlre (alphanumeric plus "_",)

(If you're not familiar with perl, you can pretty much just swap that <DATA> for <STDIN> and pipe the data straight to your script - or do more interesting things with @all_keys)

like image 137
Sobrique Avatar answered Dec 12 '25 23:12

Sobrique



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!