use strict; use warnings; my $rx_start = qr{ \A \s* PATTERN1 }xms; my $rx_stop = qr{ \A \s* PATTERN3 }xms; my @records; RECORD: while (my $record = ) { if ($record =~ $rx_start) { # push @records, $record; # UPDATE: NO: still "greedy" extraction of records/lines @records = $record; # UPDATE: FIXED: only extracts BETWEEN start/stop patterns next RECORD; } if ($record =~ $rx_stop) { print @records, $record; @records = (); next RECORD; } push @records, $record if @records; } exit;