0

I have a file with two lines like:

XXXXXXX20170412,bu.1519=1,bu.1124=2,bu.1284=3,bu.1767=13,bu.1869=15,bu.1784=16,bu.1473=32,bu.1264=43,bu.1758=96,bu.1649=122,bu.1345=140,bu.1268=215,bu.1246=516,bu.1876=601,
XXXXXXX20170413,bu.1123=1,bu.1767=27,bu.1784=32,bu.1473=57,bu.1869=41,bu.1264=91,bu.1284=54,bu.1758=191,bu.1268=387,bu.1345=319,bu.1649=322,bu.1246=1199,bu.1876=1383,

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.

0

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

Result

XXXXXXX20170412,bu.1519=1,bu.1124=2,bu.1284=3,bu.1767=13,bu.1869=15,bu.1784=16,bu.1473=32,bu.1264=43,bu.1758=96,bu.1649=122,bu.1345=140,bu.1268=215,bu.1246=516,bu.1876=601,
XXXXXXX20170413,bu.1123=1,bu.1767=27,bu.1784=32,bu.1473=57,bu.1869=41,bu.1264=91,bu.1284=54,bu.1758=191,bu.1268=387,bu.1345=319,bu.1649=322,bu.1246=1199,bu.1876=1383,bu.1519,bu.1124
0

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.