cheat.sheets:yq
# yq
# A lightweight and portable command-line YAML processor
# This is for yq v3. For version 4, use 'yq_v4'
# Read spec.template node from example.yml
yq r example.yml spec.template
# Read from stdin
cat sample.yaml | yq r - b.c
# Print the path
yq r --printMode p "a.thing*.*"
# Print the path and value
yq r --printMode pv "a.thing*.*"
# Print the length of a list
yq r sample.yml --length animals
# Read with conditions
yq r sample.yml spec.metadata[name==myapp]
# Collect results into an array
yq r sample.yaml --collect a.*.animal
# Read from the 2nd document
yq r -d1 sample.yaml b.c
# Validate a document
yq v valid.yaml
# Compare documents
yq compare data1.yaml data2.yaml
# Write b.c value with 'cat'
yq w sample.yaml b.c cat
# Delete b.c node in place from sample.yaml
yq d -i sample.yaml b.c
# Merge documents
yq merge data1.yaml data2.yaml
# Shell completion
yq shell-completion
# Select a specific field from a YAML file
yq e '.path.to.field' file.yaml
# Select a specific field from a YAML file and output in JSON
yq e -j '.path.to.field' file.yaml
# Update a field's value in a YAML file
yq e '.path.to.field = "new value"' file.yaml -i
# Delete a field from a YAML file
yq e 'del(.path.to.field)' file.yaml -i
# Merge two YAML files
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.yaml
# Convert YAML to JSON
yq e -o=json . file.yaml
# Convert JSON to YAML
yq e -o=yaml '.' file.json
# Transpose YAML into environment variables
yq e -o=props . file.yaml | sed 's/\'/\\\'/' | envsubst
# Display the structure of the YAML file (keys only)
yq e 'keys' file.yaml
# Use multi-document processing to apply filter to each document separately
yq ea '.[] | .key' file.yaml
# Checking existence of a key in a YAML file
yq e 'has("key")' file.yaml
tldr:yq
# yq
# A lightweight and portable command-line YAML processor.
# More information: <https://mikefarah.gitbook.io/yq/>.
# Output a YAML file, in pretty-print format (v4+):
yq eval path/to/file.yaml
# Output a YAML file, in pretty-print format (v3):
yq read path/to/file.yaml --colors
# Output the first element in a YAML file that contains only an array (v4+):
yq eval '.[0]' path/to/file.yaml
# Output the first element in a YAML file that contains only an array (v3):
yq read path/to/file.yaml '[0]'
# Set (or overwrite) a key to a value in a file (v4+):
yq eval '.key = "value"' --inplace path/to/file.yaml
# Set (or overwrite) a key to a value in a file (v3):
yq write --inplace path/to/file.yaml 'key' 'value'
# 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
# Merge two files and print to `stdout` (v3):
yq merge path/to/file1.yaml path/to/file2.yaml --colors
$
cheat.sh