【问题标题】:Get lines of file1 which are not in file2获取file1中不在file2中的行
【发布时间】:2013-10-05 18:08:04
【问题描述】:

我有两个长但已排序的文件。如何获取第一个文件中不在第二个文件中的所有行?

文件1

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

文件2

0000_aaa_b
0001_bccc_b
0003_aaa_d
0006_xxx
...

【问题讨论】:

标签: linux


【解决方案1】:

只需在他们身上运行diff

diff -c file1 file2

-c(用于“上下文”)标志将只显示不同的行,每行周围有两行。

【讨论】:

    【解决方案2】:

    这就是comm 命令的用途:

    $ comm -3 file1 file2
    0002_bcc
    

    来自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)
    

    【讨论】:

    • 这仅适用于已排序的文件。试试comm -23 &lt;(sort file1) &lt;(sort file2)
    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2015-10-10
    • 2017-11-20
    相关资源
    最近更新 更多