procmail
[Top] [All Lists]

Re: catching backslashes with sed

2002-09-14 02:38:23
From: Greg Matheson <cid88201(_at_)mail(_dot_)chinmin(_dot_)edu(_dot_)tw>

I'm trying to construct a filename out of an email address, using
sed to x-out all the metacharacters, and seem to have mostly
succeeded with:

        :0 wh
        FROM=| formail -rt -x To:              

        FILE=`echo $FROM | sed \
        -e 'y/~!(_at_)#$%^&*()_-+={[}]:;"<,>?|/xxxxxxxxxxxxxxxxxxxxxxxxxxx/' \
        -e 'y/\//x/' \
        -e 'y/./x/' \
        -e 'y/\`/x/' \
        -e 'y/\\/x/' `

Except for the last sed expression to replace backslashes with 'x' :-(

Depending on the number of escaping backslashes, I get either:

sed: -e expression #5, char 4: Extra characters after command

or 

sed: -e expression #5, char 6: Unexpected End-of-file

Whether the expression is quoted or not doesn't seem to make a
difference.  


Do you have SHELL set to /bin/sh, for starters?

Anyway, you need to quote backslashes multiple times.  But the
problem can also arise from "echo", which can barf on some
magic chars.

The following, from one of my rc files, works to handle the backslash
and a couple of other chars.  Adjust for your own needs:

        :0  # save us from possible SUBJ bombs when calling sed
        * SUBJ ?? [&\/]
        {
           # we're using printf %s to replace echo, which is not
           # consistent in its handling of escape quotes

           SUBJ = `printf %s "$SUBJ" | sed 's,\\\\,\\\\\\\\,g; \
                                            s/&/\\\\&/g;       \
                                            s,/,\\\\/,g'`


This was posted to the list about two years ago as part of a
thread, so if you go find the original in the archives, you
may well unearth further answers in the related articles.

-- 
dman

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