1. 前言
本文主要介绍如果高效地使用工具高亮显示某些输出结果和关键字。
您可以使用正则表达式和特定规则格式突出显示文本。分层规则格式易于理解,但功能非常强大。该注释将从标准输入读取每一行,每行根据FILE中定义的规则突出显示,然后打印到标准输出(屏幕)。
remark
命令一般很少人知道它可以把系统日志文件关键内容高亮标注颜色并打印到屏幕上,例如像ping,traceroute等等命令的输出结果。一般情况下,我们需要自己写正则表达式定义哪些关键结果应该高亮。有了remark
命令后,你可能不太需要自己手动编写规则了。
2. 安装remark
命令
Debian / Ubuntu Linux 执行以下命令安装remark:
[root@zcwyou ~]# cd /tmp
[root@zcwyou ~]# wget http://savannah.nongnu.org/download/regex-markup/regex-markup_0.10.0-1_amd64.deb
[root@zcwyou ~]# sudo dpkg -i regex-markup_0.10.0-1_amd64.deb
RHEL / CentOS / Fedora Linux 64用户,执行以下命令安装remark
命令:
[root@zcwyou ~]# cd /tmp
[root@zcwyou ~]# wget http://savannah.nongnu.org/download/regex-markup/regex-markup-0.10.0-1.x86_64.rpm
[root@zcwyou ~]# rpm -ivh regex-markup-0.10.0-1.x86_64.rpm
或者你从源代码编译它
[root@zcwyou ~]# cd /tmp
[root@zcwyou ~]# wget http://savannah.nongnu.org/download/regex-markup/regex-markup-0.10.0.tar.gz
[root@zcwyou ~]# tar -xvf regex-markup-0.10.0.tar.gz
[root@zcwyou ~]# cd regex-markup-0.10.
[root@zcwyou ~]# ./configure
[root@zcwyou ~]# make
[root@zcwyou ~]# sudo make install
3. remark
语法
command1 | remark /path/to/config
command2 arg1 arg2 | remark /path/to/config
高亮显示ping的执行结果
[root@zcwyou ~]# ping -c 4 www.linuxrumen.com | remark /usr/share/regex-markup/ping
你可以创建一个bash shell 功能和它入到~/.bashrc file的配置文件中
[root@zcwyou ~]# ping() { /bin/ping $@ | remark /usr/share/regex-markup/ping; }
remark-pin 的规则文件放在/usr/share/regex-markup/ping。它由样式和宏定义以及匹配语句组成。匹配语句的顺序很重要,因为它们是从上到下执行的。样式和宏在使用之前需要定义。规则文件的语法类似于编程语言(如C和Java)的缩写,并且使用空格并不重要:
[root@zcwyou ~]# cat /usr/share/regex-markup/ping
它的文件内容一般是这样的
# Rules to highlight the output of ping(8)
include "common"
# Special: Color all lines that don't match any of the rules below red
/.*/ red
/^PING ([-.a-zA-Z0-9]+) \(([-.a-zA-Z0-9]+)\) ([0-9]+)\(([0-9]+)\) bytes of data\./ {
default
1 blue
2 green
3,4 red
break # this is merely to skip the matches below
}
/^PING ([-.a-zA-Z0-9]+) \(([-.a-zA-Z0-9]+)\): ([0-9]+) data bytes/ {
default
1 blue
2 green
3 red
break # this is merely to skip the matches below
}
/^([0-9]+) bytes from ([-.a-zA-Z0-9]+) \(([-.a-zA-Z0-9]+)\): icmp_seq=([0-9]+) ttl=([0-9]+) time=(.*)/ {
default
1,4,5,6 red
2 blue
3 green
break
}
/^([0-9]+) bytes from ([-.a-zA-Z0-9]+): icmp_seq=([0-9]+) ttl=([0-9]+) time=(.*)/ {
default
1,3,4,5 red
2 green
break
}
/^--- ([-.a-zA-Z0-9]+) ping statistics ---/ {
default
1 blue
break
}
/^([0-9]+) packets transmitted, ([0-9]+) packets received, ([0-9]+)% packet loss/ {
default
1,2,3 red
break
}
/^([0-9]+) packets transmitted, ([0-9]+) received, ([0-9]+)% packet loss, time ([0-9]+ms)/ {
default
1,2,3,4 red
break
}
/^round-trip min\/avg\/max = ([.0-9]+)\/([.0-9]+)\/(.*)/ {
default
1,2,3 red
break
}
/^rtt min\/avg\/max\/mdev = ([.0-9]+)\/([.0-9]+)\/([.0-9]+)\/(.*)$/ {
default
1,2,3,4 red
break
}
默认的样式定义文件放在/usr/share/regex-markup/common
试试跟traceroute结合
[root@zcwyou ~]# traceroute www.linuxrumen.com | remark /usr/local/share/regex-markup/traceroute
执行以下命令把Linux系统日志高亮显示
[root@zcwyou ~]# grep something /var/log/syslog | remark /usr/share/regex-markup/syslog
[root@zcwyou ~]# tail -f /var/log/syslog | remark /usr/share/regex-markup/syslog
使用remark
命令高亮显示diff
结果
先使用diff
命令对比file1和file2,然后重定向到remark,由remark高亮显示结果
[root@zcwyou ~]# diff file1 file2 | remark /usr/share/regex-markup/diff
使用remark
命令高亮显示mark结果
[root@zcwyou ~]# cd /path/to/build
[root@zcwyou ~]# make | remark /usr/share/regex-markup/make
4. 如何定制高亮规则?
如果你有这样的需求,请参考remark
命令手册:
[root@zcwyou ~]# man remark
评论前必须登录!
注册