head命令用法舉例

head與tail就像它的名字一樣的淺顯易懂,head用來顯示檔案的開頭至標準輸出中,而tail 想當然爾就是看檔案的結尾。 

1.命令格式:head [參數]... [文件]...  

2.命令功能:head 用來顯示檔案的開頭至標準輸出中,默認head命令打印其相應文件的開頭10行。
 
3.命令參數:
-q 隱藏文件名
-v 顯示文件名
-c<字節> 顯示字節數
-n<行數> 顯示的行數

4.使用實例:
4.1顯示文件的前n行: head -n 文件名
[hduser0401@dev-l002782 ~]$ head -5 test0929.txt
1,test01
2,test02
3,test03
4,test04
5,test05

4.2顯示文件的前5行且顯示文件名: head -5 -v 文件名
[hduser0401@dev-l002782 ~]$ head -5 -v test0929.txt
==> test0929.txt <==
1,test01
2,test02
3,test03
4,test04
5,test05

4.3顯示文件的前5行且隱藏文件名: head -5 -q 文件名 等價於head -5 文件名
[hduser0401@dev-l002782 ~]$ head -5 -q test0929.txt
1,test01
2,test02
3,test03
4,test04
5,test05

4.4顯示文件前n個字節:head -c n 文件名
[hduser0401@dev-l002782 ~]$ head -c 20 test0929.txt
1,test01
2,test02
3,[hduser0401@dev-l002782 ~]$ 

4.5文件的除了最後n個字節以外的內容: head -c -n 文件名
[hduser0401@dev-l002782 ~]$ cat test0929.txt
1,test01
2,test02
3,test03
4,test04
5,test05
6,test06
7,test07
8,test08
9,test09
10,test10
[hduser0401@dev-l002782 ~]$ head -c -10 test0929.txt
1,test01
2,test02
3,test03
4,test04
5,test05
6,test06
7,test07
8,test08
9,test09

4.6輸出文件除了最後m行以外的內容:head -n -m 文件名
[hduser0401@dev-l002782 ~]$ cat test0929.txt
1,test01
2,test02
3,test03
4,test04
5,test05
6,test06
7,test07
8,test08
9,test09
10,test10
[hduser0401@dev-l002782 ~]$ head -n -4 test0929.txt
1,test01
2,test02
3,test03
4,test04
5,test05
6,test06
 

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