2020-11-17 22:44:16 +00:00
Like the multiple operator in `jq` , depending on the operands, this multiply operator will do different things. Currently only objects are supported, which have the effect of merging the RHS into the LHS.
Upcoming versions of `yq` will add support for other types of multiplication (numbers, strings).
2020-11-28 00:24:16 +00:00
To concatenate when merging objects, use the `*+` form (see examples below). This will recursively merge objects, appending arrays when it encounters them.
2020-11-17 22:44:16 +00:00
Note that when merging objects, this operator returns the merged object (not the parent). This will be clearer in the examples below.
2020-11-22 02:16:54 +00:00
## Merging files
Note the use of eval-all to ensure all documents are loaded into memory.
```bash
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.yaml
```
## Merge objects together, returning merged result only
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: {field: me, fieldA: cat}
b: {field: {g: wizz}, fieldB: dog}
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '.a * .b' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
{'': null}
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge objects together, returning parent object
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: {field: me, fieldA: cat}
b: {field: {g: wizz}, fieldB: dog}
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '. * {"a":.b}' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge keeps style of LHS
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: {things: great}
2020-11-28 00:24:16 +00:00
b:
also: "me"
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '. * {"a":.b}' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge arrays
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: [1, 2, 3]
b: [3, 4, 5]
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '. * {"a":.b}' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
2020-11-27 23:41:09 +00:00
## Merge, appending arrays
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: {array: [1, 2, {animal: dog}], value: coconut}
b: {array: [3, 4, {animal: cat}], value: banana}
'': null
2020-11-27 23:41:09 +00:00
```
then
```bash
yq eval '.a *+ .b' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
{'': null}
2020-11-27 23:41:09 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge to prefix an element
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: dog
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '. * {"a": {"c": .a}}' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge with simple aliases
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: & cat {c: frog}
b: {f: *cat}
c: {g: thongs}
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '.c * .b' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
{'': null}
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge does not copy anchor names
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
a: {c: & cat frog}
b: {f: *cat}
c: {g: thongs}
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '.c * .a' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
{'': null}
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Merge with merge anchors
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
foo: & foo
a: foo_a
thing: foo_thing
c: foo_c
bar: & bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
2020-12-26 22:36:33 +00:00
!!merge < < : [ * foo , * bar ]
2020-11-17 22:44:16 +00:00
c: foobarList_c
foobar:
c: foobar_c
!!merge < < : * foo
thing: foobar_thing
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '.foobar * .foobarList' sample.yml
```
will output
```yaml
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```