Grep from file

Professional Software Engineering PSE-L at
Wed Nov 28 21:14:21 CET 2007

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:

* ? $GREP -x -i ^$FROM $TRAVEL

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

FROM=bob at

and your travel list contains a line:

joebob at

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

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

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

* 9876543210^0 ? $GREP -x -i ^$FROM $TRAVEL
* 9876543210^0 ? $GREP -x -i $FROM_DOMAIN $TRAVELDOMAINS

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: <>
  Please DO NOT carbon me on list replies.  I'll get my copy from the list.

More information about the procmail mailing list