comm
テキスト処理
書き方
comm -12 [file1] [file2]並べ替えた二つのファイルを比べ、左だけ、右だけ、両方の三欄で示します。並べ替えていないと結果は無意味で、オプションは欄を選ぶのではなく隠すものです。
よく使うオプション
| オプション | 意味 |
|---|---|
| -1 | Hide column 1, the lines only in the first file |
| -2 | Hide column 2, the lines only in the second file |
| -3 | Hide column 3, the lines in both |
| -i | Compare without regard to case |
| --check-order | GNU: stop when the input is not sorted |
例
comm -12 a.txt b.txtOnly the lines both files share.
comm -23 a.txt b.txtOnly 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