spraff spraff - 2 months ago 14
Perl Question

*nix: perform set union/intersection/difference of lists

I sometimes need to compare two text files. Obviously,

diff
shows the differences, it also hides the similarities, which is kind of the point.

Suppose I want to do other comparisons on these files: set union, intersection, and subtraction, treating each line as an element in the set.

Are there similarly simple common utilities or one-liners which can do this?




Examples:

a.txt

john
mary


b.txt

adam
john





$> set_union a.txt b.txt
john
mary
adam





$> set_intersection a.txt b.txt
john





$> set_difference a.txt b.txt
mary

Answer

Union: sort -u files...

Intersection: sort files... | uniq -d

Difference: sort files... | uniq -u

Comments