首页·终端命令

comm

文本处理

用法

comm -12 [file1] [file2]

比较两个已排序的文件,输出三列:只在左、只在右、两边都有;输入没排序结果就是乱的,而选项是用来隐藏列而不是选列。

常用选项

选项含义
-1Hide column 1, the lines only in the first file
-2Hide column 2, the lines only in the second file
-3Hide column 3, the lines in both
-iCompare without regard to case
--check-orderGNU: stop when the input is not sorted

示例

comm -12 a.txt b.txt

Only the lines both files share.

comm -23 a.txt b.txt

Only the lines the first file has.

comm -13 <(sort a.txt) <(sort b.txt)

Only the lines the second file added (bash).

它们本来就是用管道串起来用的。串三条往往比让一条命令做完所有事更短。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. comm 是做什么的?

比较两个已排序的文件,输出三列:只在左、只在右、两边都有;输入没排序结果就是乱的,而选项是用来隐藏列而不是选列。

Q. 怎么写?

comm -12 [file1] [file2] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 5 个,完整列表在 man comm。这条命令属于文本处理。

相关命令

man 手册: man comm