deleting some empty lines with sed

David W. Tamkin dattier at vpnet.chi.il.us
Wed May 1 07:11:00 AEST 1991


weimer at ssd.kodak.com (Gary Weimer) wrote in
<1991Apr29.150244.4378 at ssd.kodak.com>:

| In article <1991Apr27.211212.18855 at convex.com>, Tom Christiansen
| <tchrist at convex.COM> writes:

| |> From the keyboard of datpete at daimi.aau.dk (Peter Andersen):
| |> :I have some source-files that I produce documentation from.

| |> :If two or more blank lines appear, I want to remove all but
| |> :one of these.

| |> :I have tried the following sed script

[script deleted]

| |> :but it didn't work.

| |> You need to set up a look with labels and branches.  I see that as I type
| |> this another poster has given you a sed solution, so I won't post my
| |> crufty version of the same.

Well, I haven't seen those sed solutions; they aren't here yet or they're
already gone.

| Wouldn't 'cat -s' be easier? I know, it's not near as exciting, but...

cat -s on a BSD system, yes, but in System V cat -s does something completely
different (it _s_uppresses error messages if input files are nonexistent or
unreadable or if the output file cannot be written).

We went around this subject on comp.editors several months ago.  The best sed
soluation was posted by someone whom I cannot name (because at the same time
a person with a very similar name posted one that didn't work, and I can
never remember which name belonged to which person.

The sed script boiled down to three lines (the ^I represents a tab):

s/[ ^I]*$//
/^$/N
/[!-~]/!D

If the input begins with one or more blank lines, a single empty line will
be preserved at the start of the output.  To get rid of that as well, add
/./,$ !d      after the first line.

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier at vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591

"Parker Lewis Can't Lose" mailing list:
 flamingo-request at esd.sgi.com (relay)  flamingo-request at ddsw1.mcs.com (digest)



More information about the Comp.unix.shell mailing list