Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge only missing keys #1038

Closed
23tux opened this issue Dec 15, 2021 · 5 comments
Closed

Merge only missing keys #1038

23tux opened this issue Dec 15, 2021 · 5 comments

Comments

@23tux
Copy link

23tux commented Dec 15, 2021

First of all: Really appreciate the work on yq, it's a huge time safer for me!

This command from the docs merges everything from file2.yml into file1.yml

yq ea '. as $item ireduce ({}; . * $item)' -i file1.yml file2.yml
# file1.yml
key1: Key1
key2: Key2
# file2.yml
key1: Overwritten Key1
key3: Key3

And this is file1.yml after the merge:

# file1.yml
# file2.yml
key1: Overwritten Key1
key2: Key2
key3: Key3

My question is: Is it possible, to skip the overwrite for key1: Overwritten Key1 / is it possible to only merge new keys and not overwrite existing ones? The resulting yaml should look like this:

# file1.yml
# file2.yml
key1: Key1
key2: Key2
key3: Key3
@mikefarah
Copy link
Owner

@mikefarah mikefarah reopened this Dec 16, 2021
@mikefarah
Copy link
Owner

oh wait - you want the opposite right? only include new field and leave the existing 🤔

@23tux
Copy link
Author

23tux commented Dec 16, 2021

yes, I only want to include new fields and the leave the existing ones as they are. So basically, the opposite of the ? flag:

? to only merge existing fields

@mikefarah
Copy link
Owner

Will have this functionality out (and documented) in the next release (next weekend eta)

@mikefarah
Copy link
Owner

Ok I've released a new merge flag (n) https://mikefarah.gitbook.io/yq/operators/multiply-merge#merge-only-new-fields in 4.17.2 :)

yq ea '. as $item ireduce ({}; . *n $item)' examples/data1.yaml examples/data2.yaml
key1: Key1
key2: Key2
key3: Key3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants