最近在学习linux基础命令,今天突然想到ngnix中的日志,我改怎么提取字段,于是在网上一顿搜索后,终于找到我要的命令了。
日志内容如下:
{"client_ip":"192.168.254.3","local_time":"20/Feb/2020:07:42:44 +0000","request":"GET / HTTP/1.1","status":"200","body_bytes_sent":"24548","http_x_forwarded_for":"-","upstream_addr":"192.168.254.145:9004","upstream_response_time":"0.168","request_time":"0.168"}
我只想截取client_ip的值,参考了awk截取json的用法
于是命令如下:
tail -10 nginx_access.log |awk -F '"client_ip":' 'match($2,/\"([^\"]*)\"/,a){print a[0]}'
显示的结果: