Can use expressions in slice #1419

This commit is contained in:
Mike Farah
2022-11-10 18:03:18 +11:00
parent af7e36bd47
commit 04847502bf
8 changed files with 189 additions and 138 deletions
+23
View File
@@ -80,3 +80,26 @@ will output
- frog
```
## Inserting into the middle of an array
using an expression to find the index
Given a sample.yml file of:
```yaml
- cat
- dog
- frog
- cow
```
then
```bash
yq '(.[] | select(. == "dog") | key + 1) as $pos | .[0:($pos)] + ["rabbit"] + .[$pos:]' sample.yml
```
will output
```yaml
- cat
- dog
- rabbit
- frog
- cow
```