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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux中统计排序的内容含有空白行的解决办法相关的知识,希望对你有一定的参考价值。

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

  废话不多说,直接上实例:

文件 sharkyun.log 的内容如下

[[email protected] ~]# 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

注意:第七行有空格哦!

我想你不会想要下面的统计结果

[[email protected] ~]# awk -F/ ‘{print $3}‘ sharkyun.log |sort |uniq -c

      1 

      1 mp3.sharkyun.com

      2 post.sharkyun.com

      3 www.sharkyun.com

[[email protected] ~]# 


所以,你应该这样

[[email protected] ~]# awk -F/ ‘NF>1{print $3}‘ sharkyun.log |sort |uniq -c

      1 mp3.sharkyun.com

      2 post.sharkyun.com

      3 www.sharkyun.com

[[email protected] ~]# 

选项说明:

  NF>1 ====>表示要符合,当以斜线分隔符时,分割的字段数要大于 1的条件,awk才处理打印此行;

  空行自然不会去处理了。


又或者这样

[[email protected] ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c

      1 mp3.sharkyun.com

      2 post.sharkyun.com

      3 www.sharkyun.com

[[email protected] ~]# 

选项说明:

  -d   ===>指定斜线为分隔字段的分界符

  -s   ===>表示不打印没有包含分界符的行  

  -f   ===>表示打印(输出)以 -d 定义的分界符后的第几个字段(这里是第3个)


也许老板会让你再搞个从大到小的排名

[[email protected] ~]# cut -d"/" -sf3 sharkyun.log |sort |uniq -c|sort -rn

      3 www.sharkyun.com

      2 post.sharkyun.com

      1 mp3.sharkyun.com

[[email protected] ~]# 


本文出自 “linux运维架构师” 博客,请务必保留此出处http://sharkyun.blog.51cto.com/10455795/1789852

以上是关于linux中统计排序的内容含有空白行的解决办法的主要内容,如果未能解决你的问题,请参考以下文章