comm
comm compares two sorted files and tells you which lines are unique to each and which are common to both.
comm [options] file1 file2
By default it prints three columns:
$ comm list-a.txt list-b.txt
apple
banana
cherry
date
- Column 1 — lines only in
file1. - Column 2 — lines only in
file2. - Column 3 — lines in both.
Each column is indented past the one before, so a line's column position tells you where it was found.
The input must be sorted
comm steps through both files together, so both must be sorted. On unsorted input the comparison is meaningless. Sort the files first with sort.
comm checks the ordering and reports a file that is out of order. --check-order forces the check; --nocheck-order disables it.
Showing only the columns you want
Each column can be switched off. The remaining columns close up to fill the gap.
| Option | Effect |
|---|---|
-1 |
Suppress column 1 — hide lines unique to file1. |
-2 |
Suppress column 2 — hide lines unique to file2. |
-3 |
Suppress column 3 — hide lines common to both. |
These combine to answer specific questions. comm -12 shows only the common lines — the intersection of the two files. comm -23 shows lines in file1 that are not in file2 — the difference.
Other options
| Option | Effect |
|---|---|
--output-delimiter=STR |
Separate the columns with STR instead of spaces. |
--total |
Print a final summary line with the count in each column. |
-z, --zero-terminated |
Treat the NUL character as the line delimiter. |
Exit status
| Code | Meaning |
|---|---|
0 |
Success. |
1 |
A file could not be read, or was not sorted. |