您的位置:首页 > 运维架构 > Linux

linux中统计排序的内容含有空白行的解决办法

2016-06-16 12:05 302 查看
linux中统计排序的内容含有空白行的解决办法
废话不多说,直接上实例:
文件 sharkyun.log 的内容如下
[root@x201t ~]# cat -n sharkyun.log
1http://www.sharkyun.com/index.html
2http://www.sharkyun.com/index.shtml
3https://post.sharkyun.com/index.html
4https://mp3.sharkyun.com/index.html
5http://www.sharkyun.com/index.jsp
6http://post.sharkyun.com/99.html
7
注意:第七行有空格哦!
我想你不会想要下面的统计结果
[root@x201t ~]# awk -F/ '{print $3}' sharkyun.log |sort |uniq -c
1
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#

所以,你应该这样
[root@x201t ~]# awk -F/ 'NF>1{print $3}' sharkyun.log |sort |uniq -c
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
选项说明:
NF>1 ====>表示当以斜线 "/" 为分隔符时,分割的字段数NF大于 1时,awk才处理打印此行;
空行自然不会去处理了。

又或者这样
[root@x201t ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c
1 mp3.sharkyun.com
2 post.sharkyun.com
3 www.sharkyun.com
[root@x201t ~]#
选项说明:
-d ===>指定斜线为分隔字段的分界符
-s ===>表示不打印没有包含分界符的行
-f ===>表示当以 -d所定义的分界符时,指定要打印的第几个字段(这里是第3个)

也许老板会让你再搞个从大到小的排名
[root@x201t ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c|sort -rn
3 www.sharkyun.com
2 post.sharkyun.com
1 mp3.sharkyun.com
[root@x201t ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux sort awk