You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用yq筛选含testing的项并将CloudProvider值修改为x?

使用yq筛选并更新指定YAML条目

针对你提供的YAML结构,使用mikefarah/yq(当前主流的yq版本)可以实现需求,执行以下命令:

yq '.List1[0].List2[] |= (select(.Comment == "testing") | .CloudProvider = "x") // .' input.yaml

命令说明:

  • .List1[0].List2[]:精准定位到目标数组的每个元素
  • |=:将后续操作的结果替换原元素
  • select(.Comment == "testing") | .CloudProvider = "x":筛选出Comment值为testing的条目,将其CloudProvider字段更新为x
  • // .:对筛选不匹配的条目(即Comment不为testing),保留原内容不变

执行后输出的YAML结果如下:

List1:
  - List2:
      - team: dba
        CloudProvider: x
        Comment: testing
      - team: it
        CloudProvider: aws
        Comment: staging
      - team: qa
        CloudProvider: x
        Comment: testing
      - team: qa
        CloudProvider: x
        Comment: testing

如果需要直接修改原文件而非仅输出结果,添加-i参数即可:

yq -i '.List1[0].List2[] |= (select(.Comment == "testing") | .CloudProvider = "x") // .' input.yaml

内容的提问来源于stack exchange,提问作者surya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:37:01