deleting some empty lines with sed

Don Bolton lugnut at sequent.UUCP
Wed May 8 07:58:43 AEST 1991


In article <May91.164529.10132 at x.co.uk> mike at x.co.uk (Mike Moore) writes:
>In article <1991Apr27.143519.26256 at daimi.aau.dk> datpete at 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



More information about the Comp.unix.shell mailing list