Get lines of file1 which are not in file2

前端 未结 2 1157
梦毁少年i
梦毁少年i 2020-12-19 02:20

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 &         


        
相关标签:
2条回答
  • 2020-12-19 02:41

    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.

    0 讨论(0)
  • 2020-12-19 02:48

    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)
    
    0 讨论(0)
提交回复
热议问题