Skip to content

How to merge and keep original anchors #1452

Answered by mikefarah
rjshrjndrn asked this question in Q&A

You must be logged in to vote

This is intentionally how merge operates, it merges all the nodes (including key nodes) - and the overriding key node does not have an anchor set.

What you need to do is merge in the leaf nodes only - I've had a think about this, and you could do it like so:

yq '(load("old_vars.yaml") | .. | select(tag != "!!map" and tag != "!!seq")) as $i ireduce(.; setpath($i | path; $i))' new_vars.yaml

Explanation:

  • We need to select all the leaf nodes of 'old_vars' - ones that aren't maps or sequences
  • Then we can use the reduce operator over this nodes, and call setpath on each one, setting the old_var value at that path.

Replies: 1 comment 1 reply

You must be logged in to vote
1 reply
@rjshrjndrn

Answer selected by rjshrjndrn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #1451 on November 27, 2022 08:27.