Compare two sorted files line by line.
comm [OPTION]... FILE1 FILE2
FILE1 or FILE2 is -.FILE1, lines unique to FILE2, and lines common to both.-1 Suppress column 1 (lines unique to FILE1).
-2 Suppress column 2 (lines unique to FILE2).
-3 Suppress column 3 (lines common to both).
--check-order Check that the input is correctly sorted, even if all input lines are paired.
--nocheck-order Do not check that the input is correctly sorted.
--output-delimiter=STR Use STR as the separator between output columns instead of the default TAB.
--total Output a fourth column containing a summary.
-z, --zero-terminated Set line delimiter to NUL instead of newline.
--help Display help information and exit.
--version Display version information and exit.
Returns 0 on success, and non-zero on failure.
Content of aaa.txt:
[root@localhost text]# cat aaa.txt
aaa
bbb
ccc
ddd
eee
111
222
Content of bbb.txt:
[root@localhost text]# cat bbb.txt
bbb
ccc
aaa
hhh
ttt
jjj
Comparison result:
[root@localhost text]# comm --nocheck-order aaa.txt bbb.txt
aaa
bbb
ccc
aaa
ddd
eee
111
222
hhh
ttt
jjj
The first column contains lines only in aaa.txt, the second column contains lines only in bbb.txt, and the third column contains lines common to both. TAB (\t) is used as the default separator between columns.
First, sort the file contents using sort:
[root@localhost ~]# sort aaa.txt > aaa1.txt
[root@localhost ~]# sort bbb.txt > bbb1.txt
Comparison result:
[root@localhost ~]# comm aaa1.txt bbb1.txt
111
222
aaa
bbb
ccc
ddd
eee
hhh
jjj
ttt
To print the intersection of two files, suppress the first and second columns:
[root@localhost text]# comm aaa.txt bbb.txt -1 -2
bbb
ccc
By suppressing unwanted columns, you can obtain the difference between aaa.txt and bbb.txt:
Lines unique to aaa.txt:
[root@localhost text]# comm aaa.txt bbb.txt -2 -3
aaa
ddd
eee
111
222
Lines unique to bbb.txt:
[root@localhost text]# comm aaa.txt bbb.txt -1 -3
aaa
hhh
ttt
jjj
GNU coreutils package. For more information, see man -s 1 comm or info coreutils 'comm invocation'.
观察