in reply to multi-line RE question

The regex option m here does not do what you think. First, it's not required to match/replace multiple newlines in a string.  Secondly, it has no influence on the behavior of the readline (<>) in the implicit while loop behind the command line option -p. In other words, all you ever have in $_ is one line of text...

One way around the problem would be to set the input record separator to undef to slurp in the whole file at once

$ printf "Line1\nLine2\nLine3\n\nXYZ: Blah\n" | perl -p -e "$/=undef; +s|\nXYZ.+\n||" Line1 Line2 Line3

(You can also say BEGIN { $/=undef } ... if you want to have it execute only once at the expense of a few more keystrokes.)

Whether that is a good solution depends on the circumstances, e.g. how large the input file is, etc.  In case it's huge, you might want to read it in single line mode, and have previous lines accumulate in a buffer up until the search pattern is found. In this case, you can trim the trailing newline in the buffer before printing it out.

$ printf "Line1\nLine2\nLine3\n\nXYZ: Blah\n" | perl -n -e '$buf.=$_; +if ($buf=~s|\nXYZ.+\n||) {print $buf; $buf=""}' Line1 Line2 Line3

Replies are listed 'Best First'.
Re^2: multi-line RE question
by jwkrahn (Abbot) on Aug 02, 2011 at 12:08 UTC

    There is a simpler solution to setting $/ to undef as described in perlrun:

    $ printf "Line1\nLine2\nLine3\n\nXYZ: Blah\n" | perl -0777pe "s|\nXYZ. ++\n||" Line1 Line2 Line3
      Wow, thanks. I try to understand the magic...
Re^2: multi-line RE question
by Anonymous Monk on Aug 02, 2011 at 11:50 UTC
    Thank you! That solves my problem.