LinuxCommandLibrary

yq

Process YAML data from the command line

TLDR

Output a YAML file, in pretty-print format (v4+)

$ yq eval [path/to/file.yaml]
copy

Output a YAML file, in pretty-print format (v3)
$ yq read [path/to/file.yaml] [[-C|--colors]]
copy

Output the first element in a YAML file that contains only an array (v4+)
$ yq eval '.[0]' [path/to/file.yaml]
copy

Output the first element in a YAML file that contains only an array (v3)
$ yq read [path/to/file.yaml] '[0]'
copy

Set (or overwrite) a key to a value in a file (v4+)
$ yq eval '.[key] = "[value]"' [[-i|--inplace]] [path/to/file.yaml]
copy

Set (or overwrite) a key to a value in a file (v3)
$ yq write [[-i|--inplace]] [path/to/file.yaml] '[key]' '[value]'
copy

Merge two files and print to stdout (v4+)
$ yq eval-all 'select(filename == "[path/to/file1.yaml]") * select(filename == "[path/to/file2.yaml]")' [path/to/file1.yaml] [path/to/file2.yaml]
copy

Merge two files and print to stdout (v3)
$ yq merge [path/to/file1.yaml] [path/to/file2.yaml] [[-C|--colors]]
copy

SYNOPSIS

yq [options] EXPRESSION [file ...]

PARAMETERS

-i
    Edit file in place

-o FORMAT
    Output format (yaml, json, xml)

-P
    Pretty print

-C
    Force color output

-M
    Disable color output

-r
    Raw output

-e
    Exit with error if null

-n
    Null input

DESCRIPTION

yq is a lightweight command-line YAML processor, similar to jq but for YAML. It can read, update, and manipulate YAML files. yq also supports JSON and XML, making it versatile for configuration file processing.

CAVEATS

Multiple implementations exist (Mike Farah's vs Python). Syntax differs slightly from jq. Comments may be lost when modifying.

SEE ALSO

jq(1), sed(1), awk(1)

Copied to clipboard