龙空技术网

高亮显示系统日志应该用什么命令

老欧讲Linux 911

前言:

如今看官们对“debian traceroute”可能比较注重,小伙伴们都想要了解一些“debian traceroute”的相关知识。那么小编同时在网络上收集了一些对于“debian traceroute””的相关内容,希望兄弟们能喜欢,大家快快来学习一下吧!

请关注本头条号,每天坚持更新原创干货技术文章。

如需学习视频,请在微信搜索公众号“智传网优”直接开始自助视频学习

1. 前言

本文主要介绍如果高效地使用工具高亮显示某些输出结果和关键字。

您可以使用正则表达式和特定规则格式突出显示文本。分层规则格式易于理解,但功能非常强大。该注释将从标准输入读取每一行,每行根据FILE中定义的规则突出显示,然后打印到标准输出(屏幕)。

高效地使用工具高亮显示某些输出结果和关键字

remark命令一般很少人知道它可以把系统日志文件关键内容高亮标注颜色并打印到屏幕上,例如像ping,traceroute等等命令的输出结果。一般情况下,我们需要自己写正则表达式定义哪些关键结果应该高亮。有了remark命令后,你可能不太需要自己手动编写规则了。

2. 安装`remark`命令

Debian / Ubuntu Linux 执行以下命令安装remark:

[root@zcwyou ~]# cd /tmp[root@zcwyou ~]# wget [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 [root@zcwyou ~]# rpm -ivh regex-markup-0.10.0-1.x86_64.rpm

或者你从源代码编译它

[root@zcwyou ~]# cd /tmp[root@zcwyou ~]# wget [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

安装remark命令自带高亮语法

3. `remark`语法

command1 | remark /path/to/config

command2 arg1 arg2 | remark /path/to/config

高亮显示ping的执行结果

[root@zcwyou ~]# ping -c 4  | 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  | 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

如何定制高亮规则

本文已同步至博客站,尊重原创,转载时请在正文中附带以下链接:

点击了解更多,快速查看更多的技术文章列表。

标签: #debian traceroute