I have a script that sends out email to sysadmin group when a server is powered on or off. The email contains dump of some important logs like /var/log/messages
, /var/log/secure
, /var/log/boot.log
. The /var/log/boot.log
contains some special characters which when sent in email looks garbled as it contains some special formatting chars for coloring and tabs. How do I remove these special characters to make it readable?
I know I can use sed to remove the characters but I am looking for an easy and elegant solution.
(The /var/log/boot.log
is from CentOS 6.x)
Here's a dump of my /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 ~]#
sed
not easy or elegant?[:cntrl:]
pattern and indeedsed 's/[[:cntrl:]]//g' file1 > file2
is first what comes to my mind. You can use alsotr
,awk
, purebash
... but hard to judge which tool is more easy or elegant.\e[0m
(colors), although I'm not sure if^M
is control or literal in you log.