procmail
[Top] [All Lists]

Re: how to drop last N characters from string?

1997-12-04 12:40:19
Jari Aalto explained,

| I worded my question badly, I wantod to "chop off", ie delete 2 last
| characters from the previos match, not to get last 2 ones. That will
| give me "90", but I wantes all the rest without "90".

Oh ... that is nearly impossible without forking an outside process.  The
cheapest might be expr because it doesn't need a shell to pipe echo to it
(as sed would and I believe perl would):

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

ksh or bash could do it as well:

  VAR=`echo ${VAR%??} ;` # semicolon to force invoking a shell

Now, if you know that the last two characters will be "90", that's different:

  :0
  * VAR ?? ()\/.*[^0]
  * MATCH ?? ()\/.*[^9]
  { VAR=$MATCH }

Of course, that totally screws up if the third-to-last character is a 9.

| | |     Also what's the procmail way to increment variable by N ?
| | 
| |      :0
| |      * $ $VAR^0
| |      * $ $N^0
| |      { VAR = $= }
| 
| This scoring drives me nuts. How does this actually work? If I understand
| right, the $VAR^0 test for variable existense...or does it test that it
| contains a number?

No, it tests whether the head contains a null string (which it always does,
so that condition is always true) and scores $VAR for its truth.  If $VAR
is not a number, we get an error, but if VAR was set as the score of an
earlier recipe or as the result of an arithmetic calculation, that won't be
a problem.