具有多种颜色的 Grep 输出? [英] Grep output with multiple Colors?

查看:25
本文介绍了具有多种颜色的 Grep 输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bash 中是否有一种优雅的方法可以针对具有两个或多个模式的文本文件运行 grep,并且每个匹配的模式都以不同的颜色输出?

Is there an elegant method in bash for running grep against a text file with two or more patterns, and each pattern that matches is output in a different color?

那么匹配 MALEAUGUST 的一行会输出蓝色的 MALE 和橙色的 AUGUST ?我愿意使用 sedawkgrep 和蜡笔或其他.

So a line that matches on MALE and AUGUST would output MALE in blue and AUGUST in orange? I am open to the use of sed, awk, grep and crayons or others.

推荐答案

您可以通过指定 --color=always 并使用正则表达式 'foo|$' 传递所有行来级联具有不同颜色的 grep.

You can cascade greps with different colors by specifying --color=always and using the regular expression 'foo|$' to pass all lines.

例如:

tail -f myfwlog | GREP_COLOR='01;36' egrep --color=always 'ssh|$' | GREP_COLOR='01;31' egrep -i --color=always 'drop|deny|$'

如果您想突出显示整行,请相应地更新您的正则表达式:

If you want the entire line to be highlighted, update your regular expression accordingly:

.... GREP_COLOR='01;31' egrep -i --color=always '^.*drop.*$|^.*deny.*$|$'

这篇关于具有多种颜色的 Grep 输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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