简体   繁体   中英

How to shuffle array with yq

There is documentation explaining how to sort an array using yq https://mikefarah.gitbook.io/yq/operators/sort

But it'd like to shuffle (random sorting)

Given an example

  • a: banana
  • a: cat
  • a: apple

i'd like to sort it randomly yq 'sort_by(RANDOM)' sample.yml

Any ideas?

I'd tried the examples in the official documentation https://mikefarah.gitbook.io/yq/operators/sort

Unfortunally, yq does not provide a shuffle command.


A workaround, until your github issue is being worked on, could by to use the bash build-in shuf :

  1. Export Bash variable with yq 's output piped to shuf
  2. Use yq 's env to insert the bash variable into the desired place
export shuffled=$(yq e '.MyData' input | shuf)
yq e '.MyData |= env(shuffled)' input

Will output a pesuedo random order of the MyData array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM