2019-05-24
发表者 Venus
shell读取json字符串已关闭评论
下载地址:https://github.com/stedolan/jq/releases
# 放到/usr/local/bin下并改名方便命令调用
[root@rootop logs]# cd /usr/local/bin/
[root@rootop logs]# wget -c https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
[root@rootop logs]# mv jq-linux64 jq
# 格式化json串,格式化后是带颜色的,很方便阅读。注意后面是跟的 点 .
[root@rootop logs]# tail -f www.rootop.org.log | jq .
{
"remote_addr": "66.249.75.44",
"remote_user": "-",
"time_local": "24/May/2019:16:08:05 +0800",
"request": "GET /pages/category/serversafe/page/2 HTTP/1.1",
"status": "200",
"body_bytes_sent": "8852",
"http_referer": "-",
"user_agent": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
}
# 获取某个key的值,注意key前面有个点:
[root@rootop logs]# tail -f www.rootop.org.log | jq .user_agent
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
# 比如要在shell脚本中调用json中的某个值:
ua=`cat json_file | jq .user_agent`
echo $ua
这样就可以调到user_agent的值了。