If I've understood you right, this should do it:

my %h; # build a giant hash of all the info. Keys are ids, values # are hashrefs whose keys are the source filename and whose # values are the lines themselves. while (<>) { my @fields = split ','; $h{$fields[0]}{$ARGV} = $_; } # for each id (lexically sorted) for my $id (sort keys %h) { my @keys = keys %{$h{$id}}; # if it was present in only one file, print it and move on if (scalar @keys == 1) { print $h{$id}{$keys[0]}; next; } # if it was present in more than one, find out whether # all the lines are the same by building a hash with # each line as the key, then testing whether you end # up with more than one key. my %cmp; $cmp{$_} = '' for values %{$h{$id}}; print keys %cmp if scalar keys %cmp == 1; }

Updated: Now I feel silly. This can be much simpler.

while (<>) { my @fields = split ','; $h{$fields[0]}{$_} = ''; } for my $id (sort keys %h) { print keys %{$h{$id}} if scalar keys %{$h{$id}} == 1; }

and, if one really wanted, the for loop could even be the gratuitously uber-terse:

scalar keys %{$h{$_}} == 1 and print keys %{$h{$_}} for sort keys %h;

I love Perl.

Updated again: You know how it goes. You start thinking about how something can be terser, and next thing you know, you're golfing.

perl -ane '$h{$F[0]}{$_}=0;END{keys%{$h{$_}}==1&&print keys%{$h{$_}}fo +r sort keys%h}' f1.txt f2.txt

OK. I stop procrastinating now.


In reply to Re: Comparing lines of multiple files by Zed_Lopez
in thread Comparing lines of multiple files by oomwrtu

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.