logo

yq Cheatsheet

Last Updated: 2024-01-31

yq: a command-line YAML processor

yq's eval/e command is the default command and no longers needs to be specified.

Install the latest version

$ wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq && chmod +x /usr/bin/yq

Check version

$ yq -V
$ yq '.name' file.yaml

$ cat file.yaml | yq '.a.b[0].c'

Inplace Update

use -i: update file inplace

$ yq -i '.a.b[0].c = "cool"' file.yaml

Select

Select from a list, by name:

$ yq '.[] | select(.name == "Foo")' example.yml

Increment numBuckets:

$ yq '(.[] | select(.name == "Foo") | .numBuckets) |= . + 1' sample.yml

|= is the operator that updates fields relative to their own value, which is referenced as dot (.)

Sort an array by a field

$ yq '.myArray |= sort_by(.numBuckets)' sample.yml