From 0e6323f88ad78aacd425db9fa6db64ac6640e4a6 Mon Sep 17 00:00:00 2001 From: Peter Romfeld Date: Thu, 20 Jul 2023 21:06:54 +0800 Subject: [PATCH] document how to add anchors/alias to array --- operators/anchor-and-alias-operators.md | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/operators/anchor-and-alias-operators.md b/operators/anchor-and-alias-operators.md index 14b0bef2..4432d270 100644 --- a/operators/anchor-and-alias-operators.md +++ b/operators/anchor-and-alias-operators.md @@ -331,3 +331,48 @@ thingTwo: !!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 +```