yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Slicing arrays
  • Slicing arrays - without the first number
  • Slicing arrays - without the second number
  • Slicing arrays - use negative numbers to count backwards from the end
  • Inserting into the middle of an array

Was this helpful?

  1. Operators

Slice Array

The slice array operator takes an array as input and returns a subarray. Like the jq equivalent, .[10:15] will return an array of length 5, starting from index 10 inclusive, up to index 15 exclusive. Negative numbers count backwards from the end of the array.

You may leave out the first or second number, which will refer to the start or end of the array 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:

- 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

Inserting into the middle of an array

using an expression to find the index

Given a sample.yml file of:

- cat
- dog
- frog
- cow

then

yq '(.[] | select(. == "dog") | key + 1) as $pos | .[0:($pos)] + ["rabbit"] + .[$pos:]' sample.yml

will output

- cat
- dog
- rabbit
- frog
- cow
PreviousShuffleNextSort

Last updated 1 year ago

Was this helpful?