Get lines of file1 which are not in file2

2020-02-12 03:58发布

问题:

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
...

回答1:

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)


回答2:

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.



标签: linux