Skip to content

Reference parent key name when replacing value of sub-key #1736

Answered by mikefarah
manuelgrabowski asked this question in Q&A
Discussion options

You must be logged in to vote

You can use the parent operator to access the parent map, then key to get the key value:

yq '(.. | select(has("replaceme")) | .replaceme) |= ("foobar in " + (parent | key))' file.yaml

Note that I also have .. | select(has("replaceme")) | .replaceme:

  • .. recursively matches all the nodes
  • select(has("replaceme")) will select all the nodes that have that field defined

Edit: formatting

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mikefarah
Comment options

@manuelgrabowski
Comment options

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