wc(Word Count)的功能是統計指定檔案的位元組數、字數與行數,並把結果輸出。
wc [選項] [檔案]......
若不指定檔名,或檔名為 -,wc 會從標準輸入讀取資料,因此常與管線搭配使用。
-c / --bytes:印出位元組數
-m / --chars:印出字元數(不可與 -c 併用)
-l / --lines:印出行數
-L / --max-line-length:印出最長一行的長度
-w / --words:印出單字數,單字定義為由空白、跳格或換行分隔的字串
[root@localhost grepDir]# cat patfile MenAngel sunjimeng [root@localhost grepDir]# cat patfile|wc -l 2統計位元組數
[root@localhost grepDir]# cat patfile|wc -c 19這裡換行算 2 個字元,加上 8 加 9,合計 19。
統計字元數
[root@localhost grepDir]# cat patfile|wc -m 19-c 與 -m 的差別
純 ASCII 內容時兩者結果相同:
[root@localhost grepDir]# cat t2.txt Every one fights for a better future,but I fight for freedom! [root@localhost grepDir]# cat t2.txt|wc -c 62 [root@localhost grepDir]# cat t2.txt|wc -m 62但含中文時差異就出現了:
[root@localhost grepDir]# cat >wcText <<EOF wc命令的功能為統計指定文件中的字節數、單詞數、行數, 並將統計結果顯示輸出 I'm MenAngel EOF [root@localhost grepDir]# cat wcText|wc -c 120 [root@localhost grepDir]# cat wcText|wc -m 52原因很清楚:-c 數的是位元組,-m 數的是字元,一個中文字佔多個位元組。
最長行長度
[root@localhost grepDir]# cat t1.txt|wc -L 116注意 -L 給出的是位元組長度。
統計檔案數量
[root@localhost grepDir]# ls -l|wc -l 6ls -l 的每一行代表一個檔案(含第一行總用量),實務上若要精確計數通常會再減一或改用
ls -1 | wc -l。只顯示數字不顯示檔名
[root@localhost grepDir]# wc -l wcText 2 wcText [root@localhost grepDir]# cat wcText|wc -l 2處理 echo 的輸出
[root@localhost grepDir]# echo "I'm MenAngel"|wc -c 13 [root@localhost grepDir]# echo "I'm MenAngel"|wc -m 13 [root@localhost grepDir]# echo "I'm MenAngel"|wc -l 1wc 與管線的搭配變化很多,值得在實務中慢慢累積。