my $abort = 0; while (<$fh>) { chomp; if ($_ eq 'START' .. $_ eq 'END') { if ($abort) { $abort = 0 if substr($_, -2) eq 'E0'; next; } ... if (!/cool data/gi) { $abort = 1; next; } ... } }