yq/mkdocs/merge.md

176 lines
2.5 KiB
Markdown
Raw Normal View History

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.
```
2018-06-20 03:42:00 +00:00
yq m <yaml_file> <path>...
```
2018-06-20 03:42:00 +00:00
2020-01-13 05:58:11 +00:00
## Merge example
Given a data1.yaml file of:
```yaml
a: simple
b: [1, 2]
```
and data2.yaml file of:
```yaml
a: other
c:
test: 1
```
then
```bash
2017-12-17 22:11:08 +00:00
yq m data1.yaml data2.yaml
```
will output:
```yaml
a: simple
b: [1, 2]
c:
test: 1
```
2020-01-13 05:58:11 +00:00
## Updating files in-place
```bash
2017-12-17 22:11:08 +00:00
yq m -i data1.yaml data2.yaml
```
2020-01-13 05:58:11 +00:00
will update the data1.yaml file with the merged result.
2020-01-13 05:58:11 +00:00
## Overwrite values
Given a data1.yaml file of:
```yaml
a: simple
b: [1, 2]
```
and data2.yaml file of:
```yaml
a: other
c:
test: 1
```
then
```bash
2017-12-17 22:11:08 +00:00
yq m -x data1.yaml data2.yaml
```
will output:
```yaml
a: other
b: [1, 2]
c:
test: 1
```
### Overwrite values with arrays
Given a data1.yaml file of:
```yaml
a: simple
b: [1, 2]
```
and data3.yaml file of:
```yaml
2018-07-07 05:27:15 +00:00
b: [3, 4]
c:
test: 2
other: true
d: false
```
then
```bash
2017-12-17 22:11:08 +00:00
yq m -x data1.yaml data3.yaml
```
will output:
```yaml
a: simple
2018-07-07 05:27:15 +00:00
b: [3, 4]
c:
test: 2
other: true
d: false
```
2018-07-07 05:27:15 +00:00
Notice that 'b' does not result in the merging of the values within an array.
2020-01-13 05:58:11 +00:00
## Append values with arrays
2018-07-07 05:27:15 +00:00
Given a data1.yaml file of:
```yaml
a: simple
b: [1, 2]
d: hi
```
and data3.yaml file of:
```yaml
a: something
b: [3, 4]
c:
test: 2
other: true
```
then
```bash
yq m -a data1.yaml data3.yaml
```
will output:
```yaml
a: simple
b: [1, 2, 3, 4]
c:
test: 2
other: true
d: hi
```
Note that the 'b' array has concatenated the values from the second data file. Also note that other map keys are not overridden (field a).
2020-01-13 05:58:11 +00:00
## Multiple Documents
### Merge into single document
2018-06-20 03:42:00 +00:00
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:
```yaml
something: else
---
a: simple
b: cat
```
and data3.yaml file of:
```yaml
b: dog
```
then
```bash
yq m -x -d1 data1.yaml data3.yaml
```
will output:
```yaml
something: else
---
a: simple
b: dog
```
2020-01-13 05:58:11 +00:00
### Merge into all documents
2018-06-20 03:42:00 +00:00
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:
```yaml
something: else
---
a: simple
b: cat
```
and data3.yaml file of:
```yaml
b: dog
```
then
```bash
yq m -x -d'*' data1.yaml data3.yaml
```
will output:
```yaml
b: dog
something: else
---
a: simple
b: dog
```