获取不在file2中的file1行

mar*_*osh 9 linux

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

文件1

0000_aaa_b
0001_bccc_b
0002_bcc <------ file2 have not that line
0003_aaa_d
0006_xxx
...
Run Code Online (Sandbox Code Playgroud)

文件2

0000_aaa_b
0001_bccc_b
0003_aaa_d
0006_xxx
...
Run Code Online (Sandbox Code Playgroud)

Chr*_*our 14

这是comm命令的用途:

$ comm -3 file1 file2
0002_bcc
Run Code Online (Sandbox Code Playgroud)

来自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)
Run Code Online (Sandbox Code Playgroud)

  • 我不知道这个命令.非常感谢你!:) (2认同)
  • 这仅适用于已排序的文件。试试 `comm -23 &lt;(sort file1) &lt;(sort file2)`。 (2认同)