procmail
[Top] [All Lists]

Jim Osborn's head search

1996-07-05 13:31:29
Jim was right: there is a way to use scoring to accept a regexp only if it's
in the first N lines of the body without having to hard-code N in the search
expression nor to use another process [such as running head()].  It might
be limited by $LINEBUF, though.

Because the extraction operator looks for the longest possible right side,
we have to attack the problem upside down by counting the lines from the
first appearance of the expression to the end of the text.  If we tried to
extract the text from the start of the body through the regexp, procmail
would continue through the last appearance of the regexp, which might be
beyond the Nth line even if the regexp also appears within the first N lines.

        N=fill_in_the_number

        :0B
        * ()\/regexp(.*$)*
        { REMAINDER=$MATCH }

        :0AB # don't bother if regexp wasn't in the body at all
        * -1^1 ^.*$
        * 1^1 REMAINDER ?? ^.*$
        * $ $N^0
        action_Jim_wants

Let's say there are L lines in the body and that the first occurrence of the
expression is on the Fth line.  $REMAINDER is L-F+1 lines long.  The first
condition scores -L; the second one scores L-F+1; the third scores N.  The
total is N-F+1, which is positive if 1<=F<=N, zero if F=N+1, and negative if
F>N+1.

<Prev in Thread] Current Thread [Next in Thread>
  • Jim Osborn's head search, David W. Tamkin <=