'Update a specific object value under YAML based on a condition using yq

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source