perl -ne 'print if (/MYLARGEREGEX_HERE/../END_OF_BLOCK/)' inputfile > outputfile #### #!perl use strict; use warnings; open my $fh_big_file, '<', $ARGV[0] || die; #first argument must be the input file open my $fh_regex, '<', $ARGV[1] || die; # second argument points to the file containing the regex my $regex = <$fh_regex>; while(<$fh_big_file>) { print if (/$regex/../^END_OF_BLOCK/); } #### while(<$fh_big_file>) { print if (/MY_HUGE_REGEX_JUST_PLAIN/../^END_OF_BLOCK/); } #### while(<$fh_big_file>) { print if (/$regex/o .. /^END_OF_BLOCK/); }