my $i = 1; while () { if ($i and /Startpoint/ ... /slack/ && $i--) { print $_; } }