sed script to combine blank lines?

Maarten Litmaath maart at cs.vu.nl
Sat Oct 15 12:42:28 AEST 1988


In article <4057 at boulder.Colorado.EDU> wu at spot.Colorado.EDU (WU SHI-KUEI) writes:
\------------------------------ cut here ------------------------------
\sed -n '
\/^[	]*$/ {
    ^
\	p
\: loop
\	n
\	/^[ 	]*$/b loop
\}
\/[!-~][!-~]*/p' $*
\------------------------------ cut here ------------------------------

At the place the caret is pointing to, a space should be added (there's only
the tab)! Furthermore, formfeeds were considered white space too.
This is an indication that non-printable characters (in essential places)
should be avoided. One way to do it here:

chars=" `echo tf | tr tf '\11\14'`"

The first `p' command should be replaced by `s/.*//p', to meet what the
original poster wanted. For the same reason the `/[!-~][!-~]*/p' command
is doing too much: `p' suffices.

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

The `next' command of `awk' can simplify this script (see my previous
posting). Why not use `/^[ \t\f]*$/' for the regular expression?
Your `sed' solution (+ modifications) is the best I've seen so far.
My `awk' solution is easier to program, but a lot slower indeed.
-- 
Hippic sport:                         |Maarten Litmaath @ Free U Amsterdam:
             a contradiction in terms.|maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.questions mailing list