comm
Overview
comm compares two sorted files line by line and outputs three columns: lines only in file1, only in file2, and in both.
Syntax
comm [options] file1 file2Common Options
| Option | Description |
|---|---|
-1 |
Suppress column 1 (only in file1) |
-2 |
Suppress column 2 (only in file2) |
-3 |
Suppress column 3 (both) |
--check-order |
Error if inputs not sorted |
--output-delimiter=STR |
Column separator |
Key Use Cases
- Set difference of two lists
- Intersection of sorted IDs
- Audit user/host lists
Examples with Explanations
Prepare sorted inputs
sort a.txt -o a.sorted
sort b.txt -o b.sorted
comm a.sorted b.sortedUnsorted input produces wrong results.
Only in first file
comm -23 a.sorted b.sortedSuppress 2 and 3 → unique to A (set difference A−B).
Intersection
comm -12 a.sorted b.sortedLines common to both.