in reply to Re: Two line match
in thread Two line match

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

Replies are listed 'Best First'.
Re^3: Two line match
by moritz (Cardinal) on Mar 10, 2008 at 15:25 UTC

    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
        The /g never handles overlapping matches in order to avoid infinite loops.

        But you can modifiy your regex: m/----(?:\n----)+/----/g

Re^3: Two line match
by grizzley (Chaplain) on Mar 10, 2008 at 16:12 UTC

    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

        Understood.

        perl -p0 -e 's/-----\n(\n|-----\n)*-----\n/-----\n/g' file.txt