my %ignore; # First pass while () { $ignore{$1} = 1 if some_condition($_); } # Second pass # reset the file to the beginning seek FH, 0, 0; while () { if (m/matches interesting string with (capture)/) { if (exists($ignore{$1}) { next; # don't print this line print; }