Using https://github.com/mikefarah/yq v4
Given a sample.yaml
file like this:
spec:
chart:
name: my-chart
version: 0.0.1
---
spec:
chart:
name: something-else
version: 0.0.2
I want to update the version
value but only for the instance of .spec.chart.version
where the sibling .spec.chart.name
element == my-chart
. The result would need to output the entire yaml file so that I can edit the YAML file inline.
If I use a select like
yq e -i 'select(.spec.chart.name == "my-chart") | .spec.chart.version = "1.0.0"' sample.yaml
The second instance of .spec
has been removed:
spec:
chart:
name: my-chart
version: 1.0.0
Any advice?
|=
to perform the update on the selected object, i.e.yq e 'select(.spec.chart.name == "my-chart").spec.chart.version |= "1.0.0"' yaml
– Hakeselect
is a filter. If the chart name doesn't match, the node is dropped. What we really need here is a conditional update (not a filter). – Redstone