toys

Tools and things that make my life easier - y'all might like them too

View the Project on GitHub pfuntner/toys

comm2

Purpose

This was inspired by the standard comm Unix utility. I was using it one day to compare a couple of files but grew dissatisfied because it requires that the input files are sorted. This script does not. It simply compares the files as-is without assuming that they’re sorted.

Syntax

Syntax: comm2 [-123] [--json] file1 file2

Options and arguments

| Option | Description | Default | | —— | ———– | ——- | | -1 | Eliminate lines unique to file1 | The default is to print all lines | | -2 | Eliminate lines unique to file2 | The default is to print all lines | | -3 | Eliminate lines unique to both files | The default is to print all lines | | --json | Print results in JSON format | The default is to lines with minimal formatting, only separating the three categories by a tab character. |

Example

$ comm2 <(echo -e one\\ntwo) <(echo -e two\\nthree)
	one
two
		three
$ comm2 <(echo -e one\\ntwo) <(echo -e two\\nthree) | cat -A
^Ione$
two$
^I^Ithree$
$ comm2 -12 <(echo -e one\\ntwo) <(echo -e two\\nthree)
two
$ comm2 -12 --json <(echo -e one\\ntwo) <(echo -e two\\nthree)
[
  {
    "line": "two",
    "pos1": 2,
    "pos2": 1,
    "status": "common"
  }
]
$

Notes