yq-yaml-processing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseyq YAML Processing
yq YAML 处理
Expert knowledge for processing, querying, and transforming YAML data using yq v4+, the lightweight command-line YAML processor with jq-like syntax.
本内容包含使用yq v4+处理、查询和转换YAML数据的专业知识,yq是一款轻量级命令行YAML处理器,语法与jq类似。
When to Use This Skill
何时使用该技能
| Use this skill when... | Use another tool instead when... |
|---|---|
| Querying or modifying YAML files | Processing JSON only (use jq) |
| Updating Kubernetes manifests | Full Kubernetes management (use kubectl) |
| Processing GitHub Actions workflows | XML processing (use xmlstarlet) |
| Merging YAML configurations | Complex data transformations (use scripting) |
| 适用场景 | 应使用其他工具的场景 |
|---|---|
| 查询或修改YAML文件 | 仅处理JSON(使用jq) |
| 更新Kubernetes清单 | 完整Kubernetes管理(使用kubectl) |
| 处理GitHub Actions工作流 | XML处理(使用xmlstarlet) |
| 合并YAML配置 | 复杂数据转换(使用脚本) |
Core Expertise
核心能力
YAML Operations
- Query and filter YAML with path expressions
- Transform YAML structure and shape
- Multi-document YAML support
- Convert between YAML, JSON, XML, and other formats
Configuration Management
- Modify Kubernetes manifests
- Update GitHub Actions workflows
- Process Helm values files
- Manage application configurations
YAML操作
- 使用路径表达式查询和过滤YAML
- 转换YAML结构和形态
- 支持多文档YAML
- 在YAML、JSON、XML等格式之间转换
配置管理
- 修改Kubernetes清单
- 更新GitHub Actions工作流
- 处理Helm值文件
- 管理应用配置
Essential Commands
必备命令
Basic Querying
基础查询
bash
undefinedbash
undefinedPretty-print YAML
格式化打印YAML
yq '.' file.yaml
yq '.' file.yaml
Extract specific field
提取特定字段
yq '.fieldName' file.yaml
yq '.fieldName' file.yaml
Extract nested field
提取嵌套字段
yq '.spec.containers[0].name' pod.yaml
yq '.spec.containers[0].name' pod.yaml
Access array element
访问数组元素
yq '.[0]' array.yaml
yq '.items[2]' data.yaml
undefinedyq '.[0]' array.yaml
yq '.items[2]' data.yaml
undefinedReading YAML
读取YAML
bash
undefinedbash
undefinedRead specific field
读取特定字段
yq '.metadata.name' deployment.yaml
yq '.metadata.name' deployment.yaml
Read nested structure
读取嵌套结构
yq '.spec.template.spec.containers[].image' deployment.yaml
yq '.spec.template.spec.containers[].image' deployment.yaml
Read with default value
带默认值读取
yq '.optional // "default"' file.yaml
yq '.optional // "default"' file.yaml
Check if field exists
检查字段是否存在
yq 'has("fieldName")' file.yaml
undefinedyq 'has("fieldName")' file.yaml
undefinedModifying YAML (In-Place)
修改YAML(原地编辑)
bash
undefinedbash
undefinedUpdate field value
更新字段值
yq -i '.metadata.name = "new-name"' file.yaml
yq -i '.metadata.name = "new-name"' file.yaml
Update nested field
更新嵌套字段
yq -i '.spec.replicas = 3' deployment.yaml
yq -i '.spec.replicas = 3' deployment.yaml
Add new field
添加新字段
yq -i '.metadata.labels.app = "myapp"' file.yaml
yq -i '.metadata.labels.app = "myapp"' file.yaml
Delete field
删除字段
yq -i 'del(.spec.nodeSelector)' file.yaml
yq -i 'del(.spec.nodeSelector)' file.yaml
Append to array
追加到数组
yq -i '.items += {"name": "new-item"}' file.yaml
yq -i '.items += {"name": "new-item"}' file.yaml
Update all matching elements
更新所有匹配元素
yq -i '(.items[] | select(.name == "foo")).status = "active"' file.yaml
undefinedyq -i '(.items[] | select(.name == "foo")).status = "active"' file.yaml
undefinedMulti-Document YAML
多文档YAML
bash
undefinedbash
undefinedSplit multi-document YAML
拆分多文档YAML
yq -s '.metadata.name' multi-doc.yaml
yq -s '.metadata.name' multi-doc.yaml
Select specific document (0-indexed)
选择特定文档(从0开始索引)
yq 'select(document_index == 0)' multi-doc.yaml
yq 'select(document_index == 0)' multi-doc.yaml
Process all documents
处理所有文档
yq eval-all '.' multi-doc.yaml
yq eval-all '.' multi-doc.yaml
Filter documents
过滤文档
yq 'select(.kind == "Deployment")' k8s-resources.yaml
yq 'select(.kind == "Deployment")' k8s-resources.yaml
Combine YAML files
合并YAML文件
yq eval-all '. as $item ireduce ({}; . * $item)' file1.yaml file2.yaml
undefinedyq eval-all '. as $item ireduce ({}; . * $item)' file1.yaml file2.yaml
undefinedObject Operations
对象操作
bash
undefinedbash
undefinedGet all keys
获取所有键
yq 'keys' file.yaml
yq 'keys' file.yaml
Select specific fields
选择特定字段
yq '{name, version}' file.yaml
yq '{name, version}' file.yaml
Merge objects
合并对象
yq '. * {"updated": true}' file.yaml
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' base.yaml override.yaml
yq '. * {"updated": true}' file.yaml
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' base.yaml override.yaml
Deep merge
深度合并
yq eval-all '. as $item ireduce ({}; . *+ $item)' base.yaml override.yaml
undefinedyq eval-all '. as $item ireduce ({}; . *+ $item)' base.yaml override.yaml
undefinedAgentic Optimizations
智能优化命令
| Context | Command |
|---|---|
| Read field | |
| Update in-place | |
| YAML to JSON | |
| Compact JSON | |
| Filter documents | |
| Merge configs | |
| Validate YAML | |
| 场景 | 命令 |
|---|---|
| 读取字段 | |
| 原地更新 | |
| YAML转JSON | |
| 紧凑JSON输出 | |
| 过滤文档 | |
| 合并配置 | |
| 验证YAML | |
Quick Reference
快速参考
Operators
运算符
- - Access field
.field - - Iterate array/object
.[] - - Pipe (chain operations)
| - - Multiple outputs
, - - Alternative operator (default value)
// - - Merge objects (shallow)
* - - Deep merge objects
*+
- - 访问字段
.field - - 遍历数组/对象
.[] - - 管道(链式操作)
| - - 多输出
, - - 备选运算符(默认值)
// - - 合并对象(浅层)
* - - 深度合并对象
*+
Functions
函数
- ,
keys- Object keys/valuesvalues - - Array/object/string length
length - - Filter
select() - - Sort
sort_by() - - Group
group_by() - - Remove duplicates
unique - - Check field existence
has() - - Get value type
type - - Sum or concatenate
add - - Delete field
del()
- ,
keys- 对象键/值values - - 数组/对象/字符串长度
length - - 过滤
select() - - 排序
sort_by() - - 分组
group_by() - - 去重
unique - - 检查字段存在性
has() - - 获取值类型
type - - 求和或拼接
add - - 删除字段
del()
Flags
标志位
| Flag | Description |
|---|---|
| In-place edit |
| Output format (json, yaml, xml, csv, tsv, props) |
| Input format (json, yaml, xml) |
| Indent (default: 2) |
| Raw output (no quotes) |
| Null input (create from scratch) |
| Split multi-document YAML |
| 标志位 | 描述 |
|---|---|
| 原地编辑 |
| 输出格式(json, yaml, xml, csv, tsv, props) |
| 输入格式(json, yaml, xml) |
| 缩进(默认:2) |
| 原始输出(无引号) |
| 空输入(从头创建) |
| 拆分多文档YAML |
String Functions
字符串函数
- ,
split()- String/array conversionjoin() - - String/array contains
contains() - - Regex match
test() - ,
sub()- String substitutiongsub() - ,
upcase- Case conversiondowncase
For detailed examples, advanced patterns, and best practices, see REFERENCE.md.
- ,
split()- 字符串/数组转换join() - - 检查字符串/数组包含关系
contains() - - 正则匹配
test() - ,
sub()- 字符串替换gsub() - ,
upcase- 大小写转换downcase
如需详细示例、高级模式和最佳实践,请查看REFERENCE.md。
Resources
资源
- Official Documentation: https://mikefarah.gitbook.io/yq/
- GitHub Repository: https://github.com/mikefarah/yq
- Try with Docker:
docker run --rm -i mikefarah/yq '.key' <<< 'key: value' - Operators Reference: https://mikefarah.gitbook.io/yq/operators/
- 官方文档: https://mikefarah.gitbook.io/yq/
- GitHub仓库: https://github.com/mikefarah/yq
- 使用Docker尝试:
docker run --rm -i mikefarah/yq '.key' <<< 'key: value' - 运算符参考: https://mikefarah.gitbook.io/yq/operators/