[comp.lang.perl] deleting some empty lines with sed

merlyn@iwarp.intel.com (Randal L. Schwartz) (04/29/91)

In article <1991Apr27.143519.26256@daimi.aau.dk>, datpete@daimi (Peter Andersen) writes:
| Does anyone have a way of doing this, perhaps using something
| else but sed. I'm not a perl-guru, but if its possible in perl
| I'd like to hear about that too.

perl -ne 'print unless /\S/ ? ($skip = 0) : $skip++' <in >out

How this works is left as an exercise to the reader. :-)

print "Just another Perl hacker," unless $skip # :-)
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/03/91)

In article <281DB41B.9E6@marob.uucp> daveh@marob.uucp (Dave Hammond) writes:
: Perhaps I'm oversimplifying the problem, but wouldn't
: 
: tr -s '\012'
: 
: squeeze multiple, consecutive newlines to a single newline?

Yes, it would, and yes, you are.  They wanted to squeeze 3 or more
consecutive newlines down to 2.

Incidentally, with perl 3.044 and later you can do it with

	perl -00pe 's/^\n+//'

or, less efficiently (because of file slurping overhead and string copying
due to modifying the middle of a long string, and because the previous
solution uses anchored search),

	perl -0777pe 's/\n{3,}/\n\n/g'

Oddly enough, any octal number will work in place of 0777 except 012, which
would set the input delimiter to newline.

Larry Wall
lwall@netlabs.com