Path: utzoo!utgpu!water!watmath!clyde!att!rutgers!mailrus!ncar!boulder!spot!wu From: wu@spot.Colorado.EDU (WU SHI-KUEI) Newsgroups: comp.unix.questions Subject: Re: sed script to combine blank lines? Keywords: sed Message-ID: <4057@boulder.Colorado.EDU> Date: 14 Oct 88 14:40:17 GMT References: <192@vlsi.ll.mit.edu> <136@nascom.UUCP> <7372@megaron.arizona.edu> Sender: news@boulder.Colorado.EDU Reply-To: wu@spot.Colorado.EDU (WU SHI-KUEI) Distribution: comp Organization: University of Colorado, Boulder Lines: 52 In article <7372@megaron.arizona.edu> rupley@arizona.edu (John Rupley) writes: >In article <136@nascom.UUCP>, rar@nascom.UUCP (Alan Ramacher) writes: >> In article <192@vlsi.ll.mit.edu>, young@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 etc >by the corresponding ascii characters) -- and I suspect it is shorter and >will run faster than an equivalent awk script. > >sed "/^[]*$/{ > N > /\n.*[^]/{ > 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