in reply to how to use matching operator on newlines

perl -p reads a line at a time, so it never sees any more than one \n at once. Try this:

perl -pi -e 'BEGIN{undef $/} s/\n\n/\n/g;' foo.txt

Replies are listed 'Best First'.
Re^2: how to use matching operator on newlines
by extremely (Priest) on Jan 03, 2007 at 21:19 UTC
    One option to avoid slurping and would be to set $/ to the double return:

    perl -i -pe 'BEGIN{$/="\n\n"} s/\n\n/\n/;' foo.txt

    Another option would be to set $/ and $\ (Yay, output format!) and chomp it:

    perl -i -pe 'BEGIN{$/="\n\n";$\="\n"} chomp;' foo.txt

    -i added back in per kyle's post. Also, ++ to ikegami, too!

    --
    $you = new YOU;
    honk() if $you->love(perl)

      perl -pe 'BEGIN{$/="\n\n";$\="\n"} chomp;' foo.txt
      can be shortened to
      perl -ple 'BEGIN{$/="\n\n"}' foo.txt

      You really need the -i option to edit the file in place as the OP did (I love the irrational number options "-pi -e"). Avoiding slurping is good (especially if it's a large file). The chomp usage is clever, but I think I prefer your first line. Not only does it have one less weird variable, but the s also makes it a lot more obvious what it's doing. On the other hand, maybe I shouldn't be worried about the readability of a one-liner.

Re^2: how to use matching operator on newlines
by redss (Monk) on Jan 03, 2007 at 20:56 UTC
    Good catch. thanks!