在 yaml 中查找嵌套的键值对

Find nested key-value pair in yaml

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

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

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

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

您可以将 houseThing 下的 select 语句用作

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

或者递归查找

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