• 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<