in reply to matching keyword in multi-line records

I'm with moritz: if the file is not too big, reading it into a single scalar string is the way to go (once you actually do read the file). But unlike moritz, I would split on the date pattern, using parens in the split regex so that the dates get returned along with the stuff around them:
use strict; my $text = do { local $/; <> }; my $date_rgx = qr/\d{4}-\d{2}-\d{2}/; my ( $date, $text ) = ( '', '' ); for ( split /\n($date_rgx)/, $text ) { # capture the date (but not th +e preceding \n) if ( /^$date_rgx$/ ) { $date = $_; } elsif ( /\bkeyword\b/ ) { print "$date$_\n"; } }
(not tested; updated to use a variable other than $_ to hold the original file data, and avoid possible confusion in the "for" loop.)