2013-02-04 135 views
16

我一直在试图使尾部多了几分服务器初创可读。我现在命令将过滤掉大部分从启动将INFO和DEBUG消息:彩色化尾输出

tail -F ../server/durango/log/server.log | grep -e "ERROR" -e "WARN" -e "Shutdown" -e "MicroKernel" | grep --color=auto -E 'MicroKernel|$' 

我想要做的是工艺的东西,将突出WARN黄色错误为红色,微内核绿色的。我尝试了管道的grep --color =自动多次,但生存的唯一颜色是在管道的最后一个命令。

是否有一个衬垫做到这一点?甚至是多班轮?

回答

22

是的,有办法做到这一点。也就是说,只要你的终端支持ANSI escape sequences。这是大多数存在的终端。

我想我不需要解释如何用grep,SED等点的颜色吗?

见下文,这将使

WARN yellow 
ERROR red 
foo green 

这里是例子:

kent$ echo "WARN 
ERROR 
foo"|sed 's#WARN#\x1b[33m&#; s#ERROR#\x1b[31m&#; s#foo#\x1b[32m&#' 

注意\x1b对于ESC十六进制(^VEsc键) 。

看到的结果:

enter image description here

+4

添加“^ [[0米”后,立即号(&)在'sed'命令,如果你只想要上色匹配的关键字,而不是整个行。 – chepner

+0

@chepner你对。好评。 – Kent

+3

我编辑了一个更容易复制/粘贴的东西。 – sehe

6

我写a script这个多年前。通过连续调用highlight彼此,您可以轻松地覆盖多种颜色的案例。

自述:

Usage: ./highlight [-i] [--color=COLOR_STRING] [--] <PATTERN0> [PATTERN1...] 

This is highlight version 1.0. 

This program takes text via standard input and outputs it with the given 
perlre(1) pattern(s) highlighted with the given color. If no color option 
is specified, it defaults to 'bold red'. Colors may be anything 
that Perl's Term::ANSIColor understands. This program is similar to 
"grep --color PATTERN" except both matching and non-matching lines are 
printed. 

The default color can be selected via the $HIGHLIGHT_COLOR environment 
variable. The command-line option takes precedence. 

Passing -i or --ignore-case will enable case-insensitive matching. 

If your pattern begins with a dash ('-'), you can pass a '--' argument 
after any options and before your pattern to distinguish it from an 
option.