One way to do it without file slurping:
use strict;
use warnings;
open MASTER, "file1" or die "cant't open master file: $!\n";
open SUBSET, "file2" or die "can't open subset file: $!\n";
open OUT, ">outfile" or die "can't open output file: $!\n";
while ( my $sline = <SUBSET> ) {
chomp $sline;
my ( $subkey, $subvals ) = split /:/, $sline, 2;
my ( $mkey, $mvals );
while ( my $mline = <MASTER> ) {
chomp $mline;
( $mkey, $mvals ) = split /:/, $mline, 2;
last if $mkey eq $subkey;
}
print OUT join( ":", $subkey, $subvals, $mvals ), "\n";
}
close SUBSET;
close MASTER;
close OUT or die "couldn't close outfile: $!\n";
The above snippet makes the assumption that the master file and the subset file are in the same order, and that every key in subset has an equal key in the master set (which is pretty much what you said). If the master file and the subset file are not in the same order, this method would need to be reworked, since synchronization is critical for its success.
Dave
"If I had my life to live over again, I'd be a plumber." -- Albert Einstein
|