How to compare two files and output lines which do not appear in the first

bashcommand linecoreutilsdiff()file-comparison

If I have a file A containing a list of fields:

2017-04-23
2017-04-30
2017-05-07
2017-05-14
2017-05-21
2017-05-28
2017-06-04
2017-06-11
2017-06-18
2017-06-25

And another file B containing a list of fields:

2017-04-23
2017-04-30
2017-05-07
2017-05-14
2017-05-21
2017-05-28
2017-06-04
2017-06-11
2017-06-18
2017-06-25
2017-07-02
2017-07-09
2017-07-16
2017-07-23

How can I quickly diff these two files where I want to know all fields in file B which are not present in file A?

This is not a regular diff where I want to see a relative difference between files but more like a hash comparison where each line is an entry in a map. I want to get a list of all lines in file B which are not present in file A so that I can remove them where each line in file A represents a directory which is to be preserved.

I am looking for a Bash/CoreUtils solution.

Best Answer

If your files are sorted, you can use comm:

$ comm -13 A B
2017-07-02
2017-07-09
2017-07-16
2017-07-23

with options:

  • -1 : suppress column 1 (lines unique to FILE1)
  • -3 : suppress column 3 (lines that appear in both files)