LINE: while (my $line = ) { next LINE while ($line =~ /./ .. $line =~ /CPN/); # Once here, you have matched the first CPN line and onwards. }