从字符串中提取版本号 [英] Extract version number from a string

查看:122
本文介绍了从字符串中提取版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件和版本号的字符串:

I have a string with components and version numbers:

<$c$c>data-c(kuh-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(kuh)-win2

有关一个shell脚本,我需要提取二进制分频器的版本号。所以,我需要得到:

For a shell script, I need to extract the version number of the divider binary. So I need to yield:

1.4.4

什么是做到这一点的好办法?与sed的?

What would be a good way to do this? with sed?

推荐答案

的回答的,这可以工作:

grep -Po '(?<=divider-bin-)\d.\d.\d'

和更好的:

grep -Po '(?<=divider-bin-)[^;]+'

里grep分频器斌 - ,直到它找到; 字符。这样,任何 NNN.NNN。 ...。 NNN 格式就可以了(不管有多少 NN 块)。

it greps from divider-bin- until it find the ; character. This way any NNN.NNN. ... . NNN format will work (no matter how many blocks of NN).

$ echo "data-c(kuh-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(kuh)-win2" | grep -Po '(?<=divider-bin-)[^;]+'
1.4.4
$ echo "data-c(kuh-small1);divider-bin-1.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(kuh)-win2" | grep -Po '(?<=divider-bin-)[^;]+'
1.4

这篇关于从字符串中提取版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆