3

I have file1 (sample):

60108903374
60172485121
60108919381
60174213128
60108919951
60108919970
601112020106
601112020107
601112020108
601112020113
601112020114
60175472940

And file2:

60179970001,A
60172681920,A
60174202041,A
60172514180,A
60174314679,A
60174325306,A
60175472940,A
60174213128,A
60175328984,A
60175349857,A
60172796759,A
60172798922,A
60179195129,A
60172485121,B
60173483126,A
60172683175,A
60174521828,A
60142536314,B
60175347909,B
60175183031,B

I want to merge file1 and file2 with output matching based on the first column as well as shows the second column from file2.

Desired output:

60172485121,B
60174213128,A

file1 has ~80k lines and file2 has 500k lines.

Tried using:

join -1 1 -2 1 -o 1.1,2.2 file1 file2
  • It is not important bu I think 60175472940,A is missing in the "desired output" – JJoao Sep 9 '16 at 9:17
2
join -t, <(sort file1) <(sort -t, file2)

The above does the job.

0
awk -F, 'NF==1 {a[$1]++}; 
         NF>=2 && a[$1]' file1 file2
  • if we have just one field, save it
  • if we have more and the first field is saved, print it

output obtained:

60175472940,A
60174213128,A
60172485121,B

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.