I asked myself this question and it turns out that I don’t need to write a script to do this, yq
has the answer already.
# sample YAML key1: - hello - world - abc key2: key21: foo key25: test key22: bar # sort the key1 array yq '.key1 |sort' /tmp/t.yaml - abc - hello - world # sort the key2 map yq '.key2 |sort_keys(.)' /tmp/t.yaml key21: foo key22: bar key25: test
More on array sorting and map sorting 🙂