join <(sort -b file1) <(sort -b file2) #### use warnings; use strict; use Quantum::Superpositions; my $s = do { open my $e, "<", "file1" or die 1; any(<$e>); }; { open my $m, "<", "file2" or die 2; while(<$m>) { $_=~/(\S+)/ and $1==$s and print; }; } __END__