procmail
[Top] [All Lists]

Re: Grep from file

2007-11-28 13:21:08
At 11:43 2007-11-28 -0600, Bill McCormick wrote:
:0 h
* ? $GREP -i ^$FROM $TRAVEL
{
  :0
  .Travel/
}

This sends any e-mail with a from address that is found in the .travel
file to the Travel folder.

FYI - the 'h' flag feeds the header to the destination action - it has 
nothing to do with the grep operation (which, sans any flags to the 
contrary, will be provided only the header anyway).  So, if you eliminate 
the 'h' flag, you can then also eliminate the nested recipe:

:0
* ? $GREP -x -i ^$FROM $TRAVEL
.Travel/

Note also the addition of the -x option to grep.  Consider:

FROM=bob(_at_)fubar(_dot_)com

and your travel list contains a line:

joebob(_at_)fubar(_dot_)com

Without the -x, your recipe would have matched.

What I need now is a $DOMAIN variable so I can sort any mail from a
domain (listed in the file) to its folder of happiness.

Already answered by Ruud - the extraction bit he posted is found in the 
procmail sandbox I have published on my website (see URL in my .sigline).

I'm thinking there's some esoteric way to do this with formail and
sed, but I don't know sed well enough.

Don't need sed to accomplish it - unless of course you're trying to get the 
base domain, without optional hostname portion.  I have a trick for that, 
but it's intensive -- I use the 'host' tool to query the SOA record for the 
domain.  However, some cheezeball outfits - such as IBM.COM - cname their 
domain to hosting outfits, so the resultant SOA is for the hosting outfit, 
not the domain itself:

         host -v -t SOA www.ibm.com

(included only for amusement, I don't offer this up as a solution to your 
query)

Using maximal scoring to provide an OR construct (the recipe matches as 
soon as one of the conditions does, and doesn't fail just because one 
doesn't match):

:0
* 9876543210^0 ? $GREP -x -i ^$FROM $TRAVEL
* 9876543210^0 ? $GREP -x -i $FROM_DOMAIN $TRAVELDOMAINS
.Travel/

if you want to use the or construct in the grep operation itself, use egrep 
(or gnu grep with the -E option), with your one monolithic file:

* ? egrep -x -i "($FROM|$FROM_DOMAIN)" $TRAVEL


Another approach is to assign the results of the grep to a variable, then 
check the content of the variable in a condition afterward.  This allows 
you to see the match in your logfile or to emit it into a header, etc.

---
  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

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