Sorting keys in a yaml
June 16, 2023 -SO I needed to sort the keys in a gigantic YAML file and I thought I would just have to write some query and for loops with yq
, and it's going to be quick, BUT that turned out to be a few hours of me trying to reinvent the wheel. WHAT. THE.
Me, realized that I just did the unnecessary, when yq
provides a function that achieves my goal, in a much more simple and efficient way:
Apparently, yq
has a function to sort keys recursively in a YAML file. Like. Everything in a YAML. Except from arrays and scalars.
Just run:
yq eval '.fruits | sort_keys(.)'
And then it will sort the keys 1-step under .fruits
. Need ALL keys under that key sorted? Say no more.
yq eval '.fruits | sort_keys(..)'
Just add another dot and BOOM, all the keys got sorted. Crazy times.