I have a file with two lines like:


I want to get all strings exist in the first line (starting with string between ", and=" ) and not existing in the last line and append them to the end of the last line.


Form a hash %h whose keys are strings between , and =. This is formed only for the first line. Then on the second line, we search whether these keys are present in the 2nd line and collect those which aren't found and join-ed together with a comma.

perl -F, -lpe '
   $. == 1 and %h = /,([^=]*)=([^,]*)/g, next;
   $l = $_;
   $_ .= join ",", grep $l !~ /,\Q$_=/, keys %h;
' your_2-line_file



I would split the file into two, each containing one of the lines. I would then split the lines up at the comma. Then run a diff and copy the results, join them back up into one line, et violà.

That's the long way around. I'm sure you could write some kind of awk script or something to do it in one fell swoop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.