I have a string which has below value
我有一个低于值的字符串
String1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
字符串1 = winos-APP-1.2.0-4.20120308.InLinuxOS.x86_64
I need to extract only version from this string which is "1.2.0-4"
我只需从这个字符串中提取“1.2.0-4”的版本
I have tried regular expression as mentioned below with sed
我用sed尝试了正则表达式,如下所述
sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,1\}[0-9][^.]\).*/\1/p'
but I am only getting result "1.2.0-", missing number after "-" which is 4. I tried correcting it but it is returning null.
但我只得到结果“1.2.0-”,在“ - ”之后缺少数字,这是4.我尝试纠正它但它返回null。
Kindly, advise
好心提醒
5 个解决方案
#1
1
how about grep:
怎么样grep:
grep -Po "(?<=-)[\d.-]*(?=.\d{8})"
#2
1
This should work, and should also account for any of the version numbers being more than one digit long.
这应该有效,并且还应该考虑长度超过一位数的任何版本号。
String1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
sed -n 's/[^0-9]*\(\([0-9]\+\.\)\{0,2\}[0-9]\+-[0-9]\+\).*/\1/p' <<< "$String1"
1.2.0-4
BTW, this will also match version strings like (which from your question is not clear if you want this behavior or not):
顺便说一下,这也会匹配版本字符串(如果你想要这个行为,你的问题就不清楚了):
1-1
1.2-1
If you want to enforce w.x.y-z
, you could use this:
如果要强制执行w.x.y-z,可以使用:
sed -n 's/[^0-9]*\(\([0-9]\+\.\)\{2\}[0-9]\+-[0-9]\+\).*/\1/p' <<< "$String1"
#3
1
sed -n 's/^.*-\([0-9.]*-[0-9]*\)\..*$/\1/p'
#4
0
Can you try this?
你能试试吗?
[^0-9]*\(\([0-9]\.\)\{0,2\}[0-9][^.]\).*
[^ 0-9] * \(\([0-9] \。\)\ {0,2 \} [0-9] [^。] \)。*
#5
0
You could also try parameter expansion:
您还可以尝试参数扩展:
string1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
string2=${string1%".${string1#*-*-*-*.}"}
version=${string2#*-*-}
printf "%s\n" "$version"
#1
1
how about grep:
怎么样grep:
grep -Po "(?<=-)[\d.-]*(?=.\d{8})"
#2
1
This should work, and should also account for any of the version numbers being more than one digit long.
这应该有效,并且还应该考虑长度超过一位数的任何版本号。
String1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
sed -n 's/[^0-9]*\(\([0-9]\+\.\)\{0,2\}[0-9]\+-[0-9]\+\).*/\1/p' <<< "$String1"
1.2.0-4
BTW, this will also match version strings like (which from your question is not clear if you want this behavior or not):
顺便说一下,这也会匹配版本字符串(如果你想要这个行为,你的问题就不清楚了):
1-1
1.2-1
If you want to enforce w.x.y-z
, you could use this:
如果要强制执行w.x.y-z,可以使用:
sed -n 's/[^0-9]*\(\([0-9]\+\.\)\{2\}[0-9]\+-[0-9]\+\).*/\1/p' <<< "$String1"
#3
1
sed -n 's/^.*-\([0-9.]*-[0-9]*\)\..*$/\1/p'
#4
0
Can you try this?
你能试试吗?
[^0-9]*\(\([0-9]\.\)\{0,2\}[0-9][^.]\).*
[^ 0-9] * \(\([0-9] \。\)\ {0,2 \} [0-9] [^。] \)。*
#5
0
You could also try parameter expansion:
您还可以尝试参数扩展:
string1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
string2=${string1%".${string1#*-*-*-*.}"}
version=${string2#*-*-}
printf "%s\n" "$version"