use strict; use warnings; my @wantedLines = grep { m{^fileid} .. m{^-----} and m{pattern} } <>; print for @wantedLines;