Wednesday, May 11, 2011

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

fgrep -x -f file2 -v file1

-x match whole line

-f FILE takes patterns from FILE

-v inverts results (show non-matching)


OR


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

No comments: