First, see How do I post a question effectively? to avoid comments as above.

Assuming that you have files that resemble 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?
...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:

#!/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__
which with inputs of file1:
one one one one one two two two two two three three three three
and file2:
one ONE ONE ONE ONE two TWO TWO TWO TWO three THREE THREE THREE
yielded:
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
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.

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: compare 2 files and print to a third by QM
in thread compare 2 files and print to a third by tux242

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.