I have a list of items in 2 files. Some items are present in both the files in any order. Say for example item name is 'apple', it may be present in line 2 of file 1 and line 10 of file 2.
I want to see the missing items only. In simple terms, Items present in file1 if not in file2 then output the missing items to another file as "<itemname> not in file2.
m1.pl
#!/usr/bin/perl
while (<>) {
chomp;
if ($ARGV=~/file2/) {
$keys{$_}++;
} else {
print "$_\n" if (! $keys {$_});
}
}
$ ./m1.pl file2 file1
pears
oranges
m2.pl
#!/usr/bin/perl
open FILE, "< $ARGV[1]" or die "Can't find $ARGV[1]: $!";
while (<FILE>) {
chomp;
$keys{$_}++;
}
close FILE;
open FILE, "< $ARGV[0]" or die "Can't find $ARGV[0]: $!";
while (<FILE>) {
chomp;
print "$_\n" if (! $keys {$_});
}
close FILE;
$ ./m2.pl file1 file2
pears
oranges
Personally, I prefer methods like these over sort because they can process millions and millions of records in both files very fast passing through each set only once.