I am trying to compare three files and print the lines that are common to all three files. The columns in the files are separated by tabs. For example:
file 1:

fji01dde AIDJFMGKG

dlp02sle VMCFIJGM

cmr03lsp CKEIFJ

and so on...

file 2:

fji01dde 25 30

dlp02sle 40 50

cmr03lsp 60 70

and so on...

file 3:

AIDJFMGKG

CKEIFJ

output needs to be:

fji01dde AIDJFMGKG 25 30

cmr03lsp CKEIFJ 60 70

and so on...

I only want lines that are common in all three files.

The below code works well for the first two files, but I need to incorporate the third file. Any ideas?

#!/usr/bin/env perl use strict; my (%file1,%file2); ## Open the 1st file open(A,"file1"); while(<A>){ chomp; ## Split the current line on tabs into the @F array. my @F=split(/\t/); push @{$file1{$F[0]}},@F[1..$#F]; } ## Open the 2nd file open(B,"file2"); while(<B>){ chomp; ## Split the current line on tabs into the @F array. my @F=split(/\t/); if (defined($file1{$F[0]})) { foreach my $col (@{$file1{$F[0]}}) { print "$F[0]\t$col\t@F[1..$#F]\n"; } } }

In reply to Compare 3 files and print matches in Perl by Anonymous Monk

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.