procmail
[Top] [All Lists]

Re: How to extract multiple email addresses in To: header

2005-05-13 13:19:56
On Fri, May 13, 2005 at 10:47:08AM -0500, mark david mcCreary wrote:
Can someone point me to a recipe that will extract pure email addresses
(not the text verbage) from the To and Cc headers.

For example,

To: Joe Schmoo <joe(_at_)schmoo(_dot_)com>, Bob Smith 
<bob(_at_)smith(_dot_)org>
Cc: Sam Spade <sam(_at_)spade(_dot_)com>

I want to end up with a procmail variable containing

joe(_at_)schmoo(_dot_)com, bob(_at_)smith(_dot_)org, sam(_at_)spade(_dot_)com

Using patched Bash,

    http://freshmeat.net/projects/bashdiff/
    http://home.eol.ca/~parkw/index.html#bash

I would do something like

    a='"a b" <aa(_at_)bb>, "c(_at_)d" <cc(_at_)dd>, ee(_at_)ff (e f)'

    IFS=',<>'${IFS}
    set -- $a                   # "a b" aa(_at_)bb "c(_at_)d" cc(_at_)dd 
ee(_at_)ff e f
    set -- ${*|/@}              # aa(_at_)bb "c(_at_)d" cc(_at_)ff ee(_at_)ff
    set -- ${*|~[\"\']}         # aa(_at_)bb cc(_at_)ff ee(_at_)ff
    echo "$*"                   # aa(_at_)bb,cc(_at_)ff,ee(_at_)ff

If you want space after comma, then

    echo "${*|,, }"             # aa(_at_)bb, cc(_at_)ff, ee(_at_)ff

Translating this to standard Bash or Ksh is left as homework.
Explanation of above notation is as follows:

    - ${...|/regex}     --> return items containing 'regex'
    - ${...|~regex}     --> return items not containing 'regex'
    - ${...|,sep}       --> join items with 'sep'

-- 
William Park <opengeometry(_at_)yahoo(_dot_)ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
           http://home.eol.ca/~parkw/thinflash.html

____________________________________________________________
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