Reference parent key name when replacing value of sub-key #1736
-
|
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. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
You can use the
Note that I also have
Edit: formatting |
Beta Was this translation helpful? Give feedback.
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