1.awk按状态码统计数量命令:
cat /usr/local/nginx/logs/access.log | awk '{print $9}' | awk 'BEGIN{count[$1]=0}{count[$1]+=1}END{for(i in count) print(i,count[i])}'
或统计最后100条日志的情况
tail -n 1000 /usr/local/nginx/logs/access.log | awk '{print $9}' | awk 'BEGIN{count[$1]=0}{count[$1]+=1}END{for(i in count) print(i,count[i])}'
2.统计访问ip的数量,去重:
cat /usr/local/nginx/logs/access.log | awk '{print $1}' | sort -nr | uniq | wc -l
或统计最后100条日志的情况
tail -n 1000 /usr/local/nginx/logs/access.log | awk '{print $1}' | sort -nr | uniq | wc -l
批注:sort排序,uniq删除重复列,wc -l统计行数
3.统计不同ip访问nginx的次数,并由多到少排序:
cat /usr/local/nginx/logs/access.log | awk '{print $1}' | sort | uniq -c| sort -rn
批注:uniq -c每列旁边显示该行重复出现的次数,sort -rn反序
4.统计在某段时间内(1月27日15点)的访问总次数:
使用正则表达式
cat /usr/local/nginx/logs/access.log | grep -E '27/Jan/2021:15'| wc -l
统计在某段时间内(1月27日15点-16点)的访问总次数:
cat /usr/local/nginx/logs/access.log | grep -E '27/Jan/2021:1[5-6]'| wc -l
统计在某段时间内(1月27日15点-24点)的访问总次数:
cat /usr/local/nginx/logs/access.log | grep -E '27/Jan/2021:1[5-9]|27/Jan/2021:2[0-3]'| wc –l
5.查看访问最频繁的前3个ip,并列出访问ip:
cat /usr/local/nginx/logs/access.log | awk '{print $1}' | sort -n | uniq -c| sort -rn | head -n 3
6.查看访问次数大于X(这里假设是100)的ip,并列出访问次数:
cat /usr/local/nginx/logs/access.log | awk '{print $1}' | sort -n | uniq -c| sort -rn | awk '{if($1>100)print $0}'
7.查询出访问最频繁的URL:
cat /usr/local/nginx/logs/access.log | awk '{print $7}' | awk -F '?' '{print $1}' | sort -n | uniq -c | sort -nr | head -n 1
对url进行统计:
cat /usr/local/nginx/logs/access.log | awk '{print $7}' | awk -F '?' '{print $1}' | sort -n | uniq -c | sort –nr
8.统计访问成功次数(状态码为2开头的都为成功2xx):
cat /usr/local/nginx/logs/access.log | awk '{if($9 ~ /2[0-9]{2}/)print $9}' | wc -l
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)