trim a range of characters from a string

trim a range of characters from a string

Post by eggo » Sun, 23 Dec 2001 03:12:59



anyone know how to do this ?
Say I have a number "2001122101", but I want to trim the last 2 characters
off, how do you do this in a shell script?
 
 
 

trim a range of characters from a string

Post by Chris F.A. Johnso » Sun, 23 Dec 2001 03:45:57



> anyone know how to do this ?
> Say I have a number "2001122101", but I want to trim the last 2 characters
> off, how do you do this in a shell script?

        var=2001122101

With a POSIX shell (bash, ksh, some flavours of sh, ...):

        echo ${var%??}

Otherwise:

        expr "$var" : '\(.*\)..'

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License