Marcin
Marcin

Reputation: 3262

Merge objects from a second file to each object on first file

I have following master.yaml file

root:
  - name: First
    some_value: One
  - name: Second
    some_value: Two

and update.yaml file

defaults:
  other_value: true
  one_more: Three

The expected result is:

root:
  - name: First
    some_value: One
    other_value: true
    one_more: Three
  - name: Second
    some_value: Two
    other_value: true
    one_more: Three

I tried following command:

yq eval-all '(select(fileIndex == 0).root.[] *+ select(fileIndex == 1).defaults)' './master.yaml' './update.yaml'

but the result is:

name: First
some_value: One
other_value: true
one_more: Three
name: Second
some_value: Two
other_value: true
one_more: Three

Upvotes: 1

Views: 720

Answers (1)

Inian
Inian

Reputation: 85590

You could use the load function also to merge the second file against the first. i.e.

yq '.root[] *= load("update.yaml").defaults' master.yaml

OP seems to be running yq on Windows/Powershell. There is an additional layer of quotes needed to execute command-line programs on it. See Quotes on Windows Powershell

Upvotes: 1

Related Questions