从文件跟随字符串中检索版本号

发布于 2025-01-18 04:30:09 字数 429 浏览 2 评论 0原文

我希望能够从字符串后面的文件 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2025-01-25 04:30:09

使用awk

awk '/version:/ { 
    pos=match($2, /[0-9]+\.[0-9]+\.[0-9]+/);
    print substr($2, pos, RLENGTH); }' sample.yaml

match()返回找到Regexp的字段中的位置,并将其设置为rlength

Use awk

awk '/version:/ { 
    pos=match($2, /[0-9]+\.[0-9]+\.[0-9]+/);
    print substr($2, pos, RLENGTH); }' sample.yaml

match() returns the position in the field where the regexp is found, and sets RLENGTH to its length.

谁的年少不轻狂 2025-01-25 04:30:09

您可以使用

sed -nE 's/.*version:[^0-9]*([0-9][0-9.]*).*/\1/p' file
grep -oP 'version:\D*\K\d[\d.]*' file

在线演示

#!/bin/bash
s='sdk: ">=0.2.4 <3.1.1"
version: ^2.3.1
sdk: flutter
version 1.0.0+1'
sed -nE 's/.*version:[^0-9]*([0-9][0-9.]*).*/\1/p' <<< "$s"
# => 2.3.1
grep -oP 'version:\D*\K\d[\d.]*' <<< "$s"
# => 2.3.1

版本:\ 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

sed -nE 's/.*version:[^0-9]*([0-9][0-9.]*).*/\1/p' file
grep -oP 'version:\D*\K\d[\d.]*' file

See an online demo:

#!/bin/bash
s='sdk: ">=0.2.4 <3.1.1"
version: ^2.3.1
sdk: flutter
version 1.0.0+1'
sed -nE 's/.*version:[^0-9]*([0-9][0-9.]*).*/\1/p' <<< "$s"
# => 2.3.1
grep -oP 'version:\D*\K\d[\d.]*' <<< "$s"
# => 2.3.1

The version:\D*\K\d[\d.]* grep regex matches version: 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 text
  • version: - 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文