我有一个脚本,可以在服务器打开或关闭时向系统管理员组发送电子邮件。该电子邮件包含一些重要日志的转储,例如/var/log/messages
,,,/var/log/secure
。/var/log/boot.log
其中/var/log/boot.log
包含一些特殊字符,在电子邮件中发送时看起来会出现乱码,因为它包含一些用于着色和制表符的特殊格式字符。如何删除这些特殊字符以使其可读?
我知道我可以使用 sed 删除字符,但我正在寻找一个简单而优雅的解决方案。
(/var/log/boot.log
来自 CentOS 6.x)
这是我的转储/var/log/boot/.log
:
[root@vagrant ~]# cat -v /var/log/boot.log
^[%G Welcome to ^[[0;36mCentOS^[[0;39m ^M
Starting udev: ^[%G^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Setting hostname vagrant: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Setting up Logical Volume Management: 5 logical volume(s) in volume group "vgdynamic" now active^M
3 logical volume(s) in volume group "vgstatic" now active^M
^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Checking filesystems^M
/dev/mapper/vgstatic-lvroot: clean, 8102/884736 files, 175959/3537920 blocks^M
/dev/sda1: clean, 44/32768 files, 17226/131072 blocks^M
/dev/mapper/vgdynamic-lvhome: clean, 10280/196608 files, 74141/786432 blocks^M
/dev/mapper/vgdynamic-lvopt: clean, 932/655360 files, 104046/2620416 blocks^M
/dev/mapper/vgstatic-lvtmp: clean, 12/131072 files, 25386/524288 blocks^M
/dev/mapper/vgstatic-lvusr: clean, 41785/262144 files, 236524/1048576 blocks^M
/dev/mapper/vgdynamic-lvvar: clean, 1989/393216 files, 93057/1572864 blocks^M
/dev/mapper/vgdynamic-lvvarlog: clean, 55/49152 files, 8030/196608 blocks^M
/dev/mapper/vgdynamic-lvaudit: clean, 14/65536 files, 73366/262144 blocks^M
^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Remounting root filesystem in read-write mode: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Mounting local filesystems: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Enabling /etc/fstab swaps: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Entering non-interactive startup^M
Calling the system activity data collector (sadc)... ^M
Starting monitoring for VG vgdynamic: 5 logical volume(s) in volume group "vgdynamic" monitored^M
^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting monitoring for VG vgstatic: 3 logical volume(s) in volume group "vgstatic" monitored^M
^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Bringing up loopback interface: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Bringing up interface eth0: ^M
Determining IP information for eth0... done.^M
^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting auditd: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting system logger: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting lwsmd: ^[[60G[^[[0;32m OK ^[[0;39m]^M^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Mounting filesystems: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Retrigger failed udev events^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting the VirtualBox Guest Additions ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting VirtualBox Guest Addition service ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting sshd: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting ntpd: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting crond: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
Starting atd: ^[[60G[^[[0;32m OK ^[[0;39m]^M^M
[root@vagrant ~]#
答案1
您应该首先从更复杂(更长)的模式开始,如果您仅从控制字符开始,其余的也可能与普通文本中的模式匹配。如果速度不是最高优先级,请将处理拆分为多个语句,这样它们更容易调试,并且您可以更轻松地控制删除顺序。
这颜色转义序列开始于^[
并结束于m
.您可以使用sed
以下命令删除它们:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m//g"
其他模式以(位置光标)开始^[
和结束:G
sed -r "s/\x1B\[.*G//g"
任何剩余的控制字符应与:
sed 's/[[:cntrl:]]//g'
合并为:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?m//g" /var/log/boot.log | \
sed -r "s/\x1B\[.*G//g" | \
sed 's/[[:cntrl:]]//g'
究竟是优雅还是大象,仁者见仁智者见智。
答案2
也许该命令dos2unix
就是您正在寻找的。