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!

manipulating ksh variables

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
I have a variable which looks like AbcDef123 and I want to extract the 123 portion. I can quite happily use
Code:
expr $testvar : "[^0-9]*\([0-9]*\)"
or
Code:
echo $testvar | tr -d '[A-Za-z]'
but these seem like a hammers to crack a nut. I've tried
Code:
# testvar="AbcDef123"
# echo ${testvar##[^0-9]*}
AbcDef123
# echo ${testvar##[A-Za-z]*}

#
but it's not returning what I want, as you can see. I tried
Code:
# echo ${testvar%%[^0-9]*}
AbcDef
which shows that it's interpreting [^0-9] as 09 and '^', not 'not a number' as I wanted.

Any ideas?
Thanks

Ceci n'est pas une signature
Columb Healy
 
What about this ?
echo ${testvar##*[!0-9]}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks guys - I knew you two would come through!

What confuses is that where the regex for expr is, for example, [0-9]*, for the ksh variables its *[0-9], not to mention the different methods of negation.

Ceci n'est pas une signature
Columb Healy
 
You confuse regex with shell pattern

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top