views:

17

answers:

3

hello

I have this string in bash:

str=sdk.iphoneos4.1.sdk

and I would like to have a variable with '4.1' in it

is there any way to parse a float/double value in bash ?

+1  A: 

Assuming the surrounding text always stays the same:

str=${str#sdk.iphoneos}
str=${str%.sdk}

This is less portable (bash only), but accepts anything in place of iphoneos:

shopt -s extglob
str=${str##sdk.*([a-z])}
str=${str%.sdk}
grawity
+1  A: 

assuming no other digits elsewhere

$ str=sdk.iphoneos4.0.0.1.sdk
$ echo $str | grep -Po '(\d+.*\d+)(?=\.)'
4.0.0.1
ghostdog74
+1  A: 

In Bash 3.2 or greater:

str=sdk.iphoneos4.1.sdk
pattern='[0-9]+\.[0-9]+'
[[ $str =~ $pattern ]]
echo ${BASH_REMATCH[0]}
Dennis Williamson