Sort YAML docs in file by key #1101
Answered
by
mikefarah
mamachanko
asked this question in
Q&A
-
I would like to sort the YAML documents in a file by a specified key.
Let's say I have this input:
How could I achieve this desired output:
❤️ |
Beta Was this translation helpful? Give feedback.
Answered by
mikefarah
Feb 10, 2022
Replies: 1 comment 1 reply
-
You can combine the documents into a single array, sort, then split back out into separate docs again. Because we want to process all the documents together (rather than one at a time), well need to use yq ea '[.] | sort_by(.name) | .[] | splitDoc' file.yml |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mamachanko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can combine the documents into a single array, sort, then split back out into separate docs again. Because we want to process all the documents together (rather than one at a time), well need to use
eval-all/ea
:yq ea '[.] | sort_by(.name) | .[] | splitDoc' file.yml