大家好,又见面了,我是你们的朋友全栈君。
jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样
yum -y install jq
以这个json结构为例子进行解析,假设文件命名为:json.txt
[{
"name": "站长工具",
"url": "http://tool.chinaz.com",
"address": {
"city": "厦门",
"country": "中国"
},
"arrayBrowser": [{
"name": "Google",
"url": "http://www.google.com"
}, {
"name": "Baidu",
"url": "http://www.baidu.com"
}]
}, {
"name": "站长之家",
"url": "http://tool.zzhome.com",
"address": {
"city": "大连",
"country": "中国"
},
"arrayBrowser": [{
"name": "360",
"url": "http://www.so.com"
}, {
"name": "bing",
"url": "http://www.bing.com"
}]
}]
cat json.txt | jq '.[0]'
cat json.txt | jq '.[0].name'
cat json.txt | jq '.[0] | {name:.name, city:.address.city}'
cat json.txt | jq '.[] | {name:.name, city:.address.city}'
cat json.txt | jq '.[0] | {name:.name, url: .arrayBrowser[1].url}'
cat json.txt | jq '[.[] | {name:.name, city:.address.city}]'
cat json.txt | jq '[.[] | {name2:.name, city2:.address.city}]'
使用 $? 上条命令的返回值。
0:表示没有错误,其他任何数值:表示有错误。
[root@localhost testShell]# echo 111 > 1.sh
[root@localhost testShell]# rm 2.sh
rm: cannot remove ‘2.sh’: No such file or directory
[root@localhost testShell]# echo $?
1
[root@localhost testShell]# rm 1.sh
rm: remove regular file ‘1.sh’? y
[root@localhost testShell]# echo $?
0
[root@localhost testShell]# echo $(date)
Tue Aug 17 06:50:29 EDT 2021
[root@localhost testShell]# echo $$
80329
[root@localhost testShell]# echo 'ping www.baidu.com' > ping.sh
[root@localhost testShell]# tail -f ping.sh &
[1] 88991
[root@localhost testShell]# echo $!
88991
[root@localhost testShell]# kill $!
[root@localhost testShell]# echo $!
88991
[1]+ Terminated tail -f ping.sh
[root@localhost testShell]# echo $-
himBH
被`包含的内容会直接执行
echo "内容" | sed 's/\"//g'
set -x
/要替换的字符串(只找第一个)/替换成的字符串 //要替换的字符串(全部替换)/替换成的字符串
[root@localhost testShell]# url=www.baiud.com
[root@localhost testShell]# echo ${net/baidu/google}
www.google.com
[root@localhost testShell]# echo ${net//./-}
www-baidu-com
[root@localhost testShell]# echo ${net/./-}
www-baidu.com
#查找的字符串 %查找的字符串
[root@localhost testShell]# url=www.baiud.com
[root@localhost testShell]# echo ${url#*www.}
baidu.com
[root@localhost testShell]# echo ${url%*.com}
www.baidu
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152901.html原文链接:https://javaforall.cn