Wednesday, August 17, 2011

List lines in file A but not in file B

I found this interesting scripts' page: and I think that I got an alternative to the anb script:


use strict;
use warnings;

sub usage {
        print "$0: filea fileb\n";
        exit -1;

usage unless scalar @ARGV == 2;

my $a = shift @ARGV;
my $b = shift @ARGV;

open A, "<$a" or die "can't open $a: $!\n";
open B, "<$b" or die "can't open $b: $!\n";

my %bl;
$bl{$_}++ for (<B>);

for (<A>) {print "$_" unless $bl{$_}};
Or using map for the last lines:
my %bl = map {$_ => 1 } (<B>);
map {print "$_" unless $bl{$_}} (<A>);


No comments: