shell 分析nginx日志

从nginx日志中获取2021:01:17日 21:30至21:50的日志内容

#sed中取的两个时间点21:31:36与21:50:08必须是日志中实际存在的,用21:30:01与21:50:01获取不到数据;

cat access.log | egrep '17/Jan/2021' | sed -n '/21:31:36/,/21:50:08/p' > t.txt
时间段查询日志时间段的情况
cat log_file | egrep '15/Aug/2015|16/Aug/2015' |awk '{print $1}'|sort|uniq -c|sort -nr|head -10
分析2015/8/15 到 2015/8/16 访问"/index.php?g=Member&m=Public&a=sendValidCode"的IP倒序排列
cat log_file | egrep '15/Aug/2015|16/Aug/2015' | awk '{if($7 == "/index.php?g=Member&m=Public&a=sendValidCode") print $1,$7}'|sort|uniq -c|sort -nr
($7~/.php/) $7里面包含.php的就输出,本句的意思是最耗时的一百个PHP页面
cat log_file |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100
列出最最耗时的页面(超过60秒的)的以及对应页面发生次数
cat access.log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100
统计网站流量(G)
cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'
统计404的连接
awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort
统计http status
cat access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' 
cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn
获取访问最高的10个IP地址 同时也可以按时间来查询
cat linewow-access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
最大请求curl
cat access.log |awk '{print $11}'|sort|uniq -c|sort -nr|head -20
访问量最大的前20个ip
awk '{print $1}' access.log |sort -n -r |uniq -c | sort -n -r | head -20
将每个IP访问的页面数进行从小到大排序:
awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n
查看某一个IP访问了哪些页面:
grep ^111.111.111.111 log_file| awk '{print $1,$7}'
去掉搜索引擎统计的页面:
awk '{print $12,$1}' log_file | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l
查看2020年8月16日14时这一个小时内有多少IP访问:
awk '{print $4,$1}' log_file | grep 16/Jan/2020:14 | awk '{print $2}'| sort | uniq | wc -l

猜你喜欢

转载自blog.csdn.net/weixin_42562106/article/details/112760610