in reply to matching keyword in multi-line records

Slupring the whole file into memory at once tends to often suck. And avoiding that also means you can avoid complicated regexes (using a very SMoP instead).

my $message= ''; while( <> ) { if( ! /^\d{4}-\d\d-\d\d / ) { $message .= $_; } else { print $message if( $message =~ /keyword/ ); $message= $_; } } print $message if( $message =~ /keyword/ );

Or, if you prefer:

my $line= <>; while( defined $line ) { my $message= ''; do { $message .= $line; } while( defined( $line= <> ) && $line !~ /^\d{4}-\d\d-\d\d / ); print $message if( $message =~ /keyword/ ); }

- tye        

Replies are listed 'Best First'.
Re^2: matching keyword in multi-line records (slurp--)
by arthur99 (Initiate) on Oct 21, 2008 at 04:09 UTC
    Thanks to all you wise and benevolent monks for your replies. In reviewing your solutions, it's obvious that I'm still quite the neophyte! Sorry about not posting the output -- it was an oversight and I apologize for that. I'm beggging for clemency as this is my first post :-) Many thanks again to all of you.