Compare Lines
Shared Lines
With grep
grep -Fxf file1.txt file2.txtWith comm
comm requires both inputs sorted first. -1 -2 suppress the
file1-only and file2-only columns, leaving just the shared lines (column
3):
comm -12 <(sort file1.txt) <(sort file2.txt)Differing Lines
With grep
Put the expected smaller first
grep -Fvxf file1.txt file2.txtWith awk
awk 'NR==FNR{a[$0]; next} !($0 in a)' file2.txt file1.txtWith comm
Same sorted-input requirement. -3 suppresses the shared-lines column,
leaving lines unique to file1 (column 1) and file2 (column 2) side by
side, tab-separated:
comm -3 <(sort file1.txt) <(sort file2.txt)Isolate just one side by also suppressing the other unique column:
comm -23 <(sort file1.txt) <(sort file2.txt) # only in file1
comm -13 <(sort file1.txt) <(sort file2.txt) # only in file2With diff
Unlike grep/comm, diff is line-order-sensitive — it doesn’t just
compare which lines exist, but where.
diff file1.txt file2.txt