在 yaml 中查找嵌套的键值对



我正在尝试使用 yq 来查找 yaml 中是否存在键值对。 下面是一个示例 yaml:

houseThings:
- houseThing:
thingType: chair
- houseThing:
thingType: table
- houseThing:
thingType: door

如果上面的 yaml 中存在thingType: door的键值对,我只想要一个计算结果为 true(或任何值,或以零状态退出)的表达式。

到目前为止,我能做的最好的事情是通过递归遍历所有节点并检查它们的值来查找该值是否存在: 返回dooryq eval '.. | select(. == "door")' my_file.yaml.但我也想确保thingType是它的关键。

您可以使用houseThing下的 select 语句作为

yq e '.houseThings[].houseThing | select(.thingType == "door")' yaml

或者对它进行递归查找

yq e '.. | select(has("thingType")) | select(.thingType == "door")' yaml

最新更新