Q:

Print lines from one file that are not contained in another file

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.
 contents of file1:
 string2
 string1
 string3

 contents of file2:
 string3
 string1

 Output:
 string2

Is there a simple script that I can accomplish this in?

A:

fgrep -x -f file2 -v file1

-x match whole line
-f FILE takes patterns from FILE
-v inverts results (show non-matching)

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};
}

A:

awk 'FNR==NR{a[$0];next} (!($0 in a))' file2 file1

