yq
Process YAML files with Yq, a lightweight command-line tool. Convert YAML to JSON, update fields, merge files, and more.
Yq - Command-Line YAML Processor
Yq is a lightweight and portable command-line YAML processor. It allows you to query, edit, and manipulate YAML files with ease, similar to how `jq` works for JSON. This tool is invaluable for developers and DevOps engineers working with configuration files, especially in environments like Kubernetes.
Core Yq Commands and Usage
Below are common `yq` commands demonstrating its versatility. Note that some commands might be specific to older versions (v3) and newer versions (v4) might have slightly different syntax or commands.
Reading and Querying YAML
# 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 of matching elements
yq r --printMode p "a.thing*.*"
# Print the path and value of matching elements
yq r --printMode pv "a.thing*.*"
# Get the length of a list
yq r sample.yml --length animals
# Read with conditions (e.g., filter by name)
yq r sample.yml spec.metadata[name==myapp]
# Collect results into an array
yq r sample.yaml --collect a.*.animal
# Read from a specific document in a multi-document YAML file
yq r -d1 sample.yaml b.c
Validating and Comparing YAML
# Validate a YAML document
yq v valid.yaml
# Compare two YAML documents
yq compare data1.yaml data2.yaml
Writing and Modifying YAML
# Write a value to a specific path
yq w sample.yaml b.c cat
# Delete a node in place from a YAML file
yq d -i sample.yaml b.c
# Merge documents
yq merge data1.yaml data2.yaml
Advanced YAML Processing with `yq e` (Expression Mode)
# Select a specific field from a YAML file
yq e '.path.to.field' file.yaml
# Select a specific field and output in JSON format
yq e -o=json '.path.to.field' file.yaml
# Update a field's value in a YAML file (in-place)
yq e '.path.to.field = "new value"' file.yaml -i
# Delete a field from a YAML file (in-place)
yq e 'del(.path.to.field)' file.yaml -i
# Merge two YAML files using expressions
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 format
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 a filter to each document separately
yq ea '.[] | .key' file.yaml
# Checking the existence of a key in a YAML file
yq e 'has("key")' file.yaml
Shell Completion
# Generate shell completion scripts
yq shell-completion