Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!brutus.cs.uiuc.edu!lll-winken!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Novice Question, Successive Lines Keywords: Novice Question, Successive Lines Message-ID: <7500@jpl-devvax.JPL.NASA.GOV> Date: 21 Mar 90 19:50:56 GMT References: <13447@cgl.ucsf.EDU> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: na Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 36 In article <13447@cgl.ucsf.EDU> alonso@maxwell.mmwb.ucsf.edu (Darwin Alonso) writes: : How would one get perl to replace two or more successive blank lines : with one blank line? Two primary ways. First is the slurp method: undef $/; $_ = <>; s/\n{3,}/\n\n/g; print; Second (and probably fastest) is the line-by-line method while (<>) { if ($_ eq "\n") { while (<>) { last unless $_ eq "\n"; } print "\n"; } print; } Alternately, there's while (<>) { next if "$prv$_" eq "\n\n"; $prv = $_; print; } There's also the grep method for Randal: print grep(($_ ne "\n" ? $_ : $prv eq "\n" ? '' : "\n", $prv = $_)[0], <>); Larry