2 minute read

Looking for a lightweight tool to parse YAML files? Look no furtherโ€ฆ
I recently bumped into yq, which is command-line YAML processor that is able to traverse YAML, sort by keys, update values, export to JSON, and much moreโ€ฆ

Usage:
  yq [flags]
  yq [command]

Available Commands:
  eval             Apply expression to each document in each yaml file given in sequence
  eval-all         Loads _all_ yaml documents of _all_ yaml files and runs expression once
  help             Help about any command
  shell-completion Generate completion script

Flags:
  -C, --colors        force print with colors
  -e, --exit-status   set exit status if there are no matches or null or false is returned
  -h, --help          help for yq
  -I, --indent int    sets indent level for output (default 2)
  -i, --inplace       update the yaml file inplace of first yaml file given.
  -M, --no-colors     force print with no colors
  -N, --no-doc        Don't print document separators (---)
  -n, --null-input    Don't read input, simply evaluate the expression given. Useful for creating yaml docs from scratch.
  -P, --prettyPrint    pretty print, shorthand for '... style = ""' 
  -j, --tojson        output as json. Set indent to 0 to print json in one line.
  -v, --verbose       verbose mode
  -V, --version       Print version information and quit

Use "yq [command] --help" for more information about a command.

Installation

Windows

I suggest using a package manager like Chocolatey:

choco install yq

Linux / macOS

Use Homebrew to install on Linux or macOS:

brew install yq

Usage

Sample fruits.yml file:

fruits:
  apple: red
  pear: green
  banana: yellow

The eval or e command, followed by the path to a YAML file will print out its entire contents:

yq e fruits.yml
fruits:
  apple: red
  pear: green
  banana: yellow

Target a specific key:

yq e '.fruits.apple' fruits.yml
red

Update file

You can also manipulate files directly:

yq e '.fruits.apple = "orange"' -i fruits.yml

โš ๏ธ If youโ€™re running yq in PowerShell and run into a parsing error, you might need to escape the quotation marks with a backslash: yq e '.fruits.apple = \"orange"' -i fruits.yml

Will update fruits.yml:

fruits:
  apple: orange ๐Ÿ‘ˆ
  pear: green
  banana: yellow

Using variables

You can use variable operators and use them to update a key:

color="green" yq e '.fruits.banana = env(color)' -i fruits.yml
fruits:
  apple: red
  pear: green
  banana: green ๐Ÿ‘ˆ

Taking things further, you could save the output of a command and assign it to the environment variable:

colorArray=("purple" "blue" "cyan")
color=${colorArray[1]} yq e '.fruits.pear = env(color)' -i fruits.yml
fruits:
  apple: red
  pear: blue ๐Ÿ‘ˆ
  banana: yellow
๐Ÿ“š Found this article useful? Consider supporting this blog ๐Ÿ‘‡


Tags: , ,

Updated:

Comments