Operating System - HP-UX
1752587 Members
3673 Online
108788 Solutions
New Discussion юеВ

Re: 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 13
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