Repository navigation
Reference parent key name when replacing value of sub-key #1736
Answered
by
mikefarah
manuelgrabowski
asked this question in
Q&A
|
I have the following input: toplevel1:
replaceme: "test1"
keepme1: "test2"
keepme2:
- "test3"
- "test4"
toplevel2:
replaceme: "test5"
keepme2:
- "test6"
- "test7"
toplevel3:
replaceme:
- "test8"
- "test9"
keepme1: "test0"and want to get to this output: toplevel1:
replaceme: "foobar in toplevel1"
keepme1: "test2"
keepme2:
- "test3"
- "test4"
toplevel2:
replaceme: "foobar in toplevel2"
keepme2:
- "test6"
- "test7"
toplevel3:
replaceme: "foobar in toplevel3"
keepme1: "test0"So replace the But I can't figure out how back-reference to the toplevel key names in the value assignment. |
Answered by
mikefarah
Jul 28, 2023
Replies: 1 comment 2 replies
|
You can use the
Note that I also have
Edit: formatting |
2 replies
Answer selected by
manuelgrabowski
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the
parentoperator to access the parent map, thenkeyto get the key value:yq '(.. | select(has("replaceme")) | .replaceme) |= ("foobar in " + (parent | key))' file.yamlNote that I also have
.. | select(has("replaceme")) | .replaceme:..recursively matches all the nodesselect(has("replaceme"))will select all the nodes that have that field definedEdit: formatting