3.7 tailf:跟踪日志文件
3.7.1 命令详解
【命令星级】 ★★★★★
【功能说明】
tailf命令在工作中的主要使命就是跟踪日志文件,首先将默认输出日志文件的最后10行,然后实时地显示文件的增加内容。
tailf命令几乎等同于tail -f,与tail -f不同的是,如果文件不增长,那么它不会去访问磁盘文件,也不会更改文件的访问时间。
【语法格式】
tailf [option] [file]
tailf [选项] [文件]
说明:在tailf命令及后面的选项和文件里,每个元素直接都至少要有一个空格。
【选项说明】
表3-10针对该命令的参数选项进行了说明。
表3-10 tailf 命令的参数选项及说明
3.7.2 使用范例
**范例3-31:**跟踪日志文件。
[root@centos7 ~]# tailf /var/log/boot.log-20201021 #可以方便地查阅正在改变的日志文件。
[ OK ] Started Service for virtual machines hosted on VMware.
[ OK ] Started D-Bus System Message Bus.
[ OK ] Started Dump dmesg to /var/log/dmesg.
[ OK ] Started Install ABRT coredump hook.
[ OK ] Started Permit User Sessions.
[ OK ] Started Resets System Activity Logs.
[ OK ] Started Login Service.
[ OK ] Started Command Scheduler.
Starting Terminate Plymouth Boot Screen...
Starting Wait for Plymouth Boot Screen to Quit...
3.8 cut:从文本中提取一段文字并输出
3.8.1 命令详解
【命令星级】 ★★★★★
【功能说明】
cut命令从文件的每一行剪切字节、字符或字段,并将这些字节、字符或字段输出至标准输出。
【语法格式】
cut [option] [file]
cut [选项] [文件]
**说明:**在cut命令及后面的选项和文件里,每个元素直接都至少要有一个空格。
【选项说明】
表3-11针对该命令的参数选项进行了说明。
表3-11 cut命令的参数选项及说明
3.8.2 使用范例
范例3-32:以字节为分隔符。
[root@centos7 ~]# echo I am neteagle myqq is 88888888. >neteagle.txt
[root@centos7 ~]# cat neteagle.txt
I am neteagle myqq is 88888888.
[root@centos7 ~]# cut -b 3 neteagle.txt #至输出第3个字节。
a
[root@centos7 ~]# cut -b 3-5,10 neteagle.txt #-b支持例如3-5的写法,而且多个定位之间用逗号隔开。
am a
[root@centos7 ~]# cut -b -3 neteagle.txt #-3表示从第一个字节到第三个字节。
I a
[root@centos7 ~]# cut -b 3- neteagle.txt #3-表示从第三个字节到行尾。
am neteagle myqq is 88888888.
[root@centos7 ~]# cut -b -3,3- neteagle.txt #这种写法会输出正好,并且不会出现连续两个重叠的字母a。
I am neteagle myqq is 88888888.
**范例3-33:**以字符为分隔符。
[root@centos7 ~]# cut -c 2-10 neteagle.txt
am netea
[root@centos7 ~]# cut -b 2-10 neteagle.txt
am netea
#说明:本例使用选项-c和-b结果没有区别,是因为字母是单字节字符。如果提取中午,区别就看出来了。
[root@centos7 ~]# echo 星期一上班 >>neteagle.txt
[root@centos7 ~]# cat neteagle.txt
I am neteagle myqq is 88888888.
星期一上班
[root@centos7 ~]# cut -c 2-10 neteagle.txt
am netea
期一上班
[root@centos7 ~]# cut -b 2-10 neteagle.txt
am netea
期一
#说明:用选项-c则会以字符为单位,输出正常。而选项-b只会傻傻的以字节(8位二进制位)来计算,输出就是乱码。当遇到多字节字符时,可以使用-n选项,-n用于告诉cut不要将多字节字符拆开。
[root@centos7 ~]# cut -nb 2-10 neteagle.txt
am netea
期一上班
**范例3-34:**自定义分隔符例子。
[root@centos7 ~]# cut -d : -f 1 /etc/passwd #选项-d指定以“:”作为分隔符,选项-f指定显示第一个区域。
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
tss
abrt
sshd
postfix
tcpdump
neteagle
[root@centos7 ~]# cut -d : -f 3-5 /etc/passwd #显示第3列到第5列。
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp
5:0:sync
6:0:shutdown
7:0:halt
8:12:mail
11:0:operator
12:100:games
14:50:FTP User
99:99:Nobody
192:192:systemd Network Management
81:81:System message bus
999:998:User for polkitd
59:59:Account used by the trousers package to sandbox the tcsd daemon
173:173:
74:74:Privilege-separated SSH
89:89:
72:72:
1000:1000:
**提示:**本例cut的功能类似于第4章的awk命令,但是awk更灵活,功能也更强。