my $found; while (<>) { my $i = /ABCD/ ... /EFGH/; if ($i) { $found .= $_; if ($i =~ /E/) { print "========\n"; print $found; $found = ''; } } }