procmail
[Top] [All Lists]

Re: Counting lines

2000-02-25 14:01:48
2000-02-25-14:53:59 Liviu Daia:
      :0 Bfh
      * H ?? ! ^Lines:
      * -1^0
      *  1^1   ^.*$
      |formail -A "Lines: $="

Cool! I _love_ these things, it's like doing cryptograms, or trying
to decypher Intercal.

Yours starts with the usual :0, with flags to only grep the body
("B"), and only filter ("f") the header ("h").

Then we override the "B" for the first condition, to search the
header and make sure it doesn't already have a Lines: header. That
much comes from procmailrc(5).

Then swich to procmailsc(5) for the real meat in the next two lines.
The first of them just starts the score counter off at -1; it's
w^x with w=-1 and x=0, and since the score accumulates a sum of
w*x^(k-1) for match "k", that means all matches after the first are
adding w*0. An empty pattern always matches.

And the condition after counts lines.

This should work, right?  Well, I've been happy with it for years, until
I discovered that it counts one too _many_ lines.  Any idea what's going
on here?

If it counts one too many, I can only guess that you're wanting to
leave off both the empty line that separates the header from the
body and also the empty line that's appended to messages to make
sure mbox separating "From " lines always have blank lines
preceeding them. In which case you might be happier with the results
if you changed "-1^0" to "-2^0"?

Now the recipe I have in my procmailrc, which I cargo-culted from a
posting on this list, looks like:

        :0
        * ! ^Lines:
        {
          :0B
          * 1^1 ^.*$
          { }
          LINES = $=

          :0 fhw
          | formail -a "Lines: $LINES"
        }

Give the above explanation, I think this one is pretty obvious, it
just does what yours does in 3 recipes rather than one. I'm
switching to yours right now (without the offset); so I'll use:

        :0 Bfhw
        * H ?? ! ^Lines:
        * 1^1 ^.*$
        |formail -A "Lines: $="

Isn't it better kharma to add a "w" to filter statements, so if the
called program finds some way to bomb procmail won't replace the
message header (in this case) with the presumably failed output?

As for the difference between -a and -A, they should be equivalent
in this case, since we're already checking to make sure there's no
Lines header before we start the game.

-Bennett

Attachment: pgpUx5sdDJlsq.pgp
Description: PGP signature

<Prev in Thread] Current Thread [Next in Thread>