in reply to Two line match

s/-----\n-----/------/g or s/-----\n+-----/------/g both should work, but only if you have both lines in $_.

If you read your files with a standard loop like this:

while (<>)[ # substition here }
it can never work, because there's only one line in $_ when you run the substition.

Replies are listed 'Best First'.
Re^2: Two line match
by jrosenfeld (Initiate) on Mar 10, 2008 at 15:21 UTC
    I am trying to do it as a one-liner on the command line using

    cat foo.txt | perl -p -e 's/-----\n-----/------/g' and it does not do anything. Is there a problem with multiple lines and command-line matching? Jeff

      The -p option enables line-by-line processing, so you have to change perls notion of a line:

      perl -pe 'BEGIN { $/ = undef }; s/---\n---/---/g;'
        That works except that it only converts two lines of ----- into one. I wanted it to be able to convert any number of lines on ----- into 1 line of it. Shouldn't the /g do that? Jeff

      use -0 to read whole file at once:

      cat foo.txt | perl -p0 -e 's/-----\n\n-----/------/g'
        Thank you for the suggestion, but it only takes two lines and converts it into 1. I would like a one-liner to convert any number of lines of:

        ------

        into one line of it. Jeff