procmail
[Top] [All Lists]

Re: How to remove multiple newlines

2004-07-16 16:04:51
Ruud H.G. van Tol wrote:

See also sed1line.txt:
   http://sed.sourceforge.net/sed1line.txt
   http://www.student.northpark.edu/pemente/sed/sed1line.txt

Eek!  If you only knew what a lousy neighborhood that place is in ...

# delete all CONSECUTIVE blank lines from file except the first; also
# deletes all blank lines from top and end of file (emulates "cat -s")

sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF

sed '/^$/N;/\n$/D'        # method 2, allows 1 blank at top, 0 at EOF

Any sed code with a semicolon is not a proper one-liner, as some seds don't grok that use of the semicolon and require two lines or two -e options.

My experiences with cat -s are that it keeps one blank line (if there are one or more there to start) at the top and likewise one (if there are one or more there to start) at the bottomm, like this:

 sed -e /./,/^$/b -e 1!d # semicolon form doesn't work

Getting sed to trim off all blank lines from the top and the bottom requires some tangling with N and D and maybe P, so I can't write it off the top of my head in Mozilla's editor; I'd need to use vi and do some testing. However, a combination of the two above ought to work, I expect:

 sed '/./,$ !d;/^$/N;/\n$/D'
if not
 sed '/./,/^$/!d;/^$/N;/\n$/D'


____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail