shell_wc(統計數目)、head(查看前幾行)、tail(查看末尾幾行)

wc -c filename:顯示一個文件的字節數
wc -m filename:顯示一個文件的字符數
wc -l filename:顯示一個文件的行數
wc -L filename:顯示一個文件中的最長行的長度
wc -w filename:顯示一個文件的字數
[rocrocket@rocrocket programming]$ cat wc1.txt
1 2
34 5
你好
[rocrocket@rocrocket programming]$ wc -c wc1.txt
16 wc1.txt
[rocrocket@rocrocket programming]$ wc -m wc1.txt
12 wc1.txt
[rocrocket@rocrocket programming]$ wc -l wc1.txt
3 wc1.txt
[rocrocket@rocrocket programming]$ wc -L wc1.txt
4 wc1.txt
[rocrocket@rocrocket programming]$ wc -w wc1.txt
5 wc1.txt
每行結尾的換行符也算一個字符,空格也算一個字符.
採用UTF-8編碼,一個漢字在這裏被轉換爲3字節,wc -c顯示的結果是16,即“第一行的4個字節+第二行的5個字節+第三行的7個字節”=4 5 7=16.
而當使用-m選項時,一個漢字就作爲一個字符計算,是4 5 3=12.
當使用-L時,是給出最長行的長度,第二行最長,有4個字符長.(顯然是不把換行符計算在內)
使用-w是計算字數,一個字是一個word,“34”表示一個word,一個“你好”表示一個word,因此2 2 1=5.
插孔說說tab製表符,這個符號比較特殊,當使用-L時,製表符算7個字符(這要依據一個製表符的長度而定,在我的系統中一個製表符算7個空格長度).而當使用-w時,製表符和空格沒有兩樣,都作爲字的間隔來看待.當用-c時,一個製表符也就算一個字符,它的確就只是一個字符而已.
如果你直接執行wc wc1.txt,則顯示:
[rocrocket@rocrocket programming]$ wc wc1.txt
3 5 16 wc1.txt
輸出信息依次是:行數 字數 字節數 文件名稱.
wc命令
  wc命令的功能爲統計指定文件中的字節數、字數、行數, 並將統計結果顯示輸出.
  語法:wc [選項] 文件…
  說明:該命令統計給定文件中的字節數、字數、行數.如果沒有給出文件名,
則從標準輸入讀取.wc同時也給出所有指定文件的總統計數.字是由空格字符區分開的最大字符串.
  該命令各選項含義如下:
  - c 統計字節數.
  - l 統計行數.
  - w 統計字數.
  這些選項可以組合使用.
  輸出列的順序和數目不受選項的順序和數目的影響.
  總是按下述順序顯示並且每項最多一列.
  行數、字數、字節數、文件名
  如果命令行中沒有文件名,則輸出中不出現文件名.
  例如:
  $ wc - lcw file1 file2
  4 33 file1
  7 52 file2
  11 11 85 total
  省略任選項-lcw,wc命令的執行結果與上面一樣.
shell_head
1.命令格式:
head [參數]... [文件]...
2.命令功能:
head 用來顯示檔案的開頭至標準輸出中,默認head命令打印其相應文件的開頭10行。
3.命令參數:
-q 隱藏文件名
-v 顯示文件名
-c<字節> 顯示字節數
-n<行數> 顯示的行數
4.使用實例:
實例1:顯示文件的前n行
命令:
head -n 5 log2014.log
輸出:
[root@localhost test]# cat log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12
==============================
[root@localhost test]# head -n 5 log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05[root@localhost test]#
實例2:顯示文件前n個字節
命令:
head -c 20 log2014.log
輸出:
[root@localhost test]# head -c 20 log2014.log
2014-01
2014-02
2014
[root@localhost test]#
實例3:文件的除了最後n個字節以外的內容
命令:
head -c -32 log2014.log
輸出:
[root@localhost test]# head -c -32 log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12[root@localhost test]#
實例4:輸出文件除了最後n行的全部內容
命令:
head -n -6 log2014.log
輸出:
[root@localhost test]# head -n -6 log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07[root@localhost test]#
shell_tail
1.命令格式;
tail[必要參數][選擇參數][文件]
2.命令功能:
用於顯示指定文件末尾內容,不指定文件時,作爲輸入信息進行處理。常用查看日誌文件。
3.命令參數:
-f 循環讀取
-q 不顯示處理信息
-v 顯示詳細的處理信息
-c<數目> 顯示的字節數
-n<行數> 顯示行數
--pid=PID 與-f合用,表示在進程ID,PID死掉之後結束.
-q, --quiet, --silent 從不輸出給出文件名的首部
-s, --sleep-interval=S 與-f合用,表示在每次反覆的間隔休眠S秒
4.使用實例:
實例1:顯示文件末尾內容
命令:
tail -n 5 log2014.log
輸出:
[root@localhost test]# tail -n 5 log2014.log
2014-09
2014-10
2014-11
2014-12
==============================[root@localhost test]#
說明:
顯示文件最後5行內容
實例2:循環查看文件內容
命令:
tail -f test.log
輸出:
[root@localhost ~]# ping 192.168.120.204 > test.log &
[1] 11891[root@localhost ~]# tail -f test.log
PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.
64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms
64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms
64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms
64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms
64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms
64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms
64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms
64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms
64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms
[root@localhost ~]#
說明:
ping 192.168.120.204 > test.log & //在後臺ping遠程主機。並輸出文件到test.log;這種做法也使用於一個以上的檔案監視。用Ctrl+c來終止。
實例3:從第5行開始顯示文件
命令:
tail -n +5 log2014.log
輸出:
[root@localhost test]# cat log2014.log
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12
==============================
[root@localhost test]# tail -n +5 log2014.log
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章