jq的使用
https://blog.csdn.net/u011641885/article/details/45559031
jq [options] filter [files]
**options:**
--version:输出jq的版本信息并退出
--slurp/-s:读入整个输入流到一个数组。
--raw-input/-R:不作为JSON解析,将每一行的文本作为字符串输出到屏幕。
--null-input/ -n:不读取任何输入,过滤器运行使用null作为输入。一般用作从头构建JSON数据。
--compact-output /-c:使输出紧凑,而不是把每一个JSON对象输出在一行。
--colour-output / -C:打开颜色显示
--monochrome-output / -M:关闭颜色显示--ascii-output /-a:指定输出格式为ASCII
-raw-output /-r :如果过滤的结果是一个字符串,那么直接写到标准输出(去掉字符串的引号)
**filter:**
. : 默认输出
.foo: 输出指定属性,foo代表属性。
.[foo] :输出指定数组元素。foo代表数组下标。
.[]:输出指定数组中全部元素
, :指定多个属性作为过滤条件时,用逗号分隔
| : 将指定的数组元素中的某个属性作为过滤条件**files:**
JOSN格式文件。英文版的说明:
-c compact instead of pretty-printed output;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
实例:
#./scripts/rpc.py get_nvme_controllers -n Nvme0 | jq -r -S '.[] .trid .trtype'
PCIe
./scripts/rpc.py get_nvme_controllers -n Nvme0 | jq -s '.[]'
[
{
"name": "Nvme0",
"trid": {
"trtype": "PCIe",
"traddr": "0000:07:00.0"
}
}
]
# ./scripts/rpc.py get_nvme_controllers -n Nvme0 | jq '.[]'
{
"name": "Nvme0",
"trid": {
"trtype": "PCIe",
"traddr": "0000:07:00.0"
}
}
get_nvme_controllers 脚本返回的是下面这样:
[ { "name": "Nvme0", "trid": { "trtype": "PCIe", "traddr": "0000:07:00.0" } } ]
posted on
浙公网安备 33010602011771号