Given a YAML file, example.yaml
:
node:
sub_node:
get_this:
I'd like to get a variable containing get_this
using Mike Farah's yq
and the string sub_node
yaml="$(cat example.yaml)"
nodename=sub_node
sub_yaml= "$(echo "$yaml" | yq -r '.$nodename' )";
# also tried -> sub_yaml= "$(echo "$yaml" | yq -r '.'"$nodename" )";
Note this is a contrived example, in reality, the sub_node
string is not known in advance so I need to substitute the $nodename
string.
I can't seem to figure out how to escape out of the single quote query string required by yq
.
How might I do this?
'.$nodename'
will pass the string.$nodename
, while".$nodename"
, will pass the string.sub_node
as the argument toyq
.--arg
or--argjson
/$ARGS.named
yq
tag at U&L? Better use theYAML
tag.