Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!unisoft!fai!sequent!lugnut From: lugnut@sequent.UUCP (Don Bolton) Newsgroups: comp.unix.shell Subject: Re: deleting some empty lines with sed Message-ID: <59489@sequent.UUCP> Date: 7 May 91 21:58:43 GMT References: <1991Apr27.143519.26256@daimi.aau.dk> <281DB41B.9E6@marob.uucp> Reply-To: lugnut@sequent.UUCP (Don Bolton) Organization: Sequent Computer Systems, Inc Lines: 89 In article mike@x.co.uk (Mike Moore) writes: >In article <1991Apr27.143519.26256@daimi.aau.dk> datpete@daimi.aau.dk (Peter Andersen) writes: >>I have some source-files that I produce documentation from. >> >>I use sed to make a few changes to the text. I have figured >>most of it out, but I have one problem remaining: >>If two or more blank lines appear, I want to remove all but >>one of these. >>[sed example deleted] >>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. > try... cat yourfile | nawk -f "the 2 non-blank lines below" :-) BEGIN { RS = "" } {print $0; print ""} the above works fine, unless there are some size limits I'm not hitting here.. > >The sed script below works, but messes up on lines at the beginning >and end of the file. It may also have problems with large files. >The awk script works wonderfully! > >in=`cat $1 | tr '\012' ' '` > >echo $in | sed -e 's/ [ ]*/\ >\ >/g' -e 's/ /\ >/g' > >#======================== > >awk ' BEGIN { blank=0 > line=0 > } > { ># remove here... > if ( blank == 0 ) > { > if ( $0 != "" ) > { > blank++ > > if ( line != 0 ) > print "" > > print $0 > } > else > line++ > } > else > { ># to here, if you do not want to account for blank lines at beginning > > if ( $0 == "" ) > line=1 > else > { > if ( line != 0 ) > { > print "" > line=0 > } > > print $0 > } > ># (and this...) > } > > } > ># remove here... > END { if ( line != 0 ) > print "" > } ># to here, if you do not want to account for blank lines at end > > ' $1 > > >#======================== Don "simple Simon" Bolton