document how to add anchors/alias to array

This commit is contained in:
Peter Romfeld 2023-07-20 21:06:54 +08:00
parent 892308bf54
commit 0e6323f88a
No known key found for this signature in database
GPG Key ID: 23DAE5C54C17180A

View File

@ -331,3 +331,48 @@ thingTwo:
!!merge <<: *item_value !!merge <<: *item_value
``` ```
## Add Anchor to array
Given a sample.yml file of:
```yaml
anchorList:
- &one first
- &two second
```
then
```bash
yq '.anchorList += "third" | (.anchorList[-1] anchor="three")' sample.yml
```
will output
```yaml
anchorList:
- &one first
- &two second
- &three third
```
## Add Alias to array
Given a sample.yml file of:
```yaml
anchorList:
- &one first
- &two second
- &three third
anchorRefList:
- *one
- *two
```
then
```bash
yq '.anchorRefList += "TMP" | (.anchorRefList[-1] anchor="three")' sample.yml
```
will output
```yaml
anchorList:
- &one first
- &two second
- &three third
anchorRefList:
- *one
- *two
- *three
```