yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Unique array of scalars (string/numbers)
  • Unique nulls
  • Unique all nulls
  • Unique array objects
  • Unique array of objects by a field
  • Unique array of arrays

Was this helpful?

  1. Operators

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
- 2

then

yq 'unique' sample.yml

will output

- 2
- 1
- 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 objects

Given a sample.yml file of:

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

then

yq 'unique' sample.yml

will output

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

Unique array of objects by a field

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

Unique array of arrays

Given a sample.yml file of:

- - cat
  - dog
- - cat
  - sheep
- - cat
  - dog

then

yq 'unique' sample.yml

will output

- - cat
  - dog
- - cat
  - sheep
PreviousUnionNextVariable Operators

Last updated 11 months ago

Was this helpful?