Compare two text files and extract lines unique to one of them #

Given two files, e.g.,
$ tail file1 file2
==> file1 <==
Oahu
Maui
Hawaii
Kauai
Molokai
Lanai

==> file2 <==
Hawaii
Kahoolawe
Kauai
Lanai
Maui
Molokai
Niihau
Oahu
here are a couple of methods, courtesy of SiegeX and ghostdog74, for extracting lines unique to the second file:
$ awk 'FNR==NR{a[$0]++}FNR!=NR && !a[$0]{print}' file1 file2
Kahoolawe
Niihau
$ awk 'FNR==NR{a[$0]++;next}(!($0 in a))' file1 file2
Kahoolawe
Niihau
$ comm -13 <(sort file1) <(sort file2)
Kahoolawe
Niihau

UPDATE: In a similar vein, find lines common to two unsorted files like so: comm <( sort file1 ) <( sort file2 )

Additional keywords: uniq, sort, diff, regex

/nix | Sep 27, 2013


Subscribe or visit the archives.