deleting some empty lines with sed
David W. Tamkin
dattier at vpnet.chi.il.us
Thu May 2 10:50:09 AEST 1991
daveh at marob.uucp (Dave Hammond) wrote in <281DB41B.9E6 at marob.uucp>:
| Perhaps I'm oversimplifying the problem, but wouldn't
| tr -s '\012'
| squeeze multiple, consecutive newlines to a single newline?
Yes, but that isn't what we want. The identical misunderstanding came up
when someone asked the same question a few months ago, and many skimmers
made equivalent suggestions [such as sed '/^$/d'], which don't answer the
question at hand.
Compressing all consecutive newlines to a single newline (1) assumes that
there are no non-empty blank lines (containing spaces and tabs but no
printing text) and (2) removes all empty lines. By leaving only one newline
character between two consecutive lines that have text, you're removing all
blank lines from the document.
Since that may not be obvious, let me illustrate. Here is some text. I'll
represent newline characters with "^J".
^J
^J
Hi there!^J
^J
^J
It's nice to see you again.^J
How was your trip?^J
^J
^J
^J
David^J
^J
The goal is to get this:
Hi there!^J
^J
It's nice to see you again.^J
How was your trip?^J
^J
David^J
All leading newlines are stripped, two or more trailing newlines are
compressed to one, and any string of three or more intermediate newlines is
reduced to two.
That's none at the top, two anywhere in the middle, and one at the end.
If we run it through tr -s '\012' we get one newline no matter where, with
this result:
^J
Hi there!^J
It's nice to see you again.^J
How was your trip?^J
David^J
The paragraphing is lost and one blank line at the top is still there.
The question is how to squeeze all consecutive blank lines to a single blank
line (which is what cat -s does on a BSD system). Removing all blank lines
and getting solid blocks of text is not the answer!
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