comm
Compare two sorted lists — what's only in the first, only in the second, in both
comm takes two sorted files and splits the lines into three columns: only in the first, only in the second, in both. It's a quick way to find the intersection and difference of two lists: users, packages, IP addresses, file names.
Syntax
comm [options] file1 file2
comm -12 <(sort a) <(sort b) # if the files aren't sorted yet
One of the files (but not both) can be - — standard input.
Output format
Without options — three columns separated by tabs:
| Column | Indent | What's in it |
|---|---|---|
| 1 | no indent | lines that are only in file1 |
| 2 | one tab | lines that are only in file2 |
| 3 | two tabs | lines that are in both |
Options
Which columns to show
The options hide columns; they combine: -12 = -1 -2.
| Option | What remains | Meaning |
|---|---|---|
-12 | column 3 | intersection: in both |
-23 | column 1 | difference: in the first, not in the second |
-13 | column 2 | difference: in the second, not in the first |
-3 | columns 1 and 2 | symmetric difference: everything that didn't match |
-1 / -2 | — | hide one column |
Other
| Option | What it does |
|---|---|
--check-order | check the sort order and stop with an error at the first unsorted line |
--nocheck-order | don't check the sort order at all |
--output-delimiter=STR | separate columns with the string STR instead of a tab |
--total | add a final line with the line count of each column and the word total |
-z | lines are separated by a NUL byte, not \n |
By default (without --check-order or --nocheck-order) comm notices disorder only where lines didn't find a match: it prints file 1 is not in sorted order but keeps going and returns code 1 at the end.
Examples
printf 'apple\nbanana\ncherry\n' > a.txt
printf 'banana\ncherry\ndate\n' > b.txt
# all three columns
comm a.txt b.txt
# intersection
comm -12 a.txt b.txt
# only in a.txt / only in b.txt
comm -23 a.txt b.txt
comm -13 a.txt b.txt
# unsorted input — sort on the fly
printf 'cherry\napple\n' > c.txt
comm -12 <(sort c.txt) <(sort a.txt)
# one file from standard input
printf 'date\nbanana\n' | sort | comm -12 - b.txt
# comma-separated columns and a summary
comm --output-delimiter=, --total a.txt b.txt
# how many lines are shared
comm -12 a.txt b.txt | wc -l
# strict sort check in a script
comm --check-order -12 a.txt b.txt || echo 'input is not sorted'
Pitfalls
- The input must be sorted the same way
sortsorts it in the current locale. On unsorted inputcommgives a wrong result: lines that are in both files end up in "only in the first" and "only in the second". - Sort in the same locale you run
commin. If a file was sorted withLC_ALL=Candcommruns inen_US.UTF-8(or vice versa), the order may differ. The reliable way is one locale for the whole pipeline:LC_ALL=C sort …andLC_ALL=C comm …. sort -nwon't do.commcompares lines as text, so a numeric sort (9before10) is disorder to it. Use plainsort.--nocheck-orderonly hides the warning; the wrong result is still there, and the exit code becomes 0.- Duplicates are counted one by one. If the first file has
atwice and the second once, oneagoes to the shared column and the other to "only in the first". If you need sets, runsort -ufirst. - Trailing spaces and
\rmake lines different.appleandapple\r(a file from Windows) won't match. Check withcat -A. - There's no case-insensitive option (no
-i). Normalize case beforehand:tr 'A-Z' 'a-z', thensort. - Columns are tab-separated, so in output without
-12/-23/-13the lines "shift". For processing in a script keep a single column or set--output-delimiter.
See also
sort · uniq · join · diff, cmp and patch · grep