温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux下jq命令怎么使用

发布时间:2022-01-22 10:33:21 来源:亿速云 阅读:288 作者:iii 栏目:开发技术

这篇文章主要介绍了Linux下jq命令怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Linux下jq命令怎么使用文章都会有所收获,下面我们一起来看看吧。

Linux下jq命令怎么使用

jq 是一个轻量级的json处理命令。可以对json数据进行分片、过滤、映射和转换。

安装。

[root@test-dhcp ~]# yum install jq

使用实例

创建
[root@test-dhcp ~]# jq -n {a:1}{
 "a": 1
}
[root@test-dhcp ~]# jq -n '{a:"test"}'{
 "a": "test"}
合并
[root@test-dhcp ~]# jq -n '{a:"test"} + {b:2}'{
 "a": "test",
 "b": 2
}
[root@test-dhcp ~]# jq -n '{a:"test"} + {b:2} + {c:"testc"}'{
 "a": "test",
 "b": 2,
 "c": "testc"}
删除
[root@test-dhcp ~]# cat test.json{"a": "test","b": 2, "c": "testc"}
[root@test-dhcp ~]# cat test.json |jq .{
 "a": "test",
 "b": 2,
 "c": "testc"}
[root@test-dhcp ~]# cat test.json |jq 'del(.b)'{
 "a": "test",
 "c": "testc"}
更新
[root@test-dhcp ~]# cat test.json{"a": "test","b": 2, "c": "testc"}
[root@test-dhcp ~]# cat test.json |jq '.b="testb"'{
 "a": "test",
 "b": "testb",
 "c": "testc"}
[root@test-dhcp ~]# cat test.json |jq '. + {d:4}'{
 "a": "test",
 "b": 2,
 "c": "testc",
 "d": 4
}
[root@test-dhcp ~]# cat test.json |jq '. + {d:4}' |jq '.d={dd:5}'{
 "a": "test",
 "b": 2,
 "c": "testc",
 "d": {
   "dd": 5
 }
}
查询
[root@test-dhcp ~]# cat test.json |jq .{
 "a": "test",
 "b": 2,
 "c": "testc",
 "d": {
   "dd": 5
 }
}
[root@test-dhcp ~]# cat test.json |jq '. + {d:4}' |jq '.d={dd:5}' |jq .d.dd5
[root@test-dhcp ~]# echo '{"a":1,"b":2}' |jq '[.a,.b]'[
 1,
 2
]
查看数据类型
[root@test-dhcp ~]# echo "{}" |jq -r typeobject
[root@test-dhcp ~]# echo '[0, false, [], {}, null, "hello"]' |jq 'map(type)'[
 "number",
 "boolean",
 "array",
 "object",
 "null",
 "string"]
查询数组中的值
[root@test-dhcp ~]# echo [1,2,3] |jq .[1]2
[root@test-dhcp ~]# echo [1,2,3] |jq .[2]3
查询数组长度
[root@test-dhcp ~]# echo [1,2,3,9] |jq '.|length'4
[root@test-dhcp ~]# echo [1,2,3] |jq '.|length'3
数组相加
[root@test-dhcp ~]# echo [1,2,3] |jq '. + [4,5,6]'[
 1,
 2,
 3,
 4,
 5,
 6
]
  1. 高级查询
[root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. >= 2))'[
 2,
 3
]
[root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. == 2))'[
 2
]
[root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. != 2))'[
 1,
 3
]
[root@test-dhcp ~]# cat test.json[
 {
   "id": "0",
   "model": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz" },
 {
   "id": "1",
   "model": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz" }
]
[root@test-dhcp ~]# cat test.json |jq .[].model"Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz""Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz"
类型转换
[root@test-dhcp ~]# echo '["a","b,c,d","e"]' |jq 'join(",")'"a,b,c,d,e"[root@test-dhcp ~]# echo '["a","b,c,d","e",1]' |jq 'join(",")'jq: error (at :1): string (",") and number (1) cannot be added
[root@test-dhcp ~]# cat test.jsonliuxin,30,male
jiaweiqiang,29,femal
[root@test-dhcp ~]# jq -R 'split(",")|{"name":.[0],"age":.[1],"sex":.[2]}' ./test.json{
 "name": "liuxin",
 "age": "30",
 "sex": "male"}
{
 "name": "jiaweiqiang",
 "age": "29",
 "sex": "femal"}
[root@test-dhcp ~]# cat test.json{
 "name": "liuxin",
 "age": "30",
 "sex": "male"}
{
 "name": "jiaweiqiang",
 "age": "29",
 "sex": "femal"}
[root@test-dhcp ~]# cat test.json |jq . -c{"name":"liuxin","age":"30","sex":"male"}
{"name":"jiaweiqiang","age":"29","sex":"femal"}

关于“Linux下jq命令怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Linux下jq命令怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI