あなたの問題の説明はあまり役に立たないということから始めましょう。次回は、もっと具体的に教えてください。もっと良い解決策を見逃す可能性があります。
あなたの説明から、空白で区切られたデータを含む 2 つのファイルがあることがわかりました。最初のファイルでは、最初の 3 列を検索パターンと照合します。見つかった場合は、最初のファイルの一致する行の 4 番目と 5 番目の列を含む別のファイルのすべての行を検索します。これらの行から、2 番目と 3 番目の列を抽出し、最初のファイルの 1 番目の列と 2 番目のファイルの 2 番目と 3 番目の列を出力する必要があります。さて、ここに行きます:
#!/usr/bin/env perl -nwa
use strict;
use File::Find 'find';
my @search = qw(X Y Z);
# if you know in advance that the otherfile isn't
# huge, you can cache it in memory as an optimization.
# with any more columns, you want a loop here:
if ($F[0] eq $search[0]
and $F[1] eq $search[1]
and $F[2] eq $search[2])
{
my @files;
find(sub {
return if not -f $_;
# verbatim search for the columns in the file name.
# I'm still not sure what your file-search criteria are, though.
push @files, $File::Find::name if /\Q$F[3]\E/ and /\Q$F[4]\E/;
# alternatively search for the combination:
#push @files, $File::Find::name if /\Q$F[3]\E.*\Q$F[4]\E/;
# or search *all* files in the search path?
#push @files, $File::Find::name;
}, '/search/path'
)
foreach my $file (@files) {
open my $fh, '<', $file or die "Can't open file '$file': $!";
while (defined($_ = <$fh>)) {
chomp;
# order of fields doesn't matter per your requirement.
my @cols = split ' ', $_;
my %seen = map {($_=>1)} @cols;
if ($seen{$F[3]} and $seen{$F[4]}) {
print join(' ', $F[0], @cols[1,2]), "\n";
}
}
close $fh;
}
} # end if matching line
多くのシステムコールを含む別のポスターのソリューションとは異なり、これはシェルにまったくフォールバックしないため、十分に高速になるはずです。