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!
Which implementation of yq are you using? Basically, you want to add a filtered array of
.services_listfrommyfile.yamlto the array of.services_listfrombackup.yaml. The two implementations, however, differ in how to access another file (inputvsload), and in how toselect(i.e. filter) by condition (INvsany_c).Here's an approach using kislyuk/yq:
And here's one using mikefarah/yq:
Both output: