I have file1 and file2.


a james
b mike
d john


a 10 20 30 40
b 11 12 13 14
c 20 30 40 50
d 10 20 30 50

I want the output file to only find the difference in column 1, and print that line for file2. In this case: C. so file3 look like:

c 20 30 40 50

I think the proper command is comm, but I don't think I'm using the right switches because every combination I can think of isn't giving me the right results.

  • So since you are mentioning comm, we can assume the first column is sorted? Great! As a first step: in a shell like bash that has process substitution we can do: comm <(cut -d ' ' -f 1 file1 ) <(cut -d ' ' -f 1 file2) – phk Jul 3 '16 at 20:58
  • yep, the first column will always be sorted. I mentioned comm, but the list of commands that I can use are: comm, sdiff, diff, join, cmp. Using your command, that doesn't seem to print out cleanly. Instead it just has my file2 contents split up across the file – nico Jul 3 '16 at 21:03
  • Huh? It should be a comparison of the first column, between file1 and file2, you should see what is missing in file2 on the left side, like I said, it would only be a first step. Oh and BTW, if you add -13 to the comm call from me you will only see the differing entries in the first column, so you can then use this to select from file2: grep "^$(comm -13 <(cut -d ' ' -f 1 file1 ) <(cut -d ' ' -f 1 file2))" file2 – phk Jul 3 '16 at 21:30

you can do this with awk

awk 'FNR==NR{a[$1];next};!($1 in a)' file1 file2
  • I am really interested in how this works. I don't have much experience with multi-file awk and most tutorials aren't about it either. – phk Jul 3 '16 at 21:00
  • awk seems to be the way to go here, but I have only been given comm, sdiff, diff, join, cmp as commands to use. – nico Jul 3 '16 at 21:04

I figured it out!

join –v 1 file2 file1

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.