use strict; use warnings; my $second_file = "npn.txt"; open my $fh2, '<', $second_file or die "Can't write to '$second_file': $!\n"; my @contents; chomp (@contents = (<$fh2>)); close $fh2; my $first_file= "hi5res.txt"; open my $fh1, '<', $first_file or die "Can't write to '$first_file': $!\n"; while( my $eachLine=<$fh1>) { chomp($eachLine); my @array = split(/,/,$eachLine); print $eachLine if (not defined $array[1]); if (grep {$_ eq $array[1]} @contents) { print "$eachLine\n" ; } }