Text Manipulation: Multiple Line Regex Deletions Using Perl

Removing portions of files that match a multiple line regular expression can be tricky, unless you’re using perl. Let’s take an example file:

We would like to remove all headers that are not followed by any data. Not much of an example but it’ll demonstrate the technique nethertheless! The easiest way to do this with perl is to read the entire file into a single scalar variable, and then just parse that substituting our multiple line regular expression with nothing. Observe:

Obviously you’ll need to redirect the output of this file, or just write $data out to a new file within the perl script itself.

Running the script on the example data gives the expected output:

Using this method, you can easily modify the regular expression in the perl script to suit your needs.