$foundit = 0; while (<$FH>) { $foundit = 1 if /^pattern/; } if ($foundit) { do some processing } #### open FH,"file" or die "Can't open file : $!\n"; $count1 = 0; while (<$FH>) { $count++ if /^pattern/; } close FH; open FH,"file" or die "Can't open file : $!\n"; $count2 = 0; while (<$FH>) { if (/^pattern/ and $count2 < $count1) { $count2++; } else { do some processing } }