yq/pkg/yqlib/doc/operators/unique.md

1.1 KiB

Unique

This is used to filter out duplicated items in an array.

{% hint style="warning" %} Note that versions prior to 4.18 require the 'eval/e' command to be specified.

yq e <exp> <file> {% endhint %}

Unique array of scalars (string/numbers)

Given a sample.yml file of:

- 1
- 2
- 3
- 2

then

yq 'unique' sample.yml

will output

- 1
- 2
- 3

Unique nulls

Unique works on the node value, so it considers different representations of nulls to be different

Given a sample.yml file of:

- ~
- null
- ~
- null

then

yq 'unique' sample.yml

will output

- ~
- null

Unique all nulls

Run against the node tag to unique all the nulls

Given a sample.yml file of:

- ~
- null
- ~
- null

then

yq 'unique_by(tag)' sample.yml

will output

- ~

Unique array object fields

Given a sample.yml file of:

- name: harry
  pet: cat
- name: billy
  pet: dog
- name: harry
  pet: dog

then

yq 'unique_by(.name)' sample.yml

will output

- name: harry
  pet: cat
- name: billy
  pet: dog