yq/pkg/yqlib/doc/operators/anchor-and-alias-operators.md

38 lines
926 B
Markdown
Raw Normal View History

2021-11-03 04:00:58 +00:00
# Anchor and Alias Operators
2022-05-25 01:02:18 +00:00
Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalises a yaml file (dereference (or expands) aliases and remove anchor names).
2021-11-03 04:00:58 +00:00
`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag.
2023-04-15 05:24:40 +00:00
## Dereference and update a field
Use explode with multiply to dereference an object
Given a sample.yml file of:
```yaml
item_value: &item_value
value: true
thingOne:
name: item_1
!!merge <<: *item_value
thingTwo:
name: item_2
!!merge <<: *item_value
```
then
```bash
yq '.thingOne |= explode(.) * {"value": false}' sample.yml
```
will output
```yaml
item_value: &item_value
value: true
2023-04-17 07:23:48 +00:00
thingOne:
name: item_1
value: false
2023-04-15 05:24:40 +00:00
thingTwo:
name: item_2
!!merge <<: *item_value
```