12

I have two long, but sorted files. How to get all lines of first file which are not in second file ?

file1

0000_aaa_b
0001_bccc_b
0002_bcc <------ file2 have not that line
0003_aaa_d
0006_xxx
...

file2

0000_aaa_b
0001_bccc_b
0003_aaa_d
0006_xxx
...
marioosh
  • 27,328
  • 49
  • 143
  • 192
  • Possible duplicate of [Fast way of finding lines in one file that are not in another?](http://stackoverflow.com/questions/18204904/fast-way-of-finding-lines-in-one-file-that-are-not-in-another) – tripleee Apr 18 '17 at 04:56

2 Answers2

17

This is what the comm command is for:

$ comm -3 file1 file2
0002_bcc

From man comm:

DESCRIPTION

   Compare sorted files FILE1 and FILE2 line by line.

   With  no  options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

   -1     suppress column 1 (lines unique to FILE1)

   -2     suppress column 2 (lines unique to FILE2)

   -3     suppress column 3 (lines that appear in both files)
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
3

Just run a diff on them:

diff -c file1 file2

The -c (for "context") flag will only display the lines that are different, with two lines surrounding each line.

Bucket
  • 7,415
  • 9
  • 35
  • 45