Linux command
yq 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Read a value
yq '.[key]' [file.yaml]
Read a nested value
yq '.[parent].[child]' [file.yaml]
Convert YAML to JSON
yq -o=json [file.yaml]
Convert JSON to YAML
yq -p=json [file.json]
Edit a file in place
yq -i '.[key] = "value"' [file.yaml]
Merge multiple YAML files
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' [file1.yaml] [file2.yaml]
Select items from an array
yq '.items[] | select(.name == "foo")' [file.yaml]
Count array elements
yq '.items | length' [file.yaml]
说明
yq is a lightweight command-line YAML, JSON, XML, CSV, TOML, and properties processor. It uses jq-like syntax for querying and manipulating structured data, making it easy to extract values, modify documents, and convert between formats. The tool supports reading from files or stdin, and can output to stdout or edit files in place. Expressions follow a path-based syntax similar to jq, allowing complex queries and transformations. Two implementations exist: mikefarah/yq (Go-based, standalone binary) and kislyuk/yq (Python wrapper around jq). The Go version is more commonly installed via package managers.
参数
- -i, --inplace
- Edit files in place.
- -p, --input-format _format_
- Input format: yaml, json, xml, props, csv, tsv, toml.
- -o, --output-format _format_
- Output format: yaml, json, xml, props, csv, tsv.
- -I, --indent _n_
- Set indentation level (default 2).
- -C, --colors
- Force colored output.
- -M, --no-colors
- Disable colored output.
- -N, --no-doc
- Don't print document separators.
- -e, --exit-status
- Set exit status based on expression result.
- --from-file _file_
- Load expression from a file.
- -n, --null-input
- Don't read input; useful for creating new documents.
- -s, --split-exp _expression_
- Split output into multiple files.
- -v, --verbose
- Enable verbose output.
- -h, --help
- Display help message.
- -V, --version
- Display version information.
FAQ
What is the yq command used for?
yq is a lightweight command-line YAML, JSON, XML, CSV, TOML, and properties processor. It uses jq-like syntax for querying and manipulating structured data, making it easy to extract values, modify documents, and convert between formats. The tool supports reading from files or stdin, and can output to stdout or edit files in place. Expressions follow a path-based syntax similar to jq, allowing complex queries and transformations. Two implementations exist: mikefarah/yq (Go-based, standalone binary) and kislyuk/yq (Python wrapper around jq). The Go version is more commonly installed via package managers.
How do I run a basic yq example?
Run `yq '.[key]' [file.yaml]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -i, --inplace do in yq?
Edit files in place.