Files
oam/knowledge base/comm.md
2023-07-09 18:17:32 +02:00

1.7 KiB

comm

Compares two files line by line.

With no options, produces 3 columns in output:

  • column 1 contains lines unique to file 1;
  • column 2 contains lines unique to file 2;
  • column 3 contains lines common to both files.

Comparisons honor the rules specified by 'LC_COLLATE'.

Table of contents

  1. TL;DR
  2. Further readings
  3. Sources

TL;DR

# Print only lines present in both file1 and file2.
comm -12 'path/to/pre-sorted/file1' 'path/to/pre-sorted/file2'

# Print unique lines of file1 which are not present in file2.
comm -23 'path/to/pre-sorted/file1' 'path/to/pre-sorted/file2'
comm -23 <(sort -u 'path/to/file1') <(sort -u 'path/to/file2')

# Check the whole content of file1 is present in file2.
test $(comm -23 'path/to/pre-sorted/file1' <(sort -u 'path/to/file2') | wc -l) -eq 0
[[ $(comm -23 <(sort -u 'path/to/file1') 'path/to/pre-sorted/file2' | wc -l) -eq 0 ]]

Further readings

Sources

All the references in the further readings section, plus the following: