url_avg_time ()
{
grep --color=auto " /topics " nginx.log | awk '{total+=$(NF-1)}END{print total/NR}'
awk '/ \/topics /{count+=1;total+=$(NF-1);}END{print $(NF-1),total/count}' nginx.log
}
find_error_log(){
awk '{if($9=="404") ++sum1;if($9=="500") ++sum2}END{print "404""\t"sum1"\n""500""\t"sum2"\n""error""\t"sum1+sum2}' nginx.log
}
```bash
find_top_10(){
awk '{print $1}' nginx.log | sort | uniq -c | sort -nr | head -10
}
```bash
find_before(){
grep -B 2 " 500 " nginx.log
}
题1.思寒老师请问下, 若第一题我这种写法,正确?和您讲的awk 这种差别大吗?find_error_log
grep -nE ’ 404 | 500 ’ nginx.log | less
参考答案:awk ‘$9~/404|500/’ nginx.log
题2.find_top_10
cat nginx.log | awk ‘{print $1}’ | sort | uniq -c | sort -ur | head -10
参考答案:awk ‘{print $1}’ nginx.log |sort | uniq -c | sort -rn | head -10
题3.url_avg_time
没做出来…
参考答案:grep “/topics” nginx.log | awk ‘{total+=$(NF-1);print total/NR}’
grep “/topics” nginx.log | awk ‘{total+=$(NF-1)}END{print total/NR}’
awk ‘/ /topics/ {total+=$(NF-1)}END{print total/NR}’ nginx.log
第三题 还是不太明白
url_avg_time ()
{
awk '$7~/^\/topics$/' nginx.log | awk '{sum += $(NF-1)}; {print $(NF-1) "\t" NR "\t" sum "\t" sum/NR}'
}
find_error_log ()
{
awk '$9~/404|500/' nginx.log
}
find_before_500_after ()
{
grep --color=auto -C 1 ' 500 ' nginx.log
}
find_top_10 ()
{
awk '{print $1}' nginx.log | sort | uniq -c | sort -nr | head -10
}
1. find_error_log ()
{
awk '$9~/404|500/' nginx.log
}
##可以用awk '{print $7,$8,$9}' | head -1 来判断状态码属于哪一列;另外定义函数的时候,func(){ awk xxxxxx;}第一注意大括号和awk之间的空格,第二注意末尾的分号;
2.我的答案find_top_10 ()
{
awk '{print $1}' nginx.log | sort | uniq -c | sort -k 1nr | head -10
}
##sort -k 1 表示根据第一列进行排序
sort -k 1nr等价于sort -k 1 -nr
ps. 课上 find_top_10 ()
{
awk '{print $1}' nginx.log | sort | uniq -c | sort -nr | head -10
}
3. 我的答案
url_avg_time ()
{
awk '$7~/^\/topics$/{count+=1;total+=$(NF-1)}END{print total/count}' nginx.log
}
我的思路:
用awk正则匹配/topics,匹配网址列,也就是第七列
ps.课上所讲的两种方法:
法一:url_avg_time ()
{
grep --color=auto " /topics " nginx.log | awk '{total+=$(NF-1)}END{ print total/NR}'
}
## 加空格是为了保证只过滤 /topics 这个网址
法二:
url_avg_time ()
{
awk '/ \/topics /{count+=1;total+=$(NF-1)}END{print total/count}' nginx.log
}
find_top_10**() {
awk '{print $1} ngnix.log | sort | unique -l | head -10 }