I wish to print lines that are in one file but not in another file. However, neither files are sorted, and I need to retain the original order in both files.
comm <(sort a) <(sort b) -3 → Lines in file b that are not in file a
In Perl, load file2 into a hash, then read through file1, outputing only lines that weren't in file2:
use strict;
use warnings;
my %file2;
open my $file2, '<', 'file2' or die "Couldn't open file2: $!";
while ( my $line = <$file2> ) {
++$file2{$line};
}
open my $file1, '<', 'file1' or die "Couldn't open file1: $!";
while ( my $line = <$file1> ) {
print $line unless $file2{$line};
}
fgrep -x -f file2 -v file1
-x match whole line
-f FILE takes patterns from FILE
-v inverts results (show non-matching)
awk 'FNR==NR{a[$0];next} (!($0 in a))' file2 file1