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

如何用yq命令提取含graph键的字段并追加到result.yaml

解决YAML提取并追加指定项的yq命令

这里使用的是**mikefarah/yq v4+**版本(当前最常用的yq实现),直接用一条命令就能完成需求:

yq -i '.services_list += [load("myfile.yaml") | .services_list[] | select(has("graph"))]' result.yaml

命令拆解:

  • -i:直接原地修改result.yaml,无需额外重定向输出
  • .services_list += ...:将筛选出的条目追加到result.yaml的services_list数组末尾
  • load("myfile.yaml"):加载源文件myfile.yaml的内容
  • .services_list[] | select(has("graph")):遍历源文件的services_list数组,筛选出包含graph键的条目
  • 外层的[]:把筛选出的零散条目重新包装成数组,确保能和原数组正确合并

分步验证(可选):

如果想先确认筛选出的内容是否正确,可以先执行这条命令查看结果:

yq '.services_list[] | select(has("graph"))' myfile.yaml

输出会是:

name: service1
location: europe
graph: true
name: service3
location: asia
graph: true

执行完最终命令后,result.yaml就会变成你期望的格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 12:28:21