regular expressions

Hello,

Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits.
I have written something like this:

result="Free 100%"
final=`expr "$result" : "Free *\(.*\)"`

It returns "100%". I still can not elimanate the "%". Please help :slight_smile:

Thank you!

just one of the many ways

var="Free 100%"
echo $var | awk '{gsub(/[[:alpha:]]|[% ]+/,"");print}'

Try...

result="Free 100%"
final=`expr "$result" : "Free *\(.*\)"%`

Another way using substitutions :

result="Free  100%  "
final=${result##*([!0-9])}  # -> '100%  '
final=${final%%\%*}         # -> '100'

Jean-Pierre.

Depends on which version/variant of ksh you have.
This works with dtksh on Solaris (and with bash, of course; doesn't work with ksh88, of course :)):

$ what /usr/dt/bin/dtksh|head -2
/usr/dt/bin/dtksh:
        Version M-12/28/93d
$ str="Free 100%"
$ echo "${str//[!0-9]}"
100
$ str="Free 123456789%"
$ echo "${str//[!0-9]}"
123456789