coreutils: Set operations
8.3.6 Union, Intersection and Difference of files
-------------------------------------------------
Combine ‘sort’, ‘uniq’ and ‘join’ to perform the equivalent of set
operations on files:
Command outcome
--------------------------------------------------------------------------
‘sort -u file1 file2’ Union of unsorted files
‘sort file1 file2 | uniq -d’ Intersection of unsorted files
‘sort file1 file1 file2 | uniq’ Difference of unsorted files
‘sort file1 file2 | uniq -u’ Symmetric Difference of unsorted
files
‘join -t'' -a1 -a2 file1 file2’ Union of sorted files
‘join -t'' file1 file2’ Intersection of sorted files
‘join -t'' -v2 file1 file2’ Difference of sorted files
‘join -t'' -v1 -v2 file1 file2’ Symmetric Difference of sorted
files
All examples above operate on entire lines and not on specific
fields: ‘sort’ without ‘-k’ and ‘join -t''’ both consider entire lines
as the key.