querying-yaml

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

yq: YAML Query and Extraction Tool

yq:YAML查询与提取工具

Always invoke yq skill to extract YAML fields - do not execute bash commands directly.
Use yq to extract specific fields from YAML files without reading entire file contents, saving 80-95% context usage.
提取YAML字段时请始终调用yq技能——不要直接执行bash命令。
使用yq从YAML文件中提取特定字段,无需读取整个文件内容,可节省80-95%的上下文占用。

When to Use yq

何时使用yq

Use yq when:
  • Need specific field(s) from structured YAML file
  • File is large (>50 lines) and only need subset of data
  • Querying nested structures in YAML
  • Filtering/transforming YAML data
  • Working with docker-compose.yml, GitHub Actions workflows, K8s configs
Just use Read when:
  • File is small (<50 lines)
  • Need to understand overall structure
  • Making edits (need full context anyway)
在以下场景使用yq:
  • 需要从结构化YAML文件中获取特定字段
  • 文件较大(超过50行)且仅需部分数据
  • 查询YAML中的嵌套结构
  • 过滤/转换YAML数据
  • 处理docker-compose.yml、GitHub Actions工作流、K8s配置文件
在以下场景使用读取功能即可:
  • 文件较小(少于50行)
  • 需要了解文件整体结构
  • 需要进行编辑(无论如何都需要完整上下文)

Tool Selection

工具选择

JSON files → Use
jq
  • Common: package.json, tsconfig.json, lock files, API responses
YAML files → Use
yq
  • Common: docker-compose.yml, GitHub Actions, CI/CD configs
Both tools extract exactly what you need in one command - massive context savings.
JSON文件 → 使用
jq
  • 常见场景:package.json、tsconfig.json、锁文件、API响应
YAML文件 → 使用
yq
  • 常见场景:docker-compose.yml、GitHub Actions、CI/CD配置文件
这两款工具都能通过一条命令精准提取你需要的数据,大幅节省上下文占用。

Default Strategy

默认策略

Invoke yq skill for extracting specific fields from YAML files efficiently. Use instead of reading entire files to save 80-95% context.
Common workflow: fd skill → yq skill → other skills (fzf, sd, bat) for extraction and transformation.
调用yq技能可高效提取YAML文件中的特定字段。无需读取整个文件,可节省80-95%的上下文占用。
常见工作流:fd技能 → yq技能 → 其他技能(fzf、sd、bat)进行提取与转换。

Pipeline Combinations

流水线组合

  • yq | fzf: Interactive selection from YAML data
  • yq | sd: Transform YAML to other formats
  • yq | bat: View extracted YAML with syntax highlighting
  • yq | fzf: 从YAML数据中进行交互式选择
  • yq | sd: 将YAML转换为其他格式
  • yq | bat: 查看带语法高亮的提取后YAML内容

Skill Combinations

技能组合

For Discovery Phase

发现阶段

  • fd → yq: Find YAML config files and extract specific fields
  • ripgrep → yq: Find YAML usage patterns and extract values
  • extracting-code-structure → yq: Understand API structures before extraction
  • fd → yq: 查找YAML配置文件并提取特定字段
  • ripgrep → yq: 查找YAML使用模式并提取值
  • extracting-code-structure → yq: 提取前先了解API结构

For Analysis Phase

分析阶段

  • yq → fzf: Interactive selection from structured data
  • yq → bat: View extracted data with syntax highlighting
  • yq → tokei: Extract statistics from YAML output
  • yq → fzf: 从结构化数据中进行交互式选择
  • yq → bat: 查看带语法高亮的提取后数据
  • yq → tokei: 从YAML输出中提取统计数据

For Refactoring Phase

重构阶段

  • yq → sd: Transform YAML data to other formats
  • yq → jq: Convert YAML to JSON for different tools
  • yq → xargs: Use extracted values as command arguments
  • yq → sd: 将YAML数据转换为其他格式
  • yq → jq: 将YAML转换为JSON以适配其他工具
  • yq → xargs: 将提取的值用作命令参数

Multi-Skill Workflows

多技能工作流

  • yq → jq → sd → bat: YAML to JSON transformation with preview
  • yq → fzf → xargs: Interactive selection and execution based on YAML data
  • fd → yq → ripgrep: Find config files, extract values, search usage
  • docker-compose workflow: yq → fzf → nc (extract ports, test connectivity)
  • yq → jq → sd → bat: YAML转JSON并预览
  • yq → fzf → xargs: 基于YAML数据进行交互式选择与执行
  • fd → yq → ripgrep: 查找配置文件、提取值、搜索使用场景
  • docker-compose工作流: yq → fzf → nc(提取端口、测试连通性)

Common Integration Examples

常见集成示例

bash
undefined
bash
undefined

Get service ports and test connectivity

获取服务端口并测试连通性

yq '.services.*.ports' docker-compose.yml | sd - '[^0-9]' '' | fzf | xargs -I {} nc -zv localhost {}
yq '.services.*.ports' docker-compose.yml | sd - '[^0-9]' '' | fzf | xargs -I {} nc -zv localhost {}

Extract and filter environment variables

提取并过滤环境变量

yq '.services..environment' docker-compose.yml | sd '\s-\s*' '' | fzf
undefined
yq '.services..environment' docker-compose.yml | sd '\s-\s*' '' | fzf
undefined

Note: Choosing Between jq and yq

注意:jq与yq的选择

  • JSON files (package.json, tsconfig.json): Use
    jq
  • YAML files (docker-compose.yml, GitHub Actions): Use
    yq
  • JSON文件(package.json、tsconfig.json):使用
    jq
  • YAML文件(docker-compose.yml、GitHub Actions):使用
    yq

Quick Examples

快速示例

bash
undefined
bash
undefined

Get service ports from docker-compose

从docker-compose中获取服务端口

yq '.services.*.ports' docker-compose.yml
yq '.services.*.ports' docker-compose.yml

Get specific service configuration

获取特定服务的配置

yq '.services.web.image' docker-compose.yml
yq '.services.web.image' docker-compose.yml

List all service names

列出所有服务名称

yq '.services | keys[]' docker-compose.yml
undefined
yq '.services | keys[]' docker-compose.yml
undefined

Detailed Reference

详细参考

For comprehensive yq patterns, syntax, and examples, load yq guide when needing:
  • Complex YAML structure navigation
  • Array manipulation and filtering
  • Data transformation patterns
  • Docker Compose and Kubernetes examples
  • Integration with other tools
  • Core patterns (80% of use cases)
  • Real-world workflows (Docker Compose, GitHub Actions, Kubernetes)
  • Advanced patterns and edge case handling
  • Output formats and pipe composition
  • Best practices and integration with other tools
如需了解全面的yq模式、语法及示例,在以下场景可加载yq指南
  • 复杂YAML结构导航
  • 数组操作与过滤
  • 数据转换模式
  • Docker Compose与Kubernetes示例
  • 与其他工具的集成
  • 核心模式(覆盖80%的使用场景)
  • 真实工作流(Docker Compose、GitHub Actions、Kubernetes)
  • 高级模式与边缘情况处理
  • 输出格式与管道组合
  • 最佳实践与其他工具的集成