# untested snippet my @good_lines,@bad_lines; foreach my $file_name (@file){ open(MYFILE, $file_name) or die qq(Cannot open '$file_name' : $!\n); # Read the whole file into memory my @lines = ; # And find the good and bad lines push @good_lines, grep { ! /NOT|INVALID|MUST|ERROR/ }, @lines; push @bad_lines, grep { /NOT|INVALID|MUST|ERROR/ }, @lines; }; print "good: @good_lines"; print "bad : @bad_lines";