yq icon indicating copy to clipboard operation
yq copied to clipboard

Add Array/String Slice

Open kenjones-cisco opened this issue 6 years ago • 3 comments

Add support for Array/String Slice

The .[10:15] syntax can be used to return a subarray of an array or substring of a string. The array returned by .[10:15] will be of length 5, containing the elements from index 10 (inclusive) to index 15 (exclusive). Either index may be negative (in which case it counts backwards from the end of the array), or omitted (in which case it refers to the start or end of the array).

Expected

a: Easy! as one two three
b:
  c: 2
  d: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  e:
    - name: fred
      value: 3
    - name: sam
      value: 4
$ yaml r sample.yaml  b.d[2:5]
- 5
- 6
- 7
- 8

kenjones-cisco avatar Sep 27 '17 00:09 kenjones-cisco

@mikefarah, possible to revisit this? Any chance to resurrect?

This is really helpful in case the input is not strictly json for jq to accept, meanwhile ok for yq. so, instead of first passing the input array data first to yq, and normalized for jq to extract the wanted slice of the array. It would be great to be able to process the data just once in yq and extract the array slice in place, especially when the amount of input data is large.

ryenus avatar Apr 14 '22 12:04 ryenus

@mikefarah was this closed unintentionally? No comments or commits were ever made.

RichardBronosky avatar Jun 28 '22 01:06 RichardBronosky

Oops - sorry yeah closed unintentionally.

mikefarah avatar Jun 28 '22 04:06 mikefarah

Fixed (finally) in 4.29.1

mikefarah avatar Oct 29 '22 07:10 mikefarah