← Full reference

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:

ColumnIndentWhat's in it
1no indentlines that are only in file1
2one tablines that are only in file2
3two tabslines that are in both

Options

Which columns to show

The options hide columns; they combine: -12 = -1 -2.

OptionWhat remainsMeaning
-12column 3intersection: in both
-23column 1difference: in the first, not in the second
-13column 2difference: in the second, not in the first
-3columns 1 and 2symmetric difference: everything that didn't match
-1 / -2—hide one column

Other

OptionWhat it does
--check-ordercheck the sort order and stop with an error at the first unsorted line
--nocheck-orderdon't check the sort order at all
--output-delimiter=STRseparate columns with the string STR instead of a tab
--totaladd a final line with the line count of each column and the word total
-zlines 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 sort sorts it in the current locale. On unsorted input comm gives 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 comm in. If a file was sorted with LC_ALL=C and comm runs in en_US.UTF-8 (or vice versa), the order may differ. The reliable way is one locale for the whole pipeline: LC_ALL=C sort … and LC_ALL=C comm ….
  • sort -n won't do. comm compares lines as text, so a numeric sort (9 before 10) is disorder to it. Use plain sort.
  • --nocheck-order only 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 a twice and the second once, one a goes to the shared column and the other to "only in the first". If you need sets, run sort -u first.
  • Trailing spaces and \r make lines different. apple and apple\r (a file from Windows) won't match. Check with cat -A.
  • There's no case-insensitive option (no -i). Normalize case beforehand: tr 'A-Z' 'a-z', then sort.
  • Columns are tab-separated, so in output without -12/-23/-13 the 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