"yq" is likely to be the best tool for the job as it's a YAML-aware parser.

So, for this, you'd do something like:

  yq <resource.yaml '.resource_type |= {"color": "red", "weight": "1 kg", "origin": "Country x"}'

(Or, to edit the file in place do `yq -i resource.yaml "..."`)

This takes advantage of the fact that YAML is a superset of JSON (i.e. any valid JSON is automatically valid YAML, but not vice-versa). So what looks like JSON above is what YAML calls a "flow-style mapping".


Other tools that may be of interest to you, if you're going down this route are "jq" (the elder sibling of yq, but for JSON) and "jo" (outputs JSON).


On 06/11/2024 21:24, Anastasios Lisgaras wrote:
Hello community,

I have a YAML file for example `resource.yaml`.
I want to find in this file all the lines ( actually it should be only one ), with the following string: "resource_type: apple" and immediately after that line add the following lines:

```
  color: red
  weight: '1 kg'
  origin: "Country x"
```

The result should be:

```
resource_type:
  color: red
  weight: '1 kg'
  origin: "Country x"
```

Any advice using awk, sed or something else ?

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to