0

I have two different files which some rows of the two data measures a same thing. I want to have a merged output such that the rows of File1 will be the output row where the rows measures same thing (second row of file1 and first line of file2 measures same thing. I want to have the file1 first line when column1 and column2 of the files are similar.)

... File1:

1257, OBM  , 47, 106, 1330
1257, IRK  , 52, 104,  467

....

File2:

1257, IRK  , 50, 100,  400
1000, CTK  , 47, 106, 1330

....

output:

1257, OBM  , 47, 106, 1330
1257, IRK  , 52, 104,  467
1000, CTK  , 47, 106, 1330

....

0

Use awk:

awk -F',' '!seen[$1,$2]++' file1 file2

Explanation:

  • In the beginning, the array item with key being fields $1,$2 is unassigned, thus false.
  • If it's false, !seen is true, so we increment it (make it true) and do the default action which is print.
  • If it's true (second time we see the same fields) !seen is false and awk will skip that line.

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.