Unique
This is used to filter out duplicated items in an array. Note that the original order of the array is maintained.
Unique array of scalars (string/numbers)
Note that unique maintains the original order of the array.
Given a sample.yml file of:
- 2
- 1
- 3
- 2then
yq 'unique' sample.ymlwill output
- 2
- 1
- 3Unique nulls
Unique works on the node value, so it considers different representations of nulls to be different
Given a sample.yml file of:
- ~
- null
- ~
- nullthen
yq 'unique' sample.ymlwill output
- ~
- nullUnique all nulls
Run against the node tag to unique all the nulls
Given a sample.yml file of:
- ~
- null
- ~
- nullthen
yq 'unique_by(tag)' sample.ymlwill output
- ~Unique array objects
Given a sample.yml file of:
- name: harry
pet: cat
- name: billy
pet: dog
- name: harry
pet: catthen
yq 'unique' sample.ymlwill output
- name: harry
pet: cat
- name: billy
pet: dogUnique array of objects by a field
Given a sample.yml file of:
- name: harry
pet: cat
- name: billy
pet: dog
- name: harry
pet: dogthen
yq 'unique_by(.name)' sample.ymlwill output
- name: harry
pet: cat
- name: billy
pet: dogUnique array of arrays
Given a sample.yml file of:
- - cat
- dog
- - cat
- sheep
- - cat
- dogthen
yq 'unique' sample.ymlwill output
- - cat
- dog
- - cat
- sheepLast updated
Was this helpful?