procmail
[Top] [All Lists]

Re: Using sed

2001-04-23 04:27:46
On Sun, 22 Apr 2001, Don Hammond <procmail1(_at_)tradersdata(_dot_)com> wrote:

On 21 Apr, Trevor Jenkins wrote:
|
|     TAG=`echo -n $SUBJECT|sed -e "s/\[([A-Za-z0-9\-_ ]*)\]/\1/g"`
|
| I've tried the failing echo and sed combination in an interactive shell
| and it works. (That is it captures any alphanumerics between brackets.)
| However when I try the above procmailrc line it fails.

That this works at a shell prompt seems unlikely. Using the same version
of sed here (GNU v3.02) it doesn't do what you want, nor would I expect
it to.

Faulty test data on my part when doing this at the shell level.

The first problem is the back reference requires escaped
parentheses. I believe you're trying to match literal parentheses inside
of brackets above. The second problem is, even after escaping the
parentheses, your just replacing "[tag]" with "tag" and not changing any
text before or after.

Knock-on effect of my faulty data.

You probably want something more like:

   TAG=`echo $SUBJECT |sed 's/.*\[\([-A-Za-z0-9_ ]*\)].*/\1/'`

What I changed it to was

TAG=`echo -n "$SUBJECT"|sed -e "s/\(.*\)\[\([A-Za-z0-9\-_& ]*\)\]\(.*\)/\2/g"`

which is an awful regexp to understand.

I would just note that this still might not do what you want if fed
multiple bracketed tags, or unmatched brackets. Presumably the procmail
code deals with those possibilities.

Noted. Some lists I'm on have additional tags to highlight specific
content. All I want to be, at this time, is to extract the first tag on
the subject header line.

I would also suggest using the built-in match operator and skipping sed
altogether.

:0
* ^Subject:.*\[[-A-Za-z0-9_ ]+]
* ^Subject:.*\[\/[-A-Za-z0-9_ ]+
{
  TAG=$MATCH
}

This is certainly easier to understand (as a regexp).

The first condition makes sure there is at least one pair of brackets,
the second condition extracts the contents within the _first_ bracket
pair for assignment to TAG within the braces.

I'd tried something similar but had put .* at the end, which of course
assigned the subject string in addition to the tag.

Regards, Trevor

British Sign Language is not inarticulate handwaving; it's a living language.
Support the campaign for formal recognition by the British government now!

-- 

Local list tags removed with a patched pine courtesy of Eduardo Chappa

<>< Re: deemed!

_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail

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