[Chicago-talk] Replacing consecutive newlines with a single newline

Steven Lembark lembark at wrkhors.com
Fri Sep 26 09:54:06 PDT 2008

Vijay Kumar wrote:
> Wow...that solves my problem.
> Thanks Andy, Randall and Jay.
> I am thrilled to get responses from you.
> However, I would like to use the following one because I think it is
> more readable.
>      perl -pe 'BEGIN{undef $/} s/\n+/\n/g' inputfile

All this is doing is rejecting lines with nothing on

     print -n -e 'print unless /^\n/'

This can also be written as

     perl -n -e 'print if /./'

Since since the '.' won't match a newline and you'll only
print lines with SOMEthing else on them.

Note: This isn't golf: depending on the size of your file
processing it by line can actually speed things up and
saves you from creating huge input buffers for no good
reason (see URI's description of Slurp for more). You
also have to examine fewer characters (just the first
one on each line) rather than searching and replacing
the entire input buffer.

Steven Lembark                                            85-09 90th St.
Workhorse Computing                                 Woodhaven, NY, 11421
lembark at wrkhors.com                                      +1 888 359 3508

More information about the Chicago-talk mailing list