您的位置:首页 > 运维架构 > Linux

linux head tail cut 命令

2016-08-22 01:27 555 查看
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命令只支持单个分割符!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux head tail