yq/pkg/yqlib/doc/operators/slice-array.md
2022-10-28 20:33:50 +11:00

823 B

Slicing arrays

Given a sample.yml file of:

- cat
- dog
- frog
- cow

then

yq '.[1:3]' sample.yml

will output

- dog
- frog

Slicing arrays - without the first number

Starts from the start of the array

Given a sample.yml file of:

- cat
- dog
- frog
- cow

then

yq '.[:2]' sample.yml

will output

- cat
- dog

Slicing arrays - without the second number

Finishes at the end of the array

Given a sample.yml file of:

- cat
- dog
- frog
- cow

then

yq '.[2:]' sample.yml

will output

- frog
- cow

Slicing arrays - use negative numbers to count backwards from the end

Given a sample.yml file of:

- cat
- dog
- frog
- cow

then

yq '.[1:-1]' sample.yml

will output

- dog
- frog