Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare 3 files and print matches in Perl
by Laurent_R (Canon) on Oct 26, 2015 at 00:18 UTC | |
by Anonymous Monk on Oct 26, 2015 at 00:51 UTC | |
by Laurent_R (Canon) on Oct 26, 2015 at 16:31 UTC | |
|
Re: Compare 3 files and print matches in Perl
by Preceptor (Deacon) on Oct 26, 2015 at 11:58 UTC | |
|
Re: Compare 3 files and print matches in Perl
by vinoth.ree (Monsignor) on Oct 26, 2015 at 03:25 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |