Bash set subtraction Bash set subtraction bash bash

Bash set subtraction


comm -23 <(command_which_generate_N|sort) <(command_which_generate_M|sort)

comm without option display 3 columns of output: 1: only in first file, 2: only in second file, 3: in both files. -23 removes the second and third columns.

$ cat > file1.listABC$ cat > file2.listACD$ comm file1.list file2.list         AB        C    D$ comm -12 file1.list file2.list # In bothAC$ comm -23 file1.list file2.list # Only in set 1B$ comm -13 file1.list file2.list # Only in set 2D

Input files must be sorted.

GNU sort and comm depends on locale, for example output order may be different (but content must be the same)

(export LC_ALL=C; comm -23 <(command_which_generate_N|sort) <(command_which_generate_M|sort))


uniq -u (manpage) is often the simplest tool for list subtraction:

Usage

uniq [OPTION]... [INPUT [OUTPUT]] [...]-u, --unique    only print unique lines

Example: list files found in directory a but not in b

$ ls afile1  file2  file3$ ls bfile1  file3$ echo "$(ls a ; ls b)" | sort | uniq -ufile2


I wrote a program recently called Setdown that does Set operations (like set difference) from the cli.

It can perform set operations by writing a definition similar to what you would write in a Makefile:

someUnion: "file-1.txt" \/ "file-2.txt"someIntersection: "file-1.txt" /\ "file-2.txt"someDifference: someUnion - someIntersection

Its pretty cool and you should check it out. I personally don't recommend the "set operations in unix shell" post. It won't work well when you really need to do many set operations or if you have any set operations that depend on each other.

At any rate, I think that it's pretty cool and you should totally check it out.