For the complete documentation index, see llms.txt. This page is also available as Markdown.

Slice Array

The slice operator works on both arrays and strings. Like the jq equivalent, .[10:15] will return a subarray (or substring) of length 5, starting from index 10 inclusive, up to index 15 exclusive. Negative numbers count backwards from the end of the array or string.

You may leave out the first or second number, which will refer to the start or end of the array or string respectively.

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:

then

will output

Slicing arrays - without the second number

Finishes at the end of the array

Given a sample.yml file of:

then

will output

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

Given a sample.yml file of:

then

will output

Inserting into the middle of an array

using an expression to find the index

Given a sample.yml file of:

then

will output

Slicing strings

Given a sample.yml file of:

then

will output

Slicing strings - without the second number

Finishes at the end of the string

Given a sample.yml file of:

then

will output

Slicing strings - without the first number

Starts from the start of the string

Given a sample.yml file of:

then

will output

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

Negative indices count from the end of the string

Given a sample.yml file of:

then

will output

Slicing strings - Unicode

Indices are rune-based, so multi-byte characters are handled correctly

Given a sample.yml file of:

then

will output

Last updated