Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


HPR3889: comm - compare two sorted files line by line

Hosted by Ken Fallon on 2023-06-29 00:00:00
Download or Listen

From the man page "comm - compare two sorted files line by line"

It's part of the core utils package and you can install it using dnf install coreutils on RPM distros, or apt install coreutils on Debian based ones.

[host@hpr]$ man comm

COMM(1)                               User Commands                              COMM(1)

NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

DESCRIPTION
       Compare sorted files FILE1 and FILE2 line by line.

       When FILE1 or FILE2 (not both) is -, read standard input.

       With no options, produce three-column output.  Column one contains lines unique to FILE1,
       column two contains lines unique to FILE2, and column three contains lines common to both
       files.

       -1     suppress column 1 (lines unique to FILE1)

       -2     suppress column 2 (lines unique to FILE2)

       -3     suppress column 3 (lines that appear in both files)

       --check-order
              check that the input is correctly sorted, even if all input lines are pairable

       --nocheck-order
              do not check that the input is correctly sorted

       --output-delimiter=STR
              separate columns with STR

       --total
              output a summary

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       Note, comparisons honor the rules specified by 'LC_COLLATE'.

EXAMPLES
       comm -12 file1 file2
              Print only lines present in both file1 and file2.

       comm -3 file1 file2
              Print lines in file1 not in file2, and vice versa.

AUTHOR
       Written by Richard M. Stallman and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2022 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later
       <https://gnu.org/licenses/gpl.html>.  This is free software: you are free to change and
       redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       join(1), uniq(1)

       Full documentation <https://www.gnu.org/software/coreutils/comm>
       or available locally via: info '(coreutils) comm invocation'

GNU coreutils 9.1

I always find that confusing, so for me it's a lot easier to see what is going on by creating some example files.

First let's create some test files by echoing the number 1 and the number 2 into a file called 1and2.txt

[host@hpr]$ echo "1" > 1and2.txt
[host@hpr]$ echo "2" >> 1and2.txt

And let's create another one with the value 2 and 3 and we'll call it 2and3.txt

[host@hpr]$ echo "2" > 2and3.txt
[host@hpr]$ echo "3" >> 2and3.txt

Then we can see what each command does using these examples.

[host@hpr]$ comm -1 -2 1and2.txt 2and3.txt
2
[host@hpr]$ comm -1 -3 1and2.txt 2and3.txt
3
[host@hpr]$ comm -2 -3 1and2.txt 2and3.txt
1

Comments



More Information...


Copyright Information

Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.

The HPR Website Design is released to the Public Domain.