歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux中統計排序的內容含有空白行的解決辦法

linux中統計排序的內容含有空白行的解決辦法

日期:2017/3/3 11:53:35   编辑:Linux技術

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運維架構師” 博客,請務必保留此出處http://sharkyun.blog.51cto.com/10455795/1789852

Copyright © Linux教程網 All Rights Reserved