Tuesday, November 18, 2014

Multiline String Matching in Perl

I came across a page here that offers a good explanation of how to use Perl for multiline search and replace. I thought I'd make a note here for future reference.

$ perl -pe 'undef $/; s/stringOnLineOne.*\nstringOnLineTwo//' inputFiles.*

See the section on that page entitled "The Field Record Separators."

A post at perlmonks shows how to use this in practice for multiline search. See the last answer to the question on this page.

For good explanations of the "-p" and "-e" flags, see this page. In brief, "-e" allows entering commands via the command line (as opposed to via a file), and "-p" allows looping over input files and printing the output.

No comments:

Post a Comment