wc是Word Count的缩写,该命令可以用来统计文件中文本的行数、单词数或字符数。
统计文本的行数、单词数和字节数
wc [option] [file]
-c 输出字节数 -m 输出字符数 -l 输出行数 -L 输出最长的行的长度 -w 输出单词数(空格或制表符分隔的字符串)
wc命令使用比较简单,不使用选项时输出文件的行数、单词数和字节数:
[peter@ibi98 wc]$ cat demo_wc Electron cryotomography (ECT) enables the 3D reconstruction of intact cells in a near-native state. Images produced by ECT have led to the proposal that an ancient sporulation-like event gave rise to the second membrane in diderm bacteria. [peter@ibi98 wc]$ wc demo_wc 4 37 241 demo_wc [peter@ibi98 wc]$ wc -l demo_wc 4 demo_wc [peter@ibi98 wc]$ wc -w demo_wc 37 demo_wc [peter@ibi98 wc]$ wc -c demo_wc 241 demo_wc [peter@ibi98 wc]$ wc -m demo_wc 241 demo_wc
由于每个英文字母和数字占一个字节,所以-c和-m选项的结果是一样的。如果是中文,他们的结果就会不同,如下面的例子:
[peter@ibi98 wc]$ cat demo_wc_Chinese 原核生物:真细菌 古细菌 真核生物:原生生物 真菌 动物 植物 [peter@ibi98 wc]$ wc demo_wc_Chinese 2 6 84 demo_wc_Chinese [peter@ibi98 wc]$ wc -c demo_wc_Chinese 84 demo_wc_Chinese [peter@ibi98 wc]$ wc -m demo_wc_Chinese 32 demo_wc_Chinese
文中有26个中文字符,每个占三个字节(UTF-8编码,如果是GB2312编码的话是2个字节),共78个字节。另外还有4个空格和2个换行符各占一个字节,所以一共是84(78+4+2)个字节,但只有32(26+4+2)个字符。