procmail
[Top] [All Lists]

Re: Variable assignment on filtering Body

1999-03-30 22:39:42
At 05:15 PM 3/30/99 -0500, Banerjee, Tapas wrote:

      I have a very simple question regarding assignment of variable after
filtering 
      some condition of mail body. I looked into "procmail FAQ"
      it lists this is one of the known bug.

I think you misunderstood the "bug" -- the one you later list doesn't
seem to relate to what you were doing (and has an easy workaround).

I found an ugly
workaround(using cat),
      appreciate if any one can provide any better idea.

If I understand your example, you want to:
        1. Put the first line of the message body into variable SUBJ;
        2. Remove the first line of the message body; and
        3. Do something with the result, using $SUBJ.
If I misread you; well, sorry.


      Let me elaborate the receipe and my problem.
      I want to strip first line of mail body and store it as variable and
use
      the varible at known places - say naming a file.

      The recepie looks like this

      :0
      * 
^From:(_dot_)*[david(_dot_)schuman(_at_)ny(_dot_)email(_dot_)gs(_dot_)com |
tapas(_dot_)banerjee(_at_)ny(_dot_)email(_dot_)gs(_dot_)com ]

That's gotta be wrong; [] enclose a set of characters.  See below.

      * !^Subject:.*RE:
      * !^Subject:.*unsubscribe
      {
                     :0 bwc             # 1. Strip first line assign that
to SUBJ variable
             | /bin/head -1 > sub_hdr; SUBJ="${SUBJ:-`cat sub_hdr` }" #
<-------- ????

See below for a simple method, no processes.


                    :0 fbw              # 2. Remove the line from mail
body
                  | egrep -v $SUBJ

This may remove more than the first line; it could match text
elsewhere in the body.  See more accurate method below.


                    :0:                  # 3. Store in a file
                  | (formail -A"Bcc: $SUBJ" \
                                ^^^^^^
You really want to add a Bcc header with arbitrary line received in a message?!?

                  -I"To:=?iso-2022-jp?B?GyRCJDQ5WEZJJE4zJyQ1JF4kWBsoQg==?="
\
                  -I"MIME-Version: 1.0" \

-I"From:=?iso-2022-jp?B?GyRCJTQhPCVrJUklXiVzISYlNSVDJS8lOUVqPy4bKEI=?=" ) >>
"SUBJ"_mails

^^^^^^^
I presume you wanted "$SUBJ" here.  Also, this got wrapped somewhere before
it got to me, so probably doesn't look like what you think you wrote.

      }

      Problem appears in #1. $SUBJ comes as "".
      I tried different ways e.g. SUBJ=`head -1`, SUBJ=|`head -1`,....
etc. stil no better.
      But if I replace $SUBJ at subsequent places e.g. #2,#3 as `cat
sub_hdr`
      it works fine.

      Procmail FAQ lists this as know bug --
---------------------------------------------------------------------------
      Variable capture clobbers variable's old value 
              I.e. the following doesn't work as expected: 
                      :0
                      variable=| echo "$variable" | tr A-Z a-z
              The value of variable will be empty by the time the echo
executes. 
      " 

This doesn't have anything to do with what you were trying (that I can see)
and if you did need to do it, just do:
                        variable2="$variable"
                        variable=| echo "$variable2" | tr A-Z a-z


----------------------------------------------------------------------------
------------------------------------------
      Looking for a simple solution.
      Thanks in advance.      

OK, try mine.  For #1, this extracts the first line from the body into $MATCH,
then copies into $SUBJ.  For #2, it uses awk to remove EXACTLY the first body
line (if you have gawk, mawk or nawk use them; they're generally faster than
vanilla awk, or use sed or something; but your egrep may remove the wrong 
stuff).
For #3, I'm not touching yours :-), so I changed it to a simple example.

# note the "fixes" to your conditions
:0
* ^From:.*(david\.schuman|tapas\.banerjee)@ny\.email\.gs\.com
* !^Subject:.*\<re:
* !^Subject:.*unsubscribe
* B ?? ^\/.*
{
    # 1. put first line into $SUBJ (extracted by "B ??" condition above)
    SUBJ="$MATCH"

    # 2. remove first body line
    :0 bfw
    | awk 'NR>1'

    # 3. Do what you want... I just add a header & store it here
    :0:
    | formail -A "Hello: $SUBJ" >> some_file
}


Hope that helps,
Stan

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