grep、awk、sed命令详解2

grep、awk、sed命令详解

上一篇对grep、awk、sed命令的基本参数做了介绍,这一篇写一些例子。

1.分析access.log日志内,当天访问次数最多的10个页面,并且按降序排列。

# cat access.log|awk -F ' ' '{print $7}'|sort|uniq -c|sort -nr|head -10

 

2.获取访问最高的10个IP地址。

# cat access.log|awk -F ' ' '{print $1}'|sort|uniq -c|sort -nr|head -10

3.查看某个时间段的access.log日志(如:12月8日11:00到11:50)

# sed -n '/08\/Dec\/2018\:11\:00/,/08\/Dec\/2018\:11\:50/p' access.log

#grep -E '08/Dec/2018:11|08/Dec/2018:11' access.log

5.查看历史命令使用最多的前10个

# cat /root/.bash_history |awk  '{print $1}'|sort|uniq -c|sort -nr|head

# cat /root/.bash_history|awk '{list[$1]++;} END{for(i in list) {print ("%s\t%d\n",i,list[i]);}}'|sort -nrk 2|head

 6.awk分词

awk '{for(i=1;i<=NF;i=i+1){print $i}}' 1.txt | sort | uniq -c | sort -nr

posted @ 2018-12-08 13:43  etwits  阅读(454)  评论(0编辑  收藏  举报