yq Cheatsheet
yq: Command-Line YAML Processor
yq is a powerful command-line YAML processor that allows you to query, update, and manipulate YAML files with a syntax similar to jq
for JSON. This cheatsheet provides essential commands for efficient YAML data handling.
Basic YAML Rendering
Render the entire YAML file to standard output:
yq -r '.'
Accessing Specific Keys
Access the value associated with the microservices
key:
yq -r '.microservices'
Appending Data
Append new key-value pairs to the YAML file in place:
yq -i '.external_secrets = {"enabled": false}' test.yaml
Deleting Keys
Delete a specific key and its contents from the YAML file:
yq -i 'del(.microservice.secrets)' test.yaml
Injecting Values from Other Files
Inject data from another YAML file into a specific section:
yq -i ".microservice.env = load(\"staging.yaml\") | .microservice.env" test.yaml.yaml
Adding New Data Structures
Add a new nested data structure to the YAML file:
yq -i '.microservice.aws = {"accountId": "000000000000","region": "eu-west-1","eks": {"clusterId": "xxxxxxxxxxxxxxx"}}' test.yaml
Sorting Keys
Sort keys alphabetically under a specific section (e.g., microservice
):
yq -i '.microservice |= (to_entries | sort_by(.key) | from_entries)' test.yaml