简体   繁体   English

每次出现时,在 Linux 终端中为特定单词着色

[英]Color specific words in Linux terminal whenever they appear

I have a bunch of scripts running in my terminal (and I don't have the ability to edit them) which output messages to the terminal.我的终端中运行了一堆脚本(我无法编辑它们),它们将消息输出到终端。 I would like my terminal to automatically color specific words in the output.我希望我的终端自动为输出中的特定单词着色。

For example, some of the scripts output FAIL when a test fails.例如,某些脚本在测试失败时会输出FAIL How can I configure the terminal to color this specific word, any time it appears, to be in a specific color (for example, red).如何配置终端以在此特定单词出现时将其着色为特定颜色(例如,红色)。

It's probably easier to colour the words yourself, rather than getting the terminal to colour them for you.自己给单词上色可能比让终端为你上色更容易。 If you can't edit the scripts that create the output, can you filter them through something else?如果您无法编辑创建输出的脚本,您可以通过其他方式过滤它们吗?

At the most likely to be available end of the scale you could pipe your output through grep :在最有可能可用的规模末端,您可以通过grep管道输出:

tail -F logfile | grep --color -P "FAIL|"

This matches either "FAIL" or "", and highlights the matched portion of the string.这匹配“FAIL”或“”,并突出显示字符串的匹配部分。

You could further use something more specialised, as described in this blog post , for example.例如,您可以进一步使用更专业的东西,如本文所述。

To colorize the text output from a command one might try piping the output of the command into sed, such as the following:要对命令的文本输出进行着色,可以尝试将命令的输出通过管道传输到 sed,如下所示:

yourcommand |您的命令 | sed -e 's/FAIL/^[[01;31mFAIL^[[00m/g' -e 's/SUCCESS/^[[01;32mSUCCESS^[[00m/g' sed -e 's/FAIL/^[[01;31mFAIL^[[00m/g' -e 's/SUCCESS/^[[01;32mSUCCESS^[[00m/g'

One could also place those substitution rules into a text file (eg colorize.sed) and use the following:也可以将这些替换规则放入文本文件(例如 colorize.sed)并使用以下内容:

yourcommand |您的命令 | sed -f colorize.sed sed -f colorize.sed

This will allow different colors to be assigned to different match strings.这将允许将不同的颜色分配给不同的匹配字符串。 Note that in my examples the '^[' means the escape character, not a carat followed by a square bracket.请注意,在我的示例中,'^[' 表示转义字符,而不是后跟方括号的克拉。 The escape character can be entered into the rule by typing Ctrl-V and then pressing the escape key.可以通过键入 Ctrl-V 然后按转义键将转义字符输入到规则中。

The colors/effects that are available for these tty codes are as follows:这些 tty 代码可用的颜色/效果如下:

Foreground colors: Black=30, Blue=34, Cyan=36, Green=32, Purple=35, Red=31, White=37, Yellow=33前景色:黑色=30,蓝色=34,青色=36,绿色=32,紫色=35,红色=31,白色=37,黄色=33

Background colors: Black=40, Blue=44, Cyan=46, Green=42, Purple=45, Red=41, White=47, Yellow=43背景颜色:黑色=40、蓝色=44、青色=46、绿色=42、紫色=45、红色=41、白色=47、黄色=43

Effects: Normal=00, Bold=01, Dim=02, Underlined=04, Blinking=05, Reversed=07, Hidden=08效果:正常=00,粗体=01,暗淡=02,下划线=04,闪烁=05,反转=07,隐藏=08

These can also be combined with a semicolon as I did (ie 01;31 to get bold red).这些也可以像我一样与分号结合使用(即 01;31 以获得粗体红色)。

Note that the '^[00m' code is required to disable the previous color/effect, otherwise the color/effect will persist after the match string.请注意,'^[00m' 代码需要禁用以前的颜色/效果,否则颜色/效果将在匹配字符串之后持续存在。 Also note that some of the effects don't work (or work as I described) with some terminal emulators.另请注意,某些效果在某些终端仿真器中不起作用(或如我所描述的那样起作用)。

I hope that I'm not just repeating what someone else has already said, because I didn't read through the entire discussion thread.我希望我不只是重复别人已经说过的话,因为我没有通读整个讨论线程。

如果安装在您的 linux/unix 风格中的 grep 命令没有 -P 参数,您可以使用:

egrep --color "\b(place_here_what_you_are_looking_for)\b|$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM