Skip to content
Latchkey

comm: Compare Two Sorted Files

comm reads two sorted files and prints three columns: lines only in the first, only in the second, and in both.

comm is the set tool: differences and intersections of two lists. Both inputs must be sorted, or the results are wrong.

What it does

comm compares two sorted files line by line and emits three tab-indented columns: column 1 is lines only in file1, column 2 is lines only in file2, column 3 is lines in both. -1, -2, -3 suppress the matching column, so -23 leaves only file1-unique lines (set difference) and -12 leaves only common lines (intersection).

Common usage

Terminal
comm -23 <(sort a.txt) <(sort b.txt)    # in a but not b
comm -12 <(sort a.txt) <(sort b.txt)    # in both (intersection)
comm -3 <(sort a.txt) <(sort b.txt)     # lines not shared

Options

FlagWhat it does
-1Suppress column 1 (lines only in file1)
-2Suppress column 2 (lines only in file2)
-3Suppress column 3 (lines in both)
-23Set difference: only file1 lines
-12Intersection: only common lines

In CI

comm -23 sorted_expected sorted_actual lists what is missing from actual, a clean way to assert a generated list matches an expected one. Sort both inputs the same way (use process substitution <(sort ...)) so the comparison is valid.

Common errors in CI

"comm: file 1 is not in sorted order" appears when an input is not sorted; comm requires both files sorted with the same collation. Mismatched locales between the two sorts can make matching lines appear unequal; set LC_ALL=C on both. Tabs in the output come from the column indentation; pipe a single column through with the right -1/-2/-3 suppression to avoid them.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →