| name | yq |
| description | yq is a YAML processor. Use this skill when reading, modifying, or querying YAML files from the command line. |
yq - YAML Processor Skill
yq [expression] [file]
YAML/JSON output depending on specified format
Overview
yq is a lightweight and portable command-line YAML processor. It's like jq but for YAML. This skill covers the mikefarah/yq version (v4+).
Installation
brew install yq
yq --version
Basic Operations
Read a Value
yq '.theme' config.yaml
yq '.agents.sm.character' personas.yaml
yq '.missing // "default"' config.yaml
Read Entire Section
yq '.agents.sm' personas.yaml
yq -o json '.agents.sm' personas.yaml
Check if Key Exists
yq '.agents.sm | has("helper")' personas.yaml
yq '.agents.missing' personas.yaml
Modifying YAML
In-Place Edit
yq -i '.theme = "startrek"' config.yaml
yq -i '.newkey = "value"' config.yaml
yq -i 'del(.unwanted)' config.yaml
Output to Stdout (Non-Destructive)
yq '.count = 42' config.yaml
yq '.theme = "newtheme"' config.yaml > new-config.yaml
Common Patterns
Iterate Over Arrays
yq '.stories[]' sprint.yaml
yq '.stories[].title' sprint.yaml
yq '.stories[] | select(.status == "in-progress")' sprint.yaml
Iterate Over Object Keys
yq '.agents | keys' personas.yaml
yq '.agents | to_entries | .[] | .key + ": " + .value.character' personas.yaml
Merge Files
yq '. *= load("overrides.yaml")' base.yaml
yq '.agents.sm *= load("sm-overrides.yaml")' personas.yaml
Extract to Environment Variables
eval $(yq -o shell '.database' config.yaml)
yq '.theme' config.yaml | xargs -I{} echo "THEME={}"
Working with Multi-Document YAML
yq 'select(document_index == 0)' multi.yaml
yq '... | select(has("kind"))' k8s-manifests.yaml
String Operations
yq '.name | upcase' config.yaml
yq '.agents | to_entries | .[] | select(.value.style | contains("honest"))' personas.yaml
yq '"Theme is: " + .theme' config.yaml
Conditional Logic
yq 'if .count > 10 then "many" else "few" end' config.yaml
yq '.stories[] | select(.points >= 5)' sprint.yaml
yq '.optional // "default"' config.yaml
Output Formats
yq '.data' file.yaml
yq -o json '.data' file.yaml
yq -o props '.data' file.yaml
yq -o csv '.items' file.yaml
Common Use Cases in Pennyfarthing
Read Persona Config
yq '.theme' .claude/persona-config.yaml
THEME=$(yq '.theme' .claude/persona-config.yaml)
yq ".agents.sm" personas/themes/${THEME}.yaml
Query Sprint Data
yq '.stories[] | select(.status == "todo")' sprint/current-sprint.yaml
yq '[.stories[].status] | group_by(.) | map({(.[0]): length}) | add' sprint/current-sprint.yaml
Update Session Files
yq -i '.status = "in-progress"' .session/current_work.yaml
Quick Reference
| Action | Command |
|---|
| Read value | yq '.key' file.yaml |
| Read nested | yq '.parent.child' file.yaml |
| Set value | yq -i '.key = "value"' file.yaml |
| Delete key | yq -i 'del(.key)' file.yaml |
| Array length | `yq '.array |
| Filter array | yq '.array[] | select(.field == "value")' file.yaml |
| Output JSON | yq -o json '.' file.yaml |
| Merge files | yq '. *= load("other.yaml")' base.yaml |
| Default value | yq '.key // "default"' file.yaml |
Troubleshooting
"null" Output
The key doesn't exist. Use // "default" for fallback:
yq '.missing // "not found"' file.yaml
Quotes in Output
yq preserves YAML formatting. Use -r (raw) for unquoted strings:
yq -r '.theme' config.yaml
Boolean as String
YAML booleans (true/false) are parsed as booleans. Compare accordingly:
yq '.enabled == true' config.yaml
yq '.enabled == "true"' config.yaml
Reference