Skip to content

Merge lists with same key into a single list #2073

Answered by mikefarah
karamellpelle asked this question in Q&A
Discussion options

You must be logged in to vote

Sure:

yq '[.. | select(has("fruits")) | .fruits | .[]] | unique' file.yaml

Explanation:

  • .. | select(has("fruits")) finds all the nodes that has a child 'fruits'
  • .fruits gets the array
  • .[] splats the array into its component fruit strings
  • The whole thing is wrapped in an array, to make a single array of all the fruit
  • This is piped into unique to remove all the dupes.
  • Bonus, you can then pipe into sort as well

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@karamellpelle
Comment options

Answer selected by karamellpelle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants