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

如何用yq命令将含graph的dependencies服务追加到backup.yaml?

用yq实现YAML条目筛选与追加

需求说明

现有myfile.yaml和backup.yaml两个文件,需要从myfile.yaml的services_list中筛选出dependencies字段包含graph的服务条目,将这些条目追加到backup.yaml的services_list中。

解决命令(适用于mikefarah/yq v4+)

yq eval -i '.services_list += (load("myfile.yaml").services_list | map(select(.dependencies | contains(["graph"]))))' backup.yaml

命令解释

  • -i:启用原地编辑模式,直接修改backup.yaml,无需额外重定向操作
  • load("myfile.yaml"):加载源文件myfile.yaml的全部内容
  • .services_list | map(select(.dependencies | contains(["graph"]))):
    1. 定位到myfile.yaml的services_list数组
    2. 通过map遍历数组,配合select筛选出dependencies数组包含graph的条目
  • .services_list += ...:将筛选得到的条目数组追加到backup.yaml的services_list末尾

示例验证

原myfile.yaml内容

services_list:
  - name: service1
    location: europe
    dependencies:
      - graph
    other_dependencies:
      - mac
      - linux
  - name: service2
    location: asia
    dependencies:
      - db
      - k8s
  - name: service3
    location: asia
    dependencies:
      - db
      - graph

原backup.yaml内容

services_list:
  - name: my-service
    location: europe
    dependencies:
     - graph

执行命令后backup.yaml结果

services_list:
  - name: my-service
    location: europe
    dependencies:
     - graph
  - name: service1
    location: europe
    dependencies:
      - graph
    other_dependencies:
      - mac
      - linux
  - name: service3
    location: asia
    dependencies:
      - db
      - graph

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 00:12:11