我正在尝试使用 yq 来查找 yaml 中是否存在键值对。 下面是一个示例 yaml:
houseThings:
- houseThing:
thingType: chair
- houseThing:
thingType: table
- houseThing:
thingType: door
如果上面的 yaml 中存在thingType: door
的键值对,我只想要一个计算结果为 true(或任何值,或以零状态退出)的表达式。
到目前为止,我能做的最好的事情是通过递归遍历所有节点并检查它们的值来查找该值是否存在: 返回door
yq 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