in reply to compare 2 files and print to a third
Assuming that you have files that resemble this:
...and you want to append data from file2 onto the end of data from file1, paired up by keys, you might try something like this:some_unique_key other stuff I want to grab another_key more other stuff that's cool wow_how_many_keys_are_there lot's - why do you ask?
which with inputs of file1:#!/your/perl/here use strict; use warnings; my %file1; my $key; my $value; usage() unless @ARGV == 3; my $output_file = pop @ARGV; # read first file while (<>) { chomp; ($key,$value) = split / /, $_, 2 or warn "Bad data on line $. in file $ARGV, "; $file1{$key} = $value or warn "Bad data on line $. in file $ARGV, "; } continue { # reset line numbers for warning messages # end loop if ( eof ) # note special form of eof { close ARGV; last; } } while (<>) { chomp; ($key,$value) = split / /, $_, 2 or warn "Bad data on line $. in file $ARGV, "; if ( exists( $file1{$key} ) ) { $file1{$key} .= ' ' . $value or warn "Bad data on line $. in file $ARGV, "; } else { warn "Can\'t find key matching <$key> (line $.) " . "in file <$ARGV>, "; $file1{$key} = $value or warn "Bad data on line $. in file $ARGV, "; } } continue { last if ( eof ) # note special form of eof } open( OUT, ">", $output_file ) or die "Error opening $output_file for writing, "; foreach my $k ( keys %file1 ) { print OUT "$k $file1{$k}\n"; } sub usage { die "Need 3 filenames on the command line.\n" . "First 2 files are merged by key into the 3rd file.\n"; } __END__
and file2:one one one one one two two two two two three three three three
yielded:one ONE ONE ONE ONE two TWO TWO TWO TWO three THREE THREE THREE
Update: doesn't handle duplicate keys in the same file. You'd need to create a parallel hash for counters, or complicate the %file1 hash.three three three three THREE THREE THREE one one one one one ONE ONE ONE ONE two two two two two TWO TWO TWO TWO
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
|---|