my $count = 0; while (<>) { if (/formulae/) { $count++; } if ($count == 2) { print "Found 2nd\n"; } }