歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux統計行數方法及效率測試

Linux統計行數方法及效率測試

日期:2017/2/28 15:47:27   编辑:Linux教程

說明:藍色=命令名稱

淺綠=命令參數

淺藍=選項

紫色=目錄

系統環境:CentOS 6.2 i686

一、行數統計方法

1、系統命令:wc -l filename

650) this.width=650;" border=0>

2、grep方法:grep -c "" filename

650) this.width=650;" border=0>

3、sed方法:sed -n '$=' filename

650) this.width=650;" border=0>

4、awk方法:awk 'END{print NR}' filename

650) this.width=650;" border=0>

二、效率測試

生成測試所需文件:awk 'BEGIN{for(i=0;i<10000000;i++)print i}'>>test

650) this.width=650;" border=0>

說明:用上文中介紹的方法分別對test文件進行三測測試統並統計時間,最後取平均值進行比較

1、wc方法: time wc -l test

650) this.width=650;" border=0>

用時:0.234s 0.242s 0.237s

平均:0.237667s

2、grep方法:time grep -c "" test

650) this.width=650;" border=0>

用時:1.503s 1.340s 1.414s

平均:1.419000s

3、sed方法:time sed -n '$=' test

650) this.width=650;" border=0>

用時:2.268s 2.258s 2.344s

平均:2.290000s

4、awk方法:time awk 'END{print NR}' test

650) this.width=650;" border=0>

用時:1.637s 1.638s 1.649s

平均:1.641330s


總結

650) this.width=650;" border=0>

wc的效率遠高於其他方法,具體效率:wc>grep>awk>sed


補充:wc用法

語法:wc [選項] 文件

說明:該命令統計給定文件中的字節數、字數、行數。如果沒有給出文件名,則從標准輸入讀取。wc同時也給出所有指定文件的總統計數。字是由空格字符區分開的最大字符串。

該命令各選項含義如下:

  - c 統計字節數。

  - l 統計行數。

  - w 統計字數。

這些選項可以組合使用

Copyright © Linux教程網 All Rights Reserved