We help IT Professionals succeed at work.

need last three characters of the  string

maverickxx
maverickxx asked
on
349 Views
Last Modified: 2013-12-26
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


Comment
Watch Question

HRL

Commented:
if this is SQL use the following:

right(stringname,3)

Author

Commented:
HRL ,
perhaps I should have been clear
unix shell scripting bash
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
Assuming you have this list in sample.text...

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

Open in new window

Author

Commented:
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
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
Try this:

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

Open in new window

Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
mgh_mgharish
thanks that worked
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#!/bin/bash
string=somefilename1.xyz112
echo ${string: -3}
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
ozo, will that work if string has multiple lines?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
string='somefilename1.xyz112
somefilename2.abc121'
echo ${string: -3}
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
That gives only 121, not 112
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
112 are not the last three characters of the  string
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
:-)

Just wanted to know if that was possible.. The question requested for last three characters for each line..
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
I thought it was
"need last three characters of the  string"
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
file_name='somefilename1.xyz112
somefilename2.abc121'
export version=`echo $file_name | sed 's/.*\(...\)$/\1/'`
echo $version
also gives only 121
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
What does echo $file_name give you? It shows only one line with your code.. Asker had multiple lines in that variable
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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"
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
In the original post, he posted two lines..
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.