jrz
jrz

Reputation: 1387

yq command to remove a specific field

Given a yaml file, I want to select the fields which contain "graph" under "dependencies:" and append them to another yaml file called backups.yaml.

for example: Given a yaml file called 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

and backup.yaml:

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

The result of backup.yaml should be:

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

Tried million ways and couldn't find the proper yq command. Would appreciate your help in here!

Upvotes: 0

Views: 249

Answers (1)

pmf
pmf

Reputation: 36088

Which implementation of yq are you using? Basically, you want to add a filtered array of .services_list from myfile.yaml to the array of .services_list from backup.yaml. The two implementations, however, differ in how to access another file (input vs load), and in how to select (i.e. filter) by condition (IN vs any_c).

Here's an approach using kislyuk/yq:

yq -y '.services_list += (
  input.services_list | map(select(IN(.dependencies[]; "graph")))
)' backup.yaml myfile.yaml

And here's one using mikefarah/yq:

yq '.services_list += (
  load("myfile.yaml").services_list
  | map(select(.dependencies | any_c(. == "graph"))))
' backup.yaml

Both output:

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

Upvotes: 0

Related Questions