12

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

文件 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
...
4

2 回答 2

17

这是该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)
于 2013-10-05T18:12:47.923 回答
3

只需diff对它们运行:

diff -c file1 file2

( -cfor "context") 标志将只显示不同的行,每行周围有两行。

于 2013-10-05T18:10:28.737 回答