in reply to comparing 2 files
Create a lookup table from file 2 with the keys being the part of the line excluding the last column. Then go through file 1 and check if the entry is in the lookup table:
open my $fh1, '<', 'file1.txt' or die $!; open my $fh2, '<', 'file2.txt' or die $!; open my $outfh, '>', "outputfile.txt" or die $!; my %lines; while (<$fh2>) { chomp; my ($key, $n) = /(.*)\s(\d+)$/; $lines{$key} = $n; } while (<$fh1>) { chomp; if (exists $lines{$_}) { print $outfh "$_ $lines{$_}\n"; } else { print $outfh "$_ 0\n"; } }
(this assumes your lookup keys are unique — in case the same key can occur multiple times in file 2, you'd have to think about which entry you then want to have in the output file...)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: comparing 2 files
by garyboyd (Acolyte) on Apr 07, 2011 at 13:07 UTC |