Skip to content

How to recursively using sort_by and pick #1480

Answered by mikefarah
shoekstra asked this question in Q&A

You must be logged in to vote

Sure:

yq 'with(.. | select(tag == "!!seq" and length > 0 and (.[0] | has("name"))); 
    . |= (sort_by(.name) | 
    (.[] |= pick(["name"] + keys | unique)) )
)' file.yaml

Explanation:

  1. Find all the arrays that have a first object with a name field .. | select(tag == "!!seq" and length > 0 and (.[0] | has("name")))
  2. with those, sort them by name, and apply the pick to all the children

Hope that helps!

Replies: 1 comment 1 reply

You must be logged in to vote
1 reply
@mikefarah

Answer selected by shoekstra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants
Converted from issue

This discussion was converted from issue #1479 on December 16, 2022 04:01.