Re: how to capture last 2 digits/chars of variable (154 Views)
Reply
Regular Advisor
Theresa Patrie
Posts: 105
Registered: ‎05-14-1996
Message 1 of 14 (154 Views)
Accepted Solution

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!
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 2 of 14 (154 Views)

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
Please use plain text.
Honored Contributor
Victor BERRIDGE
Posts: 2,576
Registered: ‎07-10-2000
Message 3 of 14 (154 Views)

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
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 4 of 14 (154 Views)

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
Please use plain text.
Honored Contributor
Simon Hargrave
Posts: 744
Registered: ‎06-15-1998
Message 5 of 14 (154 Views)

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?
Please use plain text.
Honored Contributor
Jean-Luc Oudart
Posts: 1,461
Registered: ‎03-10-1998
Message 6 of 14 (154 Views)

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
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 7 of 14 (154 Views)

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}
Please use plain text.
Regular Advisor
Theresa Patrie
Posts: 105
Registered: ‎05-14-1996
Message 8 of 14 (154 Views)

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!
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 9 of 14 (154 Views)

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
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 10 of 14 (154 Views)

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
Please use plain text.
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 11 of 14 (154 Views)

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...
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 12 of 14 (154 Views)

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.
Please use plain text.
Esteemed Contributor
Cem Tugrul
Posts: 964
Registered: ‎12-21-2003
Message 13 of 14 (154 Views)

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
Please use plain text.
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 14 of 14 (154 Views)

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

I prefer Rodney Hill's solution:

typeset -R2
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation