1、comm命令
COMM(1) BSD General Commands Manual COMM(1)
NAME
comm – select or reject lines common to two files
SYNOPSIS
comm [-123i] file1 file2
DESCRIPTION
The comm utility reads file1 and file2, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files.
The filename ``-'' means the standard input.
The following options are available:
-1 Suppress printing of column 1.
-2 Suppress printing of column 2.
-3 Suppress printing of column 3.
Each column will have a number of tab characters prepended to it equal to the number of lower numbered columns that are being printed. For example, if column number two is being suppressed, lines printed in column number one will not have any tabs preceding them, and lines printed in column number three will have one.
The comm utility assumes that the files are lexically sorted; all characters participate in line comparisons.
中文简介就是comm [-123i] file1 file2,123分别是显示(抑制)文件1独有、文件2独有、文件1和2共有;i表示忽略大小写。对比时按行对比。
使用comm命令时,文件应该是排序了的。下面简单举例:
假设文件1和2的内容分别是:
file1: file2:
a c
b d
c a
e c
d
a
直接运行:
comm fiel1 file2
a
b
c
d
a
c
e
d
a
排序结果分析:先对比file1的”a”和file2的”c”,按字典顺序,输出”a”,接着比较file1的”b”和file2的”c”,输出”b”,然后会同时输出”c”,再比较file1的”e”和file2的”d”,输出”d”….直至最后。
排序后输出:
comm <(sort file1 ) <(sort file2 )
a
a
b
c
c
d
e
结果不解释,类似上面
排序且唯一:
comm <(sort file1|uniq ) <(sort file2|uniq )
a
b
c
d
e
只求交集
comm -12 <(sort file1|uniq ) <(sort file2|uniq )
a
c
d