sed script to combine blank lines?

WU SHI-KUEI wu at spot.Colorado.EDU
Sat Oct 15 00:40:17 AEST 1988


In article <7372 at megaron.arizona.edu> rupley at arizona.edu (John Rupley) writes:
>In article <136 at nascom.UUCP>, rar at nascom.UUCP (Alan Ramacher) writes:
>> In article <192 at vlsi.ll.mit.edu>, young at vlsi.ll.mit.edu (George Young) writes:
>> > Is there a 'sed' wizard out there?  I often want to take a big ascii file
>> > (like a .c file after cc -E) and collapse each group of 'blank' lines
>> > into exactly one blank line.  'Blank' here is any combination of blanks,
>> > tabs and maybe ^L's.
>> 
>> sed is not powerful enuf for the job, but a simple awk script will
>> work. 
>
>The following simple sed script should work (after replacing <sp> etc
>by the corresponding ascii characters) -- and I suspect it is shorter and
>will run faster than an equivalent awk script.
>
>sed   "/^[<sp><tab><lf>]*$/{
>	N
>	/\n.*[^<sp><tab><lf>]/{
>	b
>	}
>	D
>	}" filename

Just to confirm the above I timed the following sed and awk scripts
on an otherwise empty 3B2/400 running SV3.1. The sed version is
approximately 35% faster when applied to a file which contains 948 and 250
lines before and after stripping off the extra blank lines.


------------------------------ cut here ------------------------------
sed -n '
/^[	]*$/ {
	p
: loop
	n
	/^[ 	]*$/b loop
}
/[!-~][!-~]*/p' $*
------------------------------ cut here ------------------------------

------------------------------ cut here ------------------------------
awk '
	BEGIN					{ empty = 1 }
	$0 !~ /^[ \t]+$|^$/			{print; empty = 0}
	$0 ~ /^$|^[ \t]+$/ && empty == 0	{print; empty = 1}
' $*
------------------------------ cut here ------------------------------

Just a guest here.  In real life
Carl Brandauer
{ncar!uunet}!nbires!bdaemon!carl
attmail!bdaemon!carl



More information about the Comp.unix.questions mailing list