Perl one liner to extract a multi-line pattern

Viewed 12539

I have a pattern in a file as follows which can/cannot span over multiple lines :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {

What I have tried :

perl -nle 'print while m/^\s*(\w+)\s+(\w+?)\s*(([\w-0-9,* \s]))\s{/gm'

I dont know what the flags mean here but all I did was write a regex for the pattern and insert it in the pattern space .This matches well if the the pattern is in a single line as :

abcd25 ef_gh ( fg*_h hj_b* hj ) {

But fails exclusively in the multiline case !

I started with perl yesterday but the syntax is way too confusing . So , as suggested by one of our fellow SO mate ,I wrote a regex and inserted it in the code provided by him .

I hope a perl monk can help me in this case . Alternative solutions are welcome .

Input file :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {

 abcd25
 ef_gh
 fg*_h
 hj_b*
 hj ) {

 jhijdsiokdù ()lmolmlxjk;
 abcd25 ef_gh ( fg*_h hj_b* hj ) {

Expected output :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {
 abcd25 ef_gh ( fg*_h hj_b* hj ) {

The input file can have multiple patterns which coincides with the start and end pattern of the required pattern. Thanks in advance for the replies.

2 Answers
Related