Linux与Shell实战之Nginx日志分析

echo "题目一"

find_error_log(){
 awk '$9~/404|500/{print"";print $0}' nginx.log
}

find_error_log

echo "题目二"

find_before_and_after(){
 echo "方法一"
 grep -E -A1 -B1 "\b500\b" nginx.log
 echo "方法二"
 grep -r -1 '\b500\b' nginx.log
}

find_before_and_after



echo "题目三"

find_top_3(){
awk '{print $1}' nginx.log| sort |uniq -c| sort -nr |head -3
}

find_top_3


echo "题目四"

url_avg_time(){
awk '$7=="/topics"{print $(NF-1)}' nginx.log | awk '{sum+=$1}END{print sum/NR}'
}

url_avg_time
echo "题目五"

url_summary(){
 less nginx.log | awk '{print $7}' | sed -e 's#/topics/[0-9]*#/topics/int#' | sed 's#/2018/[a-Z0-9-]*.png#/id.png#' | sed 's#/topics/[0-9]*/replies/[0-9]/edit#/topics/int/replies/int/edit#' | sort | uniq -c | sort -nr | head -10
}


url_summary

课后作业

url_summary ()
{
    awk '{print $7}' nginx.log | sed -E 's/[A-Za-z0-9\-]+\.png/id\.png/g;s/\/[0-9]+/\/int/g;s/page\=[0-9]+/page\=int/g;s/client_id\=[A-Za-z0-9]+/client_id\=id/g' | sort | uniq -c | sort -rn | head -10
}

课后题

url_summary(){
	#sed替换
	less nginx.log | awk '{print $7}'| sed -e 's/\/topics\/[0-9]*/\/topics\/int/g'|sed -e 's/\/topics\/int\/replies\/[0-9]*\/edit/\/topics\/int\/replies\/int\/edit /g'|sed -e 's/\/_img\/uploads\/photo\/2018\/[a-z0-9-]*.png!large/\/_img\/uploads\/photo\/2018\/id.png!large/g'

	#统计top10
	sort -u -r -k 10 nginx.log | head -n 10 | awk '{print $10"\t"$1}'
}