Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract two letters from a line 1

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
IL
Hello,
I'm a new in this area.

I would like to know how to extract the last two letters from a line, e.g. the line is "ver1.00.23.45" , so I want to put "45" into a variable.

I'm using ksh. Please tell me what commands related (sed?awk?

Thanks in advance!
 
Hi

With [tt]pdksh[/tt] and [tt]bash[/tt], should work with regular [tt]ksh[/tt] too :
Code:
v="ver1.00.23.45"
echo ${v##*.}
With some tools :
Code:
echo "ver1.00.23.45" | tail -c 3
[gray]# or[/gray]
echo "ver1.00.23.45" | sed 's/.*\(..\)$/\1/'
[gray]# or[/gray]
echo "ver1.00.23.45" | gawk '{print gensub(/.*(..)$/,"\\1","")}'
[gray]# or[/gray]
echo "ver1.00.23.45" | awk '{print substr($0,length($0)-1)}'

Feherke.
 
Not forgetting...
Code:
expr "ver1.00.23.45" : '.*\(..\)'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top