Merge
Merge multiple yaml files into a one
Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will set values for any key not existing already or where the key has no value.
yq m <yaml_file> <yaml_file2> <yaml_file3>...Merge example
Given a data1.yaml file of:
a: simple
b: [1, 2]and data2.yaml file of:
a: other
c:
test: 1then
yq merge data1.yaml data2.yamlwill output:
a: simple
b: [1, 2]
c:
test: 1Updating files in-place
yq m -i data1.yaml data2.yamlwill update the data1.yaml file with the merged result.
Overwrite values
Given a data1.yaml file of:
a: simple
b: [1, 2]
d: left aloneand data2.yaml file of:
a: other
b: [3, 4]
c:
test: 1then
yq m -x data1.yaml data2.yamlwill output:
a: other
b: [3, 4]
c:
test: 1
d: left aloneNotice that 'b' does not result in the merging of the values within an array.
Append values with arrays
Given a data1.yaml file of:
a: simple
b: [1, 2]
d: hiand data2.yaml file of:
a: something
b: [3, 4]
c:
test: 2
other: truethen
yq m --arrays=append data1.yaml data2.yamlwill output:
a: simple
b: [1, 2, 3, 4]
c:
test: 2
other: true
d: hiNote that the 'b' array has concatenated the values from the second data file. Also note that other map keys are not overridden (field a).
Auto-create
By default, yq will automatically create any missing entries in the target yaml file. This can be turned off so that only matching paths are merged in. When turned off - you will most likely want to use the override flag.
Given a data1.yml file of:
a: thing
b: something elseand data2.yml file of:
b: new value
d: not in originalThen
yq m --overwrite --autocreate=false data1.yml data2.ymlwill yield
a: thing
b: new valueMultiple Documents
Merge into single document
Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected.
Given a data1.yaml file of:
something: else
---
a: simple
b: catand data3.yaml file of:
b: dogthen
yq m -x -d1 data1.yaml data3.yamlwill output:
something: else
---
a: simple
b: dogMerge into all documents
Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected.
Given a data1.yaml file of:
something: else
---
a: simple
b: catand data3.yaml file of:
b: dogthen
yq m -x -d'*' data1.yaml data3.yamlwill output:
b: dog
something: else
---
a: simple
b: dogLast updated
Was this helpful?