my @stuff; while (<>) { if (/^\s/) { $stuff[-1] .= $_; } else { push @stuff, $_; } } print grep { /keyword/ } @stuff;