歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(35)wc命令

每天一個Linux命令(35)wc命令

日期:2017/3/3 12:12:35   编辑:Linux技術

[b] Linux系統中的wc(Word Count)命令的功能為統計指定文件中的字節數、字數、行數,並將統計結果顯示輸出。[/b][b] (1)用法:[/b]

[b] 用法: wc [選項] [文件]......[/b] [b] (2)功能:[/b]

[b] 功能: wc命令用來計算數字。利用wc指令我們可以計算文件的Byte數、字數或是列數,若不指定文件名稱,或是所給予的文件名為“-”,則wc指令會從標准輸入設備讀取數據。[/b] [b] (3)選項參數[/b]

1) -c --bytes            打印字節數 2) -m --chars             打印字符數,這個標志不能與 -c 標志一起使用。

3) -l --lines              打印行數 4) -L --max-line-length        打印最長行的長度

5) -w --words             打印單詞數,一個單詞被定義為由空白、跳格或換行字符分隔的字符串。[b] (4)實例:[/b]

1)[root@localhost grepDir]# cat patfile|wc -l            統計指定文件的行數

[root@localhost grepDir]# cat patfile
MenAngel
sunjimeng
[root@localhost grepDir]# cat patfile|wc -l
2

2)[root@localhost grepDir]# cat patfile|wc -c            統計文件中的字節數

[root@localhost grepDir]# cat patfile|wc -c                   //這裡回車應該算2個字符,2+8+9
19

3)[root@localhost grepDir]# cat patfile|wc -m           統計文件中的字符數

[root@localhost grepDir]# cat patfile|wc -m  
19

4)-c參數與-m參數的區別

[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

5)[root@localhost grepDir]# cat t1.txt|wc -L              顯示文件中最長的一行的長度,是字節長度

[root@localhost grepDir]# cat t1.txt
I'm MenAngel!
Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
[root@localhost grepDir]# cat t1.txt|wc -L
116

6)[root@localhost grepDir]# ls -l|wc -l                 統計當前目錄下的文件的總數

[root@localhost grepDir]# ls -l|wc -l
6
[root@localhost grepDir]# ll
總用量 20
-rw-r--r--. 1 root root  19 5月  31 04:44 patfile
-rw-r--r--. 1 root root 131 5月  31 03:56 t1.txt
-rw-r--r--. 1 root root  62 5月  31 03:58 t2.txt
-rw-r--r--. 1 root root 297 5月  31 04:04 t3.txt
-rw-r--r--. 1 root root 120 5月  31 18:25 wcText

7)[root@localhost grepDir]# cat wcText|wc -l             只顯示統計數字而不顯示文件名

[root@localhost grepDir]# wc -l wcText
2 wcText
[root@localhost grepDir]# cat wcText|wc -l
2

8)[root@localhost grepDir]# echo "I'm MenAngel"|wc -c       用wc命令處理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
1

wc命令與管道的配合方法,多種多樣,以後慢慢學習。

Copyright © Linux教程網 All Rights Reserved