1

我有这个结构:

foo:
  image: 123

bar:
  image: 456

baz:
  config: "my config"

我想根据子“图像”的存在打印根键(即 foo、bar、baz)

在 yq 版本 3 中,我可以这样做:

$ yq read test.yaml --printMode p "*.image" | awk -F'.' '{print $1}'
foo
bar

但我在 v4 中找不到等价物。yq + jq 解决方案是:

$ yq -j e test.yaml | jq -r 'to_entries[] | select(.value | has("image")) | [.key][]' 
foo
bar

知道如何用 yq v4 做到这一点吗?

4

1 回答 1

1

您可以使用路径运算符来获取包含标签的匹配对象的路径image

yq e '.[] | select(has("image")) | path | .[]' yaml
于 2021-04-15T14:21:15.960 回答