$linuxjunkies
>

comm(1)

Compare two sorted files line by line and output unique and common lines.

UbuntuDebianFedoraArch

Synopsis

comm [OPTION]... FILE1 FILE2

Description

comm compares two sorted text files and prints three columns: lines unique to FILE1, lines unique to FILE2, and lines common to both. Each column's presence is controlled by suppression flags.

Both input files must be sorted in the same collating order (typically with sort). If a file is -, input is read from standard input.

Common options

FlagWhat it does
-1suppress output of lines unique to FILE1
-2suppress output of lines unique to FILE2
-3suppress output of lines common to both files
-iignore case differences when comparing lines
--check-ordercheck that input files are properly sorted; exit with error if not
--nocheck-orderskip checking if files are sorted (default in some versions)
--output-delimiter=STRuse STR instead of TAB to separate output columns

Examples

Show all three columns: unique to file1, unique to file2, and common lines

comm file1.txt file2.txt

Show only lines common to both files (suppress columns 1 and 2)

comm -12 file1.txt file2.txt

Show only lines unique to file1 (suppress columns 2 and 3)

comm -23 file1.txt file2.txt

Show only lines unique to file2 (suppress columns 1 and 3)

comm -13 file1.txt file2.txt

Find users added or removed by comparing two user lists (requires pre-sorting)

sort users_old.txt > sorted_old.txt && sort users_new.txt > sorted_new.txt && comm -3 sorted_old.txt sorted_new.txt

Compare files ignoring case differences

comm -i file1.txt file2.txt

Related commands