procmail
[Top] [All Lists]

Re: how to drop last N characters from string?

1997-12-04 13:43:10
When I suggested this code to remove the last two characters from a variable,

| |   savemetas=$SHELLMETAS
| |   SHELLMETAS # Get your asterisk out of there!
| |   VAR=`expr "$VAR" : '\(.*\)..'`
| |   SHELLMETAS=$savemetas

Jari Aalto expressed a preference for this:

|            VAR = ` awk 'BEGIN{ v = ARGV[1];                                \
|                    print substr(v,1,length(v)-2); exit }'                  \
|                    "$VAR" `

I disagree.  expr is sure to be a smaller binary than awk for procmail to
fork, and it needs much less command-line code to do this job.

Note also that one still has to diddle with SHELLMETAS to avoid a shell,
because the awk code contains brackets; thus it doesn't replace all four
lines of my suggestion but only the third (though it changes the comment
on line 2).

There are many tasks for which awk is the best tool; use it then.