Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

how to capture last 2 digits/chars of variable

SOLVED
Go to solution
Theresa Patrie
Regular Advisor

how to capture last 2 digits/chars of variable

I am looking for an easy way to capture the last 2 digits/chars of a variable. I do not know the exact length of the variable...could be 5...could be 50. I am sure it is fairly simple, but I cannot get anything to work.
Thanks,
Theresa
This is my easy job!
13 REPLIES
Steven E. Protter
Exalted Contributor

Re: how to capture last 2 digits/chars of variable

substr command.

It can capture from the end of the variable.

See the man page, its simple, available in the shell script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Victor BERRIDGE
Honored Contributor

Re: how to capture last 2 digits/chars of variable

Hi Theresa,
Im no good at this, I can only think of:
octant # echo $GAGA
sdjghkjdhfgkjhdf
octant # echo $GAGA|wc -m
17
octant # echo $GAGA|cut -c 15-17
df

Im sure some gurus out there will find the more subtle approach...


All the best
Victor
H.Merijn Brand (procura
Honored Contributor

Re: how to capture last 2 digits/chars of variable

lt09:/home/merijn 110 > echo $TERM
xterm
lt09:/home/merijn 111 > perl -le'($_=$ENV{TERM})=~s/^.*?(\d{1,2})$/$1/;print'
xterm
lt09:/home/merijn 112 > echo $WINDOWID
67108881
lt09:/home/merijn 113 > perl -le'($_=$ENV{WINDOWID})=~s/^.*?(\d{1,2})$/$1/;print'
81
lt09:/home/merijn 114 > echo $G_FILENAME_ENCODING
@locale,UTF-8,ISO-8859-1,CP1252
lt09:/home/merijn 115 > perl -le'($_=$ENV{G_FILENAME_ENCODING})=~s/^.*?(\d{1,2})$/$1/;print'
52
lt09:/home/merijn 116 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Simon Hargrave
Honored Contributor

Re: how to capture last 2 digits/chars of variable

$ var="whats all this then?"
$ newvar=$(expr substr "$fish" $(expr length "$fish" - 1) 2)
$ echo $newvar
n?
Jean-Luc Oudart
Honored Contributor
Solution

Re: how to capture last 2 digits/chars of variable

another one. With AWK.

echo $var1 | awk '{print substr($0,length -1,2);}'


Regards
Jean-Luc
fiat lux
curt larson_1
Honored Contributor

Re: how to capture last 2 digits/chars of variable

if your using a posix type shell then this would be the fastest:

var="$yourVariable"
x=${var%??}
last2char=${var#$x}
Theresa Patrie
Regular Advisor

Re: how to capture last 2 digits/chars of variable

Thanks to everyone for the quick replies. I used the awk/substr command from Jean-Luc...I did not know that "length" was available for use...great tip. I looked at Curt's suggestion with a little skepticism, but it does work. Could you explain the last assignment...what does the #$x do?

Thanks Again to everyone!
Theresa
This is my easy job!
H.Merijn Brand (procura
Honored Contributor

Re: how to capture last 2 digits/chars of variable

From 'man sh-posix':

--8<---
In the parameter expansions shown previously, use of the colon in the
format results in a test for a parameter that is unset or null; omission
of the colon results in a test for a parameter that is only unset.

${#parameter} String Length. The length in characters of the
value of parameter.
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: how to capture last 2 digits/chars of variable

Drat, last part of cut-n-paste fell off. Here's the more important bit

--8<---
${parameter%word} Remove Smallest Suffix Pattern. The word is
expanded to produce a pattern. The parameter
expansion then results in parameter, with the
smallest portion of the suffix matched by the patâ
tern deleted.

${parameter%%word} Remove Largest Suffix Pattern. The word is
expanded to produce a pattern. The parameter
expansion then results in parameter, with the
largest portion of the suffix matched by the patâ
tern deleted.

${parameter#word} Remove Smallest Prefix Pattern. The word is
expanded to produce a pattern. The parameter
expansion then results in parameter, with the
smallest portion of the prefix matched by the patâ
tern deleted.

${parameter##word} Remove Largest Prefix Pattern. The word is
expanded to produce a pattern. The parameter
expansion then results in parameter, with the
largest portion of the prefix matched by the patâ
tern deleted.
-->8---

Enjoy, Have F
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: how to capture last 2 digits/chars of variable

Here's one more that uses only shell internal commands.

$ x="abcdefg"
$ typeset -R2 y=$x
$ echo $y
fg

"typeset" is an internal command to the shell, so no external processes are run.

HTH

-- Rod Hills
There be dragons...
curt larson_1
Honored Contributor

Re: how to capture last 2 digits/chars of variable

Could you explain the last assignment...what does the #$x do?

from procura's cut and paste,

x=${var%??}

This removes the smallest suffix from the variable that matches the pattern. The pattern is this case is ??. ? matches any character. So, this removes the last two characters from var.

last2char=${var#$x}

this removes the smallest prefix pattern from the variable. From above, $x is everything but the last two characters. So, this removes everything from the front of var except the last two characters. Which leaves you with just the last two characters that your looking to get.
Cem Tugrul
Esteemed Contributor

Re: how to capture last 2 digits/chars of variable

Let's try to get "anip" from the string "manipulate"

#!/bin/sh
s="manipulate"
s1=`expr substr $s 2 4`
echo $s1
-----------------------
echo "manipulate" | cut -b 2-5
------------------------
s=manipulate
echo $s | awk '{print substr($0,2,4)}'
-------------------------

Good Luck,
Our greatest duty in this life is to help others. And please, if you can't
Highlighted
Jdamian
Respected Contributor

Re: how to capture last 2 digits/chars of variable

I prefer Rodney Hill's solution:

typeset -R2