bash: extract double from string
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 ?
In Bash 3.2 or greater:
str=sdk.iphoneos4.1.sdk
pattern='[0-9]+\.[0-9]+'
[[ $str =~ $pattern ]]
echo ${BASH_REMATCH[0]}
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}
assuming no other digits elsewhere
$ str=sdk.iphoneos4.0.0.1.sdk
$ echo $str | grep -Po '(\d+.*\d+)(?=\.)'
4.0.0.1
精彩评论