in reply to remove single blank lines?

perl -p -0 -e 's/(?<!\n)\n\n(?!\n)/\n/g;' singleblank.txt

See perldoc perlrun for the flags, and perldoc perlre for the regex.

Here is the longer version:

#!/usr/bin/perl -w use strict; # set input record separator $/ (see perldoc perlvar) to undef # to "slurp" the file my $t = do {local $/; (<DATA>)}; # (?<!\n) is negative lookbehind, no \n before # (?!=\n) in negative lookahead, no \n after # in between is \n\n - two consecutive newlines eq a single blank line $t =~ s/(?<!\n)\n{2}(?!=\n)/\n/g; print $t; __DATA__ none one two three one none three two none one

which gives

none one two three one none three two none one

Replies are listed 'Best First'.
Re: Re: remove single blank lines?
by qq (Hermit) on Dec 08, 2003 at 23:33 UTC

    oops, forgot to login.

    qq

    Update: as ysth shows below, my code performs poorly. Why the forward and negative lookarounds aren't working as I expected them to defeats me right now. This regex works much better:

    s/(^|[^\n])\n{2}([^\n]|$)/$1\n$2/g;