sed 从字符串中提取版本号(只有版本,没有其他数字)
我想实现与sed:从字符串中提取版本号中解释的相同的效果,但只考虑第一个数字序列,甚至更安全,告诉 sed 仅保留命令名称后面的数字序列,忽略其余的数字序列。
我有:
Chromium 12.0.742.112 Ubuntu 11.04
我想要:
12.0.742.112
而不是:
12.0.742.11211.04
我现在知道使用 head或 tail with sort 可以显示最大/最小数字,但我如何告诉 sed 考虑第一个序列 仅有的?
编辑:我忘了提及我正在使用 bash。
I want to achieve the same as explained in sed: Extract version number from string, but taking into consideration only the first sequence of numbers or even safer, tell sed to keep only the sequence of numbers following the name of the command, leaving out the rest.
I have:
Chromium 12.0.742.112 Ubuntu 11.04
I want:
12.0.742.112
Instead of:
12.0.742.11211.04
I now know that using head or tail with sort it is possible to display the largest/smallest number, but how can I tell sed to consider the first sequence only?
EDIT: I forgot to mention that I'm using bash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
第一个数字?怎么样:
The first number? How about:
这是一个不依赖于命令在字符串中的位置的解决方案,但它将拾取其后的任何内容:
Here's a solution that doesn't rely on the position of the command in your string, but that will pick up whatever comes after it:
使用
cut
(假设您总是想要在线的第二位信息):With
cut
(assuming you always want the second bit of info on the line):好吧,如果你使用的是 bash,并且你想要的总是在第二个字段,
well, if you are using bash, and if what you want is always on 2nd field,
下面的 perl 命令可以做到这一点:
The following perl command does it:
这将匹配/输出第一次出现的数字和点:
我向它扔了几个 narly 字符串,它保持了水:)
This will match/output the first occurance of number(s) and dot(s):
I've thrown a couple of narly strings at it and it held water :)