使用 yq 归档对象数组
Filer an array of objects with 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
...
我正在尝试列出以 1.0.
开头并在 standard-helm-chart
部分中列出的 version
号码。
到目前为止,我只使用它来获取 standard-helm-chart
:
的条目
yq eval '.entries | .standard-arup-helm-chart' index.yaml
效果很好。因此,然后我尝试只获取 version
匹配 1.0.*
的行。我阅读了 select documentation 的 yq
,但它没有说明当您查看对象而不仅仅是字符串时如何匹配。
我试过这个:
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
我有一个如下所示的 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
...
我正在尝试列出以 1.0.
开头并在 standard-helm-chart
部分中列出的 version
号码。
到目前为止,我只使用它来获取 standard-helm-chart
:
yq eval '.entries | .standard-arup-helm-chart' index.yaml
效果很好。因此,然后我尝试只获取 version
匹配 1.0.*
的行。我阅读了 select documentation 的 yq
,但它没有说明当您查看对象而不仅仅是字符串时如何匹配。
我试过这个:
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