微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 yq 过滤对象数组

如何解决使用 yq 过滤对象数组

我有一个看起来像这样的 yaml 文件

apiVersion: v1
entries:
  blue-green-toggle:
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.17
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.16
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.15
    apiVersion: v2
  istio-config:
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.4
    apiVersion: v2
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.1
    apiVersion: v2
  latest-toggle:
    name: latest-toggle
    version: 1.0.5
    apiVersion: v2
  standard-helm-chart:
    name: standard-helm-chart
    version: 1.1.10
    apiVersion: v2
    name: standard-helm-chart
    version: 2.0.1
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.34
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.10
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.9
    apiVersion: v2
generated: 2021-06-22T00:22:33.1554922Z
...

我正在尝试列出以 version 开头并列在 1.0. 部分中的 standard-helm-chart 数字。

到目前为止,我已经使用它来获取 standard-helm-chart 的条目:

yq eval '.entries | .standard-arup-helm-chart' index.yaml

效果很好。然后我尝试只访问 version 匹配 1.0.* 的行。我阅读了 yqselect documentation,但是当您查看一个对象而不仅仅是一个字符串时,它没有指明如何匹配。

我试过了:

yq eval '.entries | .standard-arup-helm-chart | select(. == "1.0.*")' index.yaml

但这失败了。我希望它能够,因为它无法将“1.0.*”的字符串与整个对象进行比较。

我也试过:

yq eval '.entries | .standard-arup-helm-chart | select(.version == "1.0.*")' index.yaml

认为这会让 yq 知道我只想查看版本。但它说Error: Cannot index array with 'version'

然后我想我需要尝试一种数组样式语法:

yq eval '.entries | .standard-arup-helm-chart | select(.[version] == "1.0.*")' index.yaml

但由于解析错误而失败。

我可以发送什么命令 yq获取所有以 1.0. 开头的版本号?

解决方法

尝试了一些错误,但这就是我最终得到的结果:

yq eval '.entries.standard-helm-chart.[] | select(.version == "1.0.*") | .version' index.yaml

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。