wc#
wc (Word Count) 是包含在 GNU Coreutils 內的文字統計工具。
wc 是 Linux 系統中用於統計檔案字數的指令。它可以快速計算一個或多個檔案中的「行數 (Lines)」、「單字數 (Words)」以及「位元組數 (Bytes)」。這在處理日誌檔、程式碼審核或純文字資料分析時非常實用。
Install#
$ sudo apt install coreutilsSetting in up#
Operate#
wc [options] document.txt
僅統計行數(最常用的情境)#
cat log.txt | wc -l
同時統計多個檔案#
wc file1.txt file2.txt
| 參數 | 範例指令 | 說明 |
|---|---|---|
-l | wc -l file | 統計行數。計算檔案中換行符號的數量。 |
-w | wc -w file | 統計字數。計算由空白字元分隔的單字數量。 |
-c | wc -c file | 統計位元組數。計算檔案的總位元組大小。 |
-m | wc -m file | 統計字元數。在多位元組編碼(如 UTF-8)下,與 -c 不同。 |
-L | wc -L file | 最長行長度。顯示檔案中內容最長那一行的字元數。 |
| 格式 | 說明 | 範例 |
|---|---|---|
wc -l | 僅顯示行數 | ls | wc -l (計算目錄下的檔案數量) |
wc -lc | 顯示行數與位元組 | wc -lc config.yaml |
wc -w | 僅統計單字 | echo "Hello World" | wc -w |
Reference#
Official docs: