从文件跟随字符串中检索版本号
我希望能够从字符串后面的文件 (yaml) 中检索版本号 (nnn)。
sdk: ">=0.2.4 <3.1.1"
version: ^2.3.1
sdk: flutter
version: 1.0.0+1
sed -n '/version:/p example.yaml
返回提供“version:”的字符串的行,然后我将其通过管道传输到另一个命令 grep
以进行模式匹配版本号行:
sed -n '/version:/p example.yaml | grep -oP '(\[0-9\]+).(\[0-9\]+).(\[0-9\]+)'
我想看看是否有更有效的方法来做到这一点,如果示例中的 string = 'sdk',则当我想返回单个值时,返回值会返回两者。
I want to be able to retrieve a version number (n.n.n) from a file (yaml) that follows a string.
sdk: ">=0.2.4 <3.1.1"
version: ^2.3.1
sdk: flutter
version: 1.0.0+1
sed -n '/version:/p sample.yaml
which returns the line for the string provided "version:", I then piped this into another command grep
to pattern match the line for the version number:
sed -n '/version:/p sample.yaml | grep -oP '(\[0-9\]+).(\[0-9\]+).(\[0-9\]+)'
I would like to see if there is a more efficient way to do this, and if string = 'sdk' from the example the returned value returns both when I would like to return a single value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
awk
match()
返回找到Regexp的字段中的位置,并将其设置为rlength
。Use
awk
match()
returns the position in the field where the regexp is found, and setsRLENGTH
to its length.您可以使用
在线演示:
版本:\ d*\ d*\ k \ d [\ d [\ d [\ d 。然后省略匹配的文本(使用
\ k
),然后匹配并返回数字,然后返回一个零或更多的数字或点(带有\ d [\ d。]*
)。sed
posix erere(请参阅-e
选项启用此正则风味)模式匹配。*
- 任何文本版本:
- 固定的子弦[^0-9]*
- 零或更多非数字([0-9] [0-9。]*)*)
- 组1 (\ 1
):数字,然后零或更多数字或点。*
- 任何文本。整个比赛被组1值替换,
p
标志打印成功替换的结果(-n
抑制默认行输出)。You can use
See an online demo:
The
version:\D*\K\d[\d.]*
grep regex matchesversion:
substring, then any zero or more non-digits (with\D*
), then omits the matched text (with\K
) and then matches and returns a digit and then any zero or more digits or dots (with\d[\d.]*
).The
sed
POSIX ERE (see-E
option enabling this regex flavor) pattern matches.*
- any textversion:
- a fixed substring[^0-9]*
- zero or more non-digits([0-9][0-9.]*)
- Group 1 (\1
): a digit and then zero or more digits or dots.*
- any text.The whole match is replaced with Group 1 value, the
p
flag prints the result of the successful substitution (-n
suppresses default line output).