Link to home
Start Free TrialLog in
Avatar of maverickxx
maverickxx

asked on

need last three characters of the string

I need last three chaarcters of string .  I have series of of file names

somefilename1.xyz112
somefilename2.abc121
...

I need to get last three characters(112 or 121  or 131 etc  which are version numbers which I reuse later)
thanks in advance


Avatar of HRL
HRL

if this is SQL use the following:

right(stringname,3)
Avatar of maverickxx

ASKER

HRL ,
perhaps I should have been clear
unix shell scripting bash
Avatar of Harisha M G
Assuming you have this list in sample.text...

sed 's/.*\(...\)$/\1/' sample.txt

Open in new window

mgh-mgharish

can I do something like this

version = $file_name | sed 's/\(.*\).../\1/'

then use version in some other

where $file_name is various file names
Try this:

export version=`echo $file_name | sed 's/\(.*\)...$/\1/'`
echo $version

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mgh_mgharish
thanks that worked
#!/bin/bash
string=somefilename1.xyz112
echo ${string: -3}
ozo, will that work if string has multiple lines?
string='somefilename1.xyz112
somefilename2.abc121'
echo ${string: -3}
That gives only 121, not 112
112 are not the last three characters of the  string
:-)

Just wanted to know if that was possible.. The question requested for last three characters for each line..
I thought it was
"need last three characters of the  string"
file_name='somefilename1.xyz112
somefilename2.abc121'
export version=`echo $file_name | sed 's/.*\(...\)$/\1/'`
echo $version
also gives only 121
What does echo $file_name give you? It shows only one line with your code.. Asker had multiple lines in that variable
export version=`echo "$file_name" | sed 's/.*\(...\)$/\1/'`
echo $version
gives
112 121

but the asker did not specify any variables
the asker did say "I have series of of file names"
which might be a series of variables
the asker also said "I need to get last three characters(112 or 121  or 131 etc"
112 or 121 is not the same as "112 121"
In the original post, he posted two lines..