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