bash 着色尾部输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14691680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:26:47  来源:igfitidea点击:

Colorize tail output

linuxbashshell

提问by tpederson

I've been trying to make tail a little more readable for server startups. My current command filters out most of the INFO and DEBUG messages from the startup:

我一直在努力让 tail 对于服务器初创公司更具可读性。我当前的命令从启动中过滤掉了大部分 INFO 和 DEBUG 消息:

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

What I would like to do is craft something that would highlight WARNin yellow and ERRORin red, and MicroKernelin green. I tried just piping grep --color=automultiple times, but the only color that survives is the last command in the pipe.

我想做的是制作一些东西,用黄色突出显示警告,红色突出显示错误,绿色突出微内核。我尝试多次使用管道grep --color=auto,但唯一幸存的颜色是管道中的最后一个命令。

Is there a one liner to do this? Or even a many-liner?

有没有一个班轮可以做到这一点?甚至是多班轮?

回答by Kent

yes, there is way to do this. That is, as long as your terminal supports ANSI escape sequences. This is most terminals that exist.

是的,有办法做到这一点。也就是说,只要您的终端支持ANSI 转义序列。这是大多数存在的终端。

I think I don't need explain how to grep, sed etc. point is the color right?

我想我不需要解释如何 grep、sed 等。点是颜色对吗?

see below, this will make

见下文,这将使

WARN yellow
ERROR red
foo   green

here is example:

这是示例:

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

Note: \x1bis hexadecimal for the ESCcharacter (^VEsc).

注意:\x1bESC字符 ( ^VEsc) 的十六进制。

to see the result:

查看结果:

enter image description here

在此处输入图片说明

回答by Dave Goodell

I wrote a scriptfor this years ago. You can easily cover the case of multiple colors by piping successive invocations of highlightto each other.

我多年前为此写了一个脚本。您可以通过管道相互连续调用来轻松涵盖多种颜色的情况highlight

From the README:

从自述文件:

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.

回答by kimusan

I have been using a tool called grc for this for years. works like a charm. It comes with some quite good templates for many standard log outputs and formats and it is easy to define your own. A command I use often is

多年来,我一直在使用名为 grc 的工具。奇迹般有效。它为许多标准日志输出和格式提供了一些非常好的模板,并且很容易定义您自己的模板。我经常使用的命令是

grc tail -f /var/log/syslog

It colorizes the syslog output so it is easy to spot errors (typically marked red.

它为系统日志输出着色,因此很容易发现错误(通常标记为红色。

Find the tool here:

在此处找到该工具:

https://github.com/garabik/grc

https://github.com/garabik/grc

(it is also available as package for most common linux flavours).

(它也可以作为最常见的 linux 风格的包提供)。

回答by Mark Li

You can create a colored log instead of using a complex command.

您可以创建彩色日志而不是使用复杂的命令。

enter image description here

在此处输入图片说明

For php is like this:

对于 php 是这样的:

echo "^[[30;43m".$ip."^[[0m";

The key point is to use Ctrl-v ctrl-[ to input a green ^[under insert mode in vim, direct input ^[ does not work.

关键是在vim的insert模式下用Ctrl-v ctrl-[输入一个绿色的^[,直接输入^[不行。

enter image description here

在此处输入图片说明

More info here

更多信息在这里

回答by Art Swri

I use a version of this that I hacked: python log watcher

我使用了一个我破解的版本: python log watcher