Linux shell中基于正则表达式的颜色高亮输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4267400/
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
Colour highlighting output based on regex in shell
提问by Joel
I'd like to know if I can colour highlight the output of a shell command that matches certain strings.
我想知道是否可以突出显示与某些字符串匹配的 shell 命令的输出。
For example, if I run myCommand, with the output below:
例如,如果我运行 myCommand,输出如下:
> myCommand
DEBUG foo bar
INFO bla bla
ERROR yak yak
I'd like all lines matching ^ERROR\s.* to be highlighted red.
我希望所有与 ^ERROR\s.* 匹配的行都以红色突出显示。
Similarly, I'd like the same highlighting to be applied to the output of grep, less etc...
同样,我希望将相同的突出显示应用于 grep、less 等的输出...
EDIT: I probably should mention that ideally I'd like to enable this feature globally via a 'profile' option in my .bashrc.
编辑:我可能应该提到,理想情况下,我想通过我的 .bashrc 中的“配置文件”选项全局启用此功能。
采纳答案by Gilles 'SO- stop being evil'
If you want to enable this globally, you'll want a terminal feature, not a process that you pipe output into, because a pipe would be disruptive to some command (two problems are that stdout and stderr would appear out-of-order and buffered, and that some commands just behave differently when outputting to a terminal).
如果要全局启用此功能,则需要终端功能,而不是通过管道将输出传输到的进程,因为管道会破坏某些命令(两个问题是 stdout 和 stderr 会出现乱序和缓冲,并且某些命令在输出到终端时的行为有所不同)。
I don't know of any “conventional” terminal with this feature. It's easily done in Emacs, in a term
buffer: configure font-lock-keywords
for term-mode
.
我不知道有任何具有此功能的“传统”终端。这很容易在 Emacs 中完成,在term
缓冲区中:configure font-lock-keywords
for term-mode
.
However, you should think carefully whether you really want that feature all the time. What if the command has its own colors (e.g. grep --color
, ls --color
)? Maybe it would be better to define a short alias to a colorizer commandand run myCommand 2>&1|c
when you want to colorize myCommand
's output. You could also alias some specific always-colorize commands.
但是,您应该仔细考虑是否真的一直想要该功能。如果命令有自己的颜色(例如grep --color
, ls --color
)怎么办?也许最好为colorizer 命令定义一个简短的别名,并myCommand 2>&1|c
在您想要为myCommand
输出着色时运行。您还可以为某些特定的 always-colorize 命令设置别名。
Note that the return status of a pipeline is its lastcommand, so if you run myCommand | c
, you'll get the status of c
, not myCommand
. Here's a bash wrapper that avoids this problem, which you can use as w myCommand
:
请注意,管道的返回状态是它的最后一个命令,因此如果您运行myCommand | c
,您将获得 的状态c
,而不是myCommand
。这是避免此问题的 bash 包装器,您可以将其用作w myCommand
:
w () {
"$@" | c
return $PIPESTATUS[0]
}
回答by Benoit
You could try (maybe needs a bit more escaping):
您可以尝试(可能需要更多的转义):
BLUE="$(tput setaf 4)"
BLACK="$(tput sgr0)"
command | sed "s/^ERROR /${BLUE}ERROR ${BLACK}/g"
回答by Paused until further notice.
You can use programs such as:
您可以使用以下程序:
- spc (Supercat)
- grc (Generic Colouriser)
- highlight
- histring
- pygmentize
- grep --color
- spc(超级猫)
- grc(通用着色器)
- 强调
- 字符串
- 色素沉着
- grep --color
You can do something like this, but the commands won't see a tty (some will refuse to run or behave differently or do weird things):
你可以做这样的事情,但命令不会看到 tty(有些会拒绝运行或表现不同或做奇怪的事情):
exec > >(histring -fEi error) # Bash
回答by dietbuddha
You could probably enable it for specific commands using aliases and user defined shell functions wihtout too much trouble. If your coloring errors I assume you want to process stderr. Since stderr in unbuffered you would probably want to line buffer it by sending through a fifo.
您可能可以使用别名和用户定义的 shell 函数为特定命令启用它,而不会带来太多麻烦。如果您的着色错误,我假设您要处理 stderr。由于 stderr 在无缓冲中,您可能希望通过发送 fifo 来对它进行行缓冲。
回答by E.Z.
There is an answer in superuser.com:
在superuser.com 中有一个答案:
your-command | grep -E --color 'pattern|$'
or
或者
your-command | grep --color 'pattern\|$'
This will "match your pattern or the end-of-line on each line. Only the pattern is highlighted..."
这将“匹配您的模式或每行的行尾。仅突出显示该模式......”
回答by userM1433372
Try
尝试
tail -f yourfile.log | egrep --color 'DEBUG|'
where DEBUG is the text you want to highlight.
其中 DEBUG 是您要突出显示的文本。
回答by Bush
You can use the hl command avalaible on github :
git clone http://github.com/mbornet-hl/hl
您可以在 github 上使用 hl 命令:
git clone http://github.com/mbornet-hl/hl
Then :
myCommand | hl -r '^ERROR.*'
然后:
myCommand | hl -r '^错误。*'
You can use the $HOME/.hl.cfg configuration file to simplify the command line.
hl is written in C (source is available).
You can use up to 42 differents colors of text.
您可以使用 $HOME/.hl.cfg 配置文件来简化命令行。
hl 是用 C 编写的(源代码可用)。您最多可以使用 42 种不同颜色的文本。
回答by skensell
Use awk.
使用awk。
COLORIZE_AWK_COMMAND='{ print ... | awk "$COLORIZE_AWK_COMMAND"
}'
if [ -n "$COLORIZE" ]; then
COLORIZE_AWK_COMMAND='
/pattern1/ { printf "3[1;30m" }
/pattern2/ { printf "3[1;31m" }
// { print ##代码## "3[0m"; }'
fi
then later you can pipe your output
然后你可以通过管道输出
##代码##printf is used in the patterns so we don't print a newline, just set the color.
printf 用于模式中,因此我们不打印换行符,只需设置颜色。