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? #### #!/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__ #### one one one one one two two two two two three three three three #### one ONE ONE ONE ONE two TWO TWO TWO TWO three THREE THREE THREE #### 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