Bash终端输出-突出显示包含某些文本的行 [英] Bash terminal output - highlight lines containing some text

查看:70
本文介绍了Bash终端输出-突出显示包含某些文本的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用bash输出时,我得到标准的2色屏幕.默认情况下,如果其中包含一些关键文本输出,有什么办法可以使我突出显示一行?

When I get output in bash I get my standard 2 colour screen. Is there any way I can, by default, highlight a line if it contains some key text output?

例如如果它包含单词"FAIL",则该行将显示为红色.

E.g. if it contains the word "FAIL" then the line is coloured red.

我已阅读此 https://unix.stackexchange.com/questions/46562/how-do-you-colorize-only-some-keywords-for-a-bash-script 但是与寻找包装脚本相比,我正在寻找更简单的方法,而在未来的某个时候,我将不可避免地要调试该包装脚本.

I’ve read this https://unix.stackexchange.com/questions/46562/how-do-you-colorize-only-some-keywords-for-a-bash-script but am looking for something simpler than having to write a wrapper script which I’d inevitably have to debug at some time in the future.

推荐答案

一个简单的解决方法是,将其通过grep --color传递给某些单词,以使其变为红色.

For a simple workaround, pipe it through grep --color to turn some words red.

添加类似^的后备选项以打印不包含任何匹配项的行.

Add a fallback like ^ to print lines which do not contain any matches otherwise.

grep --color -e 'FAIL' -e '^' <<<$'Foo\nBar FAIL Baz\nIck'

具有多种颜色的Grep输出?描述了一种黑客,如果您要获取多种颜色需要那个.

Grep output with multiple Colors? describes a hack for getting multiple colors if you need that.

这篇关于Bash终端输出-突出显示包含某些文本的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆