use strict; use warnings; # Load the acceptable ids from file2 into a hash my %ids; open my $idfile, '<', 'file2' or die $!; for (<$idfile>) { $ids{$1} = 1 if m/Accept\s+=>\s+(\S+)/; } close $idfile; # Scan file1 printing each line where the ID matches one found in our hash open my $datafile, '<', 'file1' or die $!; for (<$datafile>) { chomp; my ($id) = m/ID\s+(\S+)/; print "$_\n" if exists $ids{$id}; } close $datafile; #### ID John06/ext $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY ID lily099/poli $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY ID lily099/poli $wwrk(b05bfn00ld0p8)/b05bfn00ld0p8 ; #<= b05bfn00ld0s0 Size:INFINITY