procmail
[Top] [All Lists]

Re: How to remove multiple newlines

2004-07-14 18:50:32
At 17:43 2004-07-14 -0500, mrl wrote:
I have a special address me-lo(_at_)me(_dot_)tld, that I use to save bits of 
text I
might find interesting.
Example:
  :0
  * ^Subject:.*todo.*
  | formail -I "" >> $HOME/todo
everything sent to me-lo(_at_)me(_dot_)tld, with a subject containing "todo", 
gets
shoved in my todo file on my home directory. However, usually there is a
leading and trailing newline, after formail gets rid of all the headers. I
could just strip all newlines from the message, like so:
  | formail -I "" | tr -d '\n' >> $HOME/todo

Use sed instead of tr. Then, you can script a little whitespace eater. The book _sed_&_awk_ from ORA would be a good addition to any technical library.

I have a sed script that condenses whitespace and shrinks multiple newlines to no more than two consecutive ones. Not what your're looking for though.

There's (gnu) 'cat -s' which would shrink multiple to just one (that still leaves you with blank lines though).

Probably what you're looking for is:

sed -e '/^$/d'

basically, take blank lines (ones where there's nothing between the beginning of the line ^ and the end of the line $), and delete them. Optionally, chuck in a whitespace character class, which will take care of lines which might have some non-visible spaces or tabs:

:0:
* ^Subject:.*todo
| formail -I "" | sed -e '/^[   ]*$/d' >> $HOME/todo


Besides changing the tr to a sed operation, note addition of the lockfile flag on the flags line - this is important, or else two concurrent message processes could ruin your todo file. Additionally, the '.*' from the tail end of your condition line are removed - they're wholly unnecessary since '*' means ZERO or more, and thus matching nothing would be fine (you'd see it legitimatley used here and there on a MATCH construct though).


I'll take a six-pack of dark beer.  MacTarnahan's Black Watch will do.

---
 Sean B. Straw / Professional Software Engineering

 Procmail disclaimer: <http://www.professional.org/procmail/disclaimer.html>
 Please DO NOT carbon me on list replies.  I'll get my copy from the list.


____________________________________________________________
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