Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

perl oneliner print the array and other match in same print statement

I have a set of files with sample data like the one below, I need to transform the data to put all the object files(followed by -o) in 1 column and linked libs(followed by -l) in 2nd column. This format is consistent in the entire make output.

hello there -o one two three four -labc -lfoo -lbar
something useless -o abc doo zoo  -lkoo -lfoo -lmoo

I am trying to parse it to a simpler format for further processing:

one two three four, abc foo bar
abc doo zoo, koo foo moo

I am trying this, clearly this is not what I was trying to get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

perl  -ne '/-o(.*?)-/m; @libs = /-l([^ ]+)/gs; printf "%s %s\n", $1 , join(", ", @libs);' inputfile
bar
 abc, foo, bar

moo
 koo, foo, moo

Here, I am trying to store all the objects into $1 and all the libs in @libs array. Only libs are correctly printed, but objects are incorrect, can someone help fixing it? I seperatly verified that $1 is holding the correct value.

perl  -wne '/-o(.*?)-/m;  printf "%s %s\n", $1, " "' inputfile
 one two three four
 abc doo zoo

Similarly, when I am printing the 2nd part(libs) seperatly, its also works.

perl  -ne '@libs = /-l([^ ]+)/gs; printf "%s\n",  join(", ", @libs);' x
abc, foo, bar

koo, foo, moo

So, it only messes up when I combine the two together.

>Solution :

perl -wnlE'
    ($o, @l) = /(?:-o|-l) \s* ([^-]+) /gx;
    s/^\s+|\s+$//g for $o, @l;            
    say join ",", $o, "@l"
' file

On the file with given two lines it prints

one two three four,abc foo bar
abc doo zoo,koo foo moo
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading