y
y
yq
Search…
Unique
This is used to filter out duplicated items in an array.

Unique array of scalars (string/numbers)

Given a sample.yml file of:
1
- 1
2
- 2
3
- 3
4
- 2
Copied!
then
1
yq eval 'unique' sample.yml
Copied!
will output
1
- 1
2
- 2
3
- 3
Copied!

Unique nulls

Unique works on the node value, so it considers different representations of nulls to be different
Given a sample.yml file of:
1
- ~
2
- null
3
- ~
4
- null
Copied!
then
1
yq eval 'unique' sample.yml
Copied!
will output
1
- ~
2
- null
Copied!

Unique all nulls

Run against the node tag to unique all the nulls
Given a sample.yml file of:
1
- ~
2
- null
3
- ~
4
- null
Copied!
then
1
yq eval 'unique_by(tag)' sample.yml
Copied!
will output
1
- ~
Copied!

Unique array object fields

Given a sample.yml file of:
1
- name: harry
2
pet: cat
3
- name: billy
4
pet: dog
5
- name: harry
6
pet: dog
Copied!
then
1
yq eval 'unique_by(.name)' sample.yml
Copied!
will output
1
- name: harry
2
pet: cat
3
- name: billy
4
pet: dog
Copied!
Last modified 2mo ago