linux head tail cut 命令

  • head

默認輸出文件內容的前10行

NAME

  - output the first part of files

SYNOPSIS( 大綱,摘要)

  - head [option]... [file]...

參數

  -n 指定行

  -c --bytes

  -v 顯示文件的文件名

#顯示前5行
head -n 5 /etc/inittab
head -5 /etc/inittab  #只需要掌握這條命令即可
#顯示前5個字節
head -c 5 /etc/inittab
#除去最後10行,其它內容都輸出
head -n -10 /etc/inittab
#同時查看多個文件的前10行
head /etc/inittab /etc/services
#顯示文件的文件名
head -v /etc/inittab
  • tail

默認輸出文件內容的後10行

NAME

  - output the last part of files

SYNOPSIS

  - tail [option]... [file]...

參數

  -n 指定行

  -f --follow

     output appended data as the file grows

#顯示後5行
tail -5 /etc/inittab
#動態實時的顯示文件的內容
tail -f test.log
tailf test.log
#tailf是單獨的命令:follow the growth of a log file
  • cut

切割:cut命令默認是以tab鍵作爲分割符,但是,只支持單個分割符!

NAME

  - remove sections(區段) from each line of files

SYNOPSIS

  - cut option... [file]...

參數

  -b --bytes  字節

  -c --characters  字符

  #1個英文字符 = 1個字節

  #1箇中文字符 = 2個字節

  -d --delimiter  指定分割符(默認是以tab鍵作爲分割符)

  -f --fields  指定分割的區域(常和-d參數配合使用)


#練習素材
echo "I am oldboy my qq is 1234567" >test.txt
#按字節切割(按字符切割操作同理,如果有中文,一定要指定-c參數,否則會出現亂碼)
cut -b 3 test.txt
cut -b 3-4 test.txt
cut -b -4 test.txt
cut -b 4- test.txt
cut -b 1,4- test.txt
cut -b -4,4- test.txt
#切割出來指定的域
head -1 /etc/passwd|cut -d : -f4
#修改練習素材
cat >test.txt<<EOF
thisistabline.
this is space line.
#把tab鍵顯示出來
cat -T test.txt    #參數T:顯示tab鍵 ^I 
sed -n 1 test.txt  #參數n:取消默認輸出  參數L:打印不可見字符 \t(tab) $(空格)
#cut命令默認是以tab鍵作爲分割符(awk命令默認是以空格作爲分割符)
cut -f2-3 test.txt
#指定空格爲分割符
cut -d ' ' -f2-3 test.txt
#注意:cut命令只支持單個分割符!


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