常用 linux jq 命令语法整理

常用 linux jq 命令语法整理

语法json命令result str说明
.“Hello, world!”jq ‘.’“Hello, world!”
{“foo”: 42, “bar”: “less interesting data”}jq ‘.foo’42返回对象foo key 值
{“notfoo”: true, “alsonotfoo”: false}jq ‘.foo’null对象key不存在,返回null
.[][1,2,3]jq ‘.[]’1
2
3
元素遍历
{“a”: 1, “b”: 2}jq ‘.[]’1
2
key 对值遍历
.[<string>]{“foo”: 42}jq .[“foo”]42获取key 对应值
.[<value>][{“name”:“JSON”, “good”:true}, {“name”:“XML”, “good”:false}]jq ‘.[0]’{“name”:“JSON”, “good”:true}返回1下标元素
[1,2,3]jq ‘.[-2]’2返回倒数两个位置元素
.[start:end][“a”,“b”,“c”,“d”,“e”]jq ‘.[2:4]’[“c”, “d”]下标开始到下标结束的元素
“abcdefghi”jq ‘.[2:4]’“cd”下标开始到下标结束的字符
.[:end][“a”,“b”,“c”,“d”,“e”]jq ‘.[:3]’[“a”, “b”, “c”]首个元素到指定下标元素之间的元素
.[start:][“a”,“b”,“c”,“d”,“e”]jq ‘.[-2:]’[“d”, “e”]指定下标开始到未元素之间的元素
,{“foo”: 42, “bar”: “something else”, “baz”: true}jq ‘.foo, .bar’42, “something else”获取多个key值
{“user”:“stedolan”, “projects”: [“jq”, “wikiflow”]}jq ‘.user, .projects[]’“stedolan”, “jq”, “wikiflow”b取多个元素的值
[“a”,“b”,“c”,“d”,“e”]jq ‘.[4,2]’“e”, “c”取4 和 2 下标元素
|[{“name”:“JSON”, “good”:true}, {“name”:“XML”, “good”:false}]jq ‘.[] | .name’“JSON”, “XML”管道
[]{“user”:“stedolan”, “projects”: [“jq”, “wikiflow”]}jq ‘[.user, .projects[]]’[“stedolan”, “jq”, “wikiflow”]元素值组成新数组
[1, 2, 3]jq ‘[ .[] | . * 2]’[2, 4, 6]元素值组成新数组
{}[{“user”:“stedolan”, “title”: “JQ Primer”}]jq ‘.[] | {user, title}{“user”:“stedolan”, “title”: “JQ Primer”}组成对象(使用原key名)
[{“user”:“stedolan”, “title”: “JQ Primer”}]jq ‘.[] | {user1: .user, title: .title}’{“user1”:“stedolan”, “title”: “JQ Primer”}组成对象(改变key名)
+{“a”: 7}jq ‘.a + 1’8算术运算符
{“a”: [1,2], “b”: [3,4]}jq ‘.a + .b’[1,2,3,4]数组元素合并
jq ‘{a: 1} + {b: 2} + {c: 3} + {a: 42}’{“a”: 42, “b”: 2, “c”: 3}合并属性
-{“a”:3}jq ‘4 - .a’1算术运算符
[“xml”, “yaml”, “json”]jq ‘. - [“xml”, “yaml”]’[“json”]删除指定数组内元素
/5jq '10 / . ’2算术运算符
“a,b,c,d,e”jq ‘. / “,”’[“a”,“b”,“c”,“d”,“e”]字符串分隔成数组
length[1,2]jq ‘. | length’2数组元素个数
“string”jq ‘. | length’6string 的长度
{“a”:2}jq ‘. | length’1对象key个数
keys[“a”,“b”,“c”]jq ’ . | keys’[0,1,2]数组下标数组
{“abc”: 1, “abcd”: 2, “Foo”: 3}jq ’ . | keys’[“Foo”, “abc”, “abcd”]kye数组
has(key){“abc”: 1, “abcd”: 2, “Foo”: 3}jq ‘. | has(“abc”)’true是否包含key
map(x)[1,2,3]jq ‘map(.+1)’[2,3,4]元素遍历
map_values(x){“a”: 1, “b”: 2, “c”: 3}jq ‘map_values(.+1)’{“a”: 2, “b”: 3, “c”: 4}对象值遍历
del(path_expression){“foo”: 42, “bar”: 9001, “baz”: 42}jq ‘del(.foo)’{“bar”: 9001, “baz”: 42}删除key
[“foo”, “bar”, “baz”]jq ‘del(.[1, 2])’[“foo”]删除元素
getpath(PATHS){“a”:{“b”:0, “c”:1}}jq ‘[getpath([“a”,“b”], [“a”,“c”])]’[0, 1]获取指定值
setpath(PATHS; VALUE){“a”:{“b”:0}}jq ‘setpath([“a”,“b”]; 1)’{“a”: {“b”: 1}}设置指定值
jq ‘setpath([0,“a”]; 1)’[{“a”:1}]设置指定值
select(boolean_expression)[1,5,3,0,7]jq ‘map(select(. >= 2))’[5,3,7]select
[{“id”: “first”, “val”: 1}, {“id”: “second”, “val”: 2}]jq ‘.[] | select(.id == “second”)’{“id”: “second”, “val”: 2}select
arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars[[],{},1,“foo”,null,true,false]jq ‘.[] | numbers’1从数组中取指定类型值
add[“a”,“b”,“c”]jq ‘add’“abc”字符串元素拼接
[1, 2, 3]jq ‘add’6sum
any, any(condition), any(generator; condition)[true, false]jq ‘any’true有一个元素是true返回true
[false, false]jq ‘any’false
[]jq ‘any’false
[1,2,3]jq ‘any(. > 1)’true
all, all(condition), all(generator; condition)[true, false]jq ‘all’false所有元素是true返回true
range(upto), range(from;upto) range(from;upto;by)jq ‘[range(4)]’[0,1,2,3]步进
jq ‘range(2;4)’2, 3
jq ‘[range(2;4)]’[2,3]
jq ‘[range(0;10;3)]’[0,3,6,9]
jq ‘[range(0;-5;-1)]’[0,-1,-2,-3,-4]
floor3.14159jq ‘floor’3取整
sqrt9jq ‘sqrt’3开根
tonumber[1, “1”]jq ‘.[] | tonumber’1, 1转数字
tostring[1, “1”, [1]]jq ‘.[] | tostring’[“1”, “1”, “[1]”]转json 字符串
type[0, false, [], {}, null, “hello”]jq ‘map(type)’[“number”, “boolean”, “array”, “object”, “null”, “string”]查看类型
sort, sort_by(path_expression)[8,3,null,6]jq ‘sort’[null,3,6,8]order by
[{“foo”:4, “bar”:10}, {“foo”:3, “bar”:100}, {“foo”:2, “bar”:1}]jq ‘sort_by(.foo)’[{“foo”:2, “bar”:1}, {“foo”:3, “bar”:100}, {“foo”:4, “bar”:10}]
group_by(path_expression)[{“foo”:1, “bar”:10}, {“foo”:3, “bar”:100}, {“foo”:1, “bar”:1}]jq ‘group_by(.foo)’[[{“foo”:1, “bar”:10}, {“foo”:1, “bar”:1}], [{“foo”:3, “bar”:100}]]聚合函数group by
min, max, min_by(path_exp), max_by(path_exp)
unique, unique_by(path_exp)
reverse
contains(element)“foobar”jq ‘contains(“bar”)’true
[“foobar”, “foobaz”, “blarp”]jq ‘contains([“baz”, “bar”])’true
[“foobar”, “foobaz”, “blarp”]jq ‘contains([“bazzzzz”, “bar”])’false
{“foo”: 12, “bar”:[1,2,{“barp”:12, “blip”:13}]}jq ‘contains({foo: 12, bar: [{barp: 12}]})’true
{“foo”: 12, “bar”:[1,2,{“barp”:12, “blip”:13}]}jq ‘contains({foo: 12, bar: [{barp: 15}]})’false
indices(s)“a,b, cd, efg, hijk”jq ‘indices(", ")’[3,7,12]返回出现元素下标位置集合
[0,1,2,1,3,1,4]jq ‘indices(1)’[1,3,5]
[0,1,2,3,1,4,2,5,1,2,6,7]jq ‘indices([1,2])’[1,8]s可以输入为数组
index(s), rindex(s)“a,b, cd, efg, hijk”jq ‘index(", ")’3indexof
“a,b, cd, efg, hijk”jq 'rindex(", ")12lastIndexOf
inside“bar”jq ‘inside(“foobar”)’true包含元素
{“foo”: 12, “bar”: [{“barp”: 15}]}jq ‘inside({“foo”: 12, “bar”:[1,2,{“barp”:12, “blip”:13}]})’false
startswith(str)[“fo”, “foo”, “barfoo”, “foobar”, “barfoob”]jq ‘[.[] | startswith(“foo”)]’[false, true, false, true, false]开始于
endswith(str)结束于
combinations, combinations(n)[[1,2], [3, 4]]jq ‘combinations’[1, 3], [1, 4], [2, 3], [2, 4]组合笛卡尔积
[0, 1]jq ‘combinations(2)’[0, 0], [0, 1], [1, 0], [1, 1]
ltrimstr(str)
rtrimstr(str)[“fo”, “foo”, “barfoo”, “foobar”, “afoo”]jq ‘[.[] | ltrimstr(“foo”)]’[“fo”,"",“barfoo”,“bar”,“afoo”]
split(str)split
join(str)[“a”,1,2.3,true,null,false]jq ‘join(" ")’“a 1 2.3 true false”转string指定分隔符
while(cond; update)
until(cond; next)4jq ‘[.,1] | until(.[0] < 1; [.[0] - 1, .[1] * .[0]]) | .[1]’24 (4*3*2*1)
foreach
tojson
Format strings and escaping######
@text######
@json######
@html######
@uri######
@csv######
@tsv######
@sh######
@base64“This is a message”jq '@base64“VGhpcyBpcyBhIG1lc3NhZ2U=”加密
@base64d######解密
==, != >, >=, <=, < and/or/not
+=, -=, *=, /=, %=, //=
if A then elif B else C end
//{}jq ‘.foo // 42’42提供默认值
isempty(exp)
limit(n; exp)[0,1,2,3,4,5,6,7,8,9]jq ‘[limit(3;.[])]’[0,1,2]
first(expr), last(expr), nth(n; expr)10jq ‘[first(range(.)), last(range(.)), nth(./2; range(.))]’[0,9,5]
first, last, nth(n)10jq ‘[range(.)] | [first, last, nth(5)]’[0,9,5]
?忽略异常
  • 0
    点赞
  • 0
    评论
  • 3
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

心灰灰

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值