replacing two EOL chars by one

Jesse Tov tov at eecs.harvREMOVEard.edu
Sun Dec 21 04:57:26 CET 2003


Pascal Bourguignon <spam at thalassa.informatimago.com>:
> Even with sed I find difficult to do that.

If you want to remove blank lines, then sed '/^$/d' will do.  If, on
the other hand, you actually want to compress pairs of newlines into
single newlines (as I think the specification may have said), something
a bit more complicated is required.  I think that sed 'N;P;/\n$/d;D'
will do it.

If you want to use Perl or Ruby, these are equivalent to the first sed
solution:

    ruby -ne 'print unless /^$/'
    perl -ne 'print unless /^$/'

In Ruby you might also use:

    ruby -pe 'next if /^$/'

The equivalent doesn't work in Perl, probably because -p puts the
implicit print statement in a "continue" block, which gets executed
before the loop is restarted by "next".

If you're doing this on a huge file and care about speed, sed will
be slightly faster than Perl and Perl will blow Ruby away.  Because
I was curious, I tried writing it in C to see if I could beat sed.
My first attempt was slightly slower (!), but my second attempt ran
much faster than sed; unfortunately, it also took 30 minutes and
34 lines.

Jesse




More information about the Python-list mailing list