comm

Compare two sorted files line by line. Output the lines that are common, plus the lines that are unique.

Syntax
      comm [-123i] file1 file2

Options
   -1    Suppress lines unique to file1
   -2    Suppress lines unique to file2
   -3    Suppress lines that appear in both files 
   -i    Case insensitive comparison of lines

With no options, 'comm' produces three text columns as output. The utility reads file1 and file2, which should be sorted lexically.
This will output:

 Lines only in file1; Lines only in file2; Lines in both files. 

If printing of a column is suppressed, the output will be padded with TAB characters.

The options -1, -2, and -3 suppress printing of the corresponding columns.
The filename '-' means standard input.

Each column will have a number of tab characters prepended to it equal to the number of lower numbered columns that are being printed. For example, if column number two is being suppressed, lines printed in column number one will not have any tabs preceding them, and lines printed in column number three will have one.

The comm utility assumes that the files are lexically sorted; all characters participate in line comparisons.

Unlike some other comparison utilities, 'comm' has an exit status that does not depend on the result of the comparison. Upon normal completion 'comm' produces an exit code of zero. If there is an error it exits with nonzero status.

Collation sorting: the LANG, LC_ALL, LC_COLLATE, and LC_CTYPE environment variables affect the execution of comm as described in environ(7)

Examples

Return the unique lines in the file words.txt that don't exist in countries.txt

$ comm -23 <(sort words.txt | uniq) <(sort countries.txt | uniq)

Return the lines that are in both words.txt and countries.txt:

$ comm -12 <(sort words.txt | uniq) <(sort countries.txt | uniq)

Return the files that are in the directory 'march' but not in the directory 'april':

$ comm -23 <(ls march) <(ls april)

Return the files that are in the directory 'march' but not in the directory 'april':

$ comm -23 <(ls march) <(ls april)

Return the files that are in the directory 'april' but not in the directory 'march':

$ comm -13 <(ls march) <(ls april)

Copy those files to the directory 'result'

$ mkdir result
$ cp april/`comm -13 <(ls march) <(ls april)` result

“When I'm President, I will go line by line to make sure that we are not spending money unwisely” - Barack Obama

Related macOS commands:

cmp - Compare two files.
diff - Display the differences between two files.
diff3 - Show differences among three files.
sdiff - merge two files interactively.
sort - Sort text files.
uniq - Uniquify files.
Command substitution - `with backquotes`


 
Copyright © SS64.com 1999-2020
Some rights reserved