首頁·終端機命令

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