y
y
yq
Search…
Unique
This is used to filter out duplicated items in an array. Note that the original order of the array is maintained.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Unique array of scalars (string/numbers)

Note that unique maintains the original order of the array.
Given a sample.yml file of:
1
- 2
2
- 1
3
- 3
4
- 2
Copied!
then
1
yq 'unique' sample.yml
Copied!
will output
1
- 2
2
- 1
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 '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 '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 'unique_by(.name)' sample.yml
Copied!
will output
1
- name: harry
2
pet: cat
3
- name: billy
4
pet: dog
Copied!