deleting some empty lines with sed

Mike Moore mike at x.co.uk
Sat May 4 02:45:29 AEST 1991


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.


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


#========================

-- 
---            | usual and obvious disclaimers etc...      | Never take a
Mike Moore     | (anyone daft enough to sue me deserves    | Scorpio seriously.
mike at x.co.uk   |  to win a share in my negative cashflow!) | I know, I am one.



More information about the Comp.unix.shell mailing list