Linux - wc統計檔案行數、單詞數或位元組數

來源:互聯網
上載者:User

標籤:linux   wc   

一 wc簡介

       wc命令用來列印檔案的文本行數、單詞數、位元組數等(print the number of newlines, words, and bytes in files)。在Windows的Word中有個“字數統計”的工具,可以幫我們把選中範圍的字數、字元數統計出來。Linux下的wc命令可以實現這個 功能。使用vi開啟檔案的時候,底下的資訊也會顯示行數和位元組數。

二 常用參數

格式:wc -l <file>

列印指定檔案的文本行數。(l=小寫L)

以下參數可組合使用。

參數:-c, --bytes
列印位元組數(print the byte counts)

參數:-m, --chars
列印字元數(print the character counts)

參數:-l, --lines
列印行數(print the newline counts)

參數:-L, --max-line-length
列印最長行的長度(print the length of the longest line)

參數:-w, --words
列印單詞數(print the word counts)

 

三 使用樣本樣本 一[[email protected] ~]# wc /etc/passwd   46   66 2027 /etc/passwd行數 單詞數 位元組數 檔案名稱 

[[email protected] ~]# wc -l /etc/passwd 
46 /etc/passwd
[[email protected] ~]# wc -cmlwL /etc/passwd 
 46   66 2027 2027   74 /etc/passwd
[[email protected] ~]# wc -cmlLw /etc/passwd 
 46   66 2027 2027   74 /etc/passwd
[[email protected] ~]# wc -wcmlL /etc/passwd 
 46   66 2027 2027   74 /etc/passwd
[[email protected] ~]#

問題來了:從上面的命令列執行結果來看,wc的輸出資料的順序與的幾個參數的順序好像沒有關係?!

樣本二 用wc命令怎麼做到只列印統計數字不列印檔案名

使用管道線,這在編寫shell指令碼時特別有用。

[[email protected] ~]# wc -l /etc/passwd 
46 /etc/passwd
[[email protected] ~]# cat /etc/passwd | wc -l 
46
[[email protected] ~]#

樣本三 中文編碼的問題

執行環境是中文編碼的。

[[email protected] ~]# echo $LANG

zh_CN.UTF-8

中文編碼檔案ehr_object.gv,UTF8編碼的檔案ehr_object_utf8.gv。

[[email protected] ~]# file ehr_object.gv ehr_object_utf8.gv 
ehr_object.gv:      ISO-8859 text
ehr_object_utf8.gv: UTF-8 Unicode text
[[email protected] ~]#

[[email protected] ~]# wc ehr_object.gv ehr_object_utf8.gv 
  11  105  830 ehr_object.gv
wc: ehr_object_utf8.gv:4: 無效或不完整的多位元組字元或寬字元
  11  105  866 ehr_object_utf8.gv
  22  210 1696 總計
[[email protected] ~]#

 

樣本四 中文單詞數的計算

[[email protected] ~]# cat test1

你好中國
Linux


[[email protected] ~]# wc test1

2 2 19 test1

行數 單詞數 位元組數 檔案名稱 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.