open(FILE, "; foreach (@file) { chomp; # --> Note: no need to chomp, it just forces you to add the \n again when you print next unless /\S/; # skip blank lines --> Note: this is useless given that you print only line matching the /Passed CLEAN/ pattern if (/Passed CLEAN/) { #search string Passed CLEAN print "$_ \n"; # print ALL lines containing Passed CLEAN } } #### open(FILE, "; foreach (@file) { print if /Passed CLEAN/; }