yq命令安装及基础使用
安装
yq命令可以直接从Git的发布页面中下载与操作系统内核一致的版本的bin文件使用,详见此链接地址。
例如在x86架构下,直接下载使用即可。
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq && chmod +x /usr/bin/yq
简单使用
从yaml文件读取参数。
yq '.a.b[0].c' file.yaml
更新参数值。
yq -i '.a.b[0].c = "cool"' file.yaml
使用环境变量更新参数值。
NAME=mike yq -i '.a.b[0].c = strenv(NAME)' file.yaml
一次更新多个参数值。
yq -i '
.a.b[0].c = "cool" |
.x.y.z = "foobar" |
.person.name = strenv(NAME)
' file.yaml
删除键值。
yq -i 'del(.b)' file.yaml
环境变量操作
直接写入参数值。
myenv="cat meow" yq --null-input '.a = env(myenv)'
输出信息。
a: cat meow
以字符串写入参数值。
myenv="true" yq --null-input '.a = strenv(myenv)'
输出信息。
a: "true"
JSON处理
JSON转换为YAML。
yq -p=json sample.json
输出示例。
cat: meow
YAML转换为JSON。
yq -o=json '.' sample.yml
输出示例。
{
"cat": "meow"
}
JSON转CSV
{
"code": "0",
"msg": "success",
"data": {
"total": 406,
"pageNo": 1,
"pageSize": 500,
"list": [
{
"cameraIndexCode": "dde45299daad4c2fb6c44c2f3a421595",
"cameraName": "camera1",
"cameraType": 0,
"cameraTypeName": "枪机"
},
{
"cameraIndexCode": "7c6ad868131b4a60b77dfd894f2e5d1c",
"cameraName": "camera2",
"cameraType": 0,
"cameraTypeName": "枪机"
}
]
}
}
yq '.data.list[] |[.cameraIndexCode,.cameraName] | @csv' camera.json > camera.csv
JSON提取字段
{
"a":"1",
"b":"2",
"c":["3","4"]
}
yq '. |= pick( ["a","b"] )' sample.json
yq -I0 '. |= pick(["a","b","c"]) | .c = .c[0]' sample.json
JSON数组排序
echo '{"data":[{"id":"2"},{"id":"1"}]}' | yq '.data |= sort_by(.id)'
{"data": [{"id": "1"}, {"id": "2"}]}
Properties文件处理
示例文件:
# block comments come through
# comments on values appear
person.name = Mike Wazowski
# comments on array values appear
person.pets.0 = cat
person.pets.1.nested.0 = list entry
person.food.0 = pizza
进行修改操作:
yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
输出信息:
# block comments come through
# comments on values appear
person.name = Mike Wazowski
# comments on array values appear
person.pets.0 = dog
person.pets.1.nested.0 = list entry
person.food.0 = pizza
其它语法
详见官方文档。
Usage:
yq [flags]
yq [command]
Examples:
# yq defaults to 'eval' command if no command is specified. See "yq eval --help" for more examples.
yq '.stuff' < myfile.yml # outputs the data at the "stuff" node from "myfile.yml"
yq -i '.stuff = "foo"' myfile.yml # update myfile.yml in place
Available Commands:
completion Generate the autocompletion script for the specified shell
eval (default) Apply the expression to each document in each yaml file in sequence
eval-all Loads _all_ yaml documents of _all_ yaml files and runs expression once
help Help about any command
Flags:
-C, --colors force print with colors
-e, --exit-status set exit status if there are no matches or null or false is returned
-f, --front-matter string (extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact
--header-preprocess Slurp any header comments and separators before processing expression. (default true)
-h, --help help for yq
-I, --indent int sets indent level for output (default 2)
-i, --inplace update the file in place of first file given.
-p, --input-format string [yaml|y|xml|x] parse format for input. Note that json is a subset of yaml. (default "yaml")
-M, --no-colors force print with no colors
-N, --no-doc Don't print document separators (---)
-n, --null-input Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.
-o, --output-format string [yaml|y|json|j|props|p|xml|x] output format type. (default "yaml")
-P, --prettyPrint pretty print, shorthand for '... style = ""'
-s, --split-exp string print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter.
--unwrapScalar unwrap scalar, print the value with no quotes, colors or comments (default true)
-v, --verbose verbose mode
-V, --version Print version information and quit
--xml-attribute-prefix string prefix for xml attributes (default "+")
--xml-content-name string name for xml content (if no attribute name is present). (default "+content")
Use "yq [command] --help" for more information about a command.