my $threshold = 0; while($line=) { if($line=~ /$SearchString/) { $threshold++; } last if ( $threshold eq 2 ); # continue processing }