bash 具有多种颜色的 Grep 输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17236005/
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-09 23:52:03  来源:igfitidea点击:

Grep output with multiple Colors?

bashcolorssedawkgrep

提问by Evil Genius

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

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

So a line that matches on MALEand AUGUSTwould output MALEin blue and AUGUSTin orange? I am open to the use of sed, awk, grepand crayons or others.

那么匹配MALE并以蓝色和橙色AUGUST输出的行?我打开使用,,和蜡笔或其他人。MALEAUGUSTsedawkgrep

回答by Rob Windsor

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

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

For example:

例如:

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.*$|$'

回答by chepner

grepis a regular expression matcher, not a syntax highlighter :). You'll have to use multiple invocations of grep, using a different value of GREP_COLORfor each.

grep是正则表达式匹配器,而不是语法高亮器:)。您必须多次调用grep,并GREP_COLOR为每个调用使用不同的值。

GREP_COLOR="1;32" grep foo file.txt | GREP_COLOR="1;36" grep bar

That would highlight "foo" and "bar" in different colors in lines that match both. I don't think there is a (simple) way to handle alloccurrences of either pattern, short of merging the output stream of two independent calls:

这将在匹配两者的行中以不同颜色突出显示“foo”和“bar”。我认为除了合并两个独立调用的输出流之外,没有一种(简单的)方法来处理任一模式的所有出现:

{ GREP_COLOR="1;32" grep foo file.txt
  GREP_COLOR="1;36" grep bar file.txt
} | ...

which will obviously look different than if there were a way to assign a separate color to each regular expression.

如果有一种方法可以为每个正则表达式分配单独的颜色,这显然会有所不同。



You can use awkto substitute each match with itself wrapped in the correct control code.

您可以使用awk包装在正确控制代码中的自身替换每个匹配项。

 echo "foo bar" | awk '{ gsub("bar", "3[1;33m&3[0m");
                         gsub("foo", "3[1;36m&3[0m"); print }'

In each line, you globally replace anything matching the given regular expression with itself (&) wrapped in the ANSI escape sequences for desired color (which grep --colordoes for you). After processing all of the possible matches, you need to explicitly print the line.

在每一行中,您将与给定正则表达式匹配的任何内容全局替换为自己 ( &) 包裹在所需颜色的 ANSI 转义序列中(这grep --color对您有用)。处理完所有可能的匹配项后,您需要显式打印该行。

回答by Ehtesh Choudhury

If you want something out of the box, you're probably looking for hhighlighter.

如果您想要开箱即用的东西,您可能正在寻找hhighlighter

Here's an example:JBoss output

下面是一个例子:JBoss 输出

Take a look. It's incredibly useful for coloring words in different colors automatically. It's an impressive project that's built on top of ack.

看一看。它对于自动为不同颜色的单词着色非常有用。这是一个令人印象深刻的项目,它建立在ack之上。

回答by Bush

I had the need to highlight strings in text files, and did not find a way that suited my needs, so I wrote a simple C program to colorize strings matching regular expressions.

我需要突出显示文本文件中的字符串,但没有找到适合我需要的方法,所以我编写了一个简单的 C 程序来为匹配正则表达式的字符串着色。

You can download the source code from GitHub :

您可以从 GitHub 下载源代码:

git clone http://github.com/mbornet-hl/hl

then :

然后 :

cd hl/cr; make

Here is the usage :

这是用法:

hl: version 1.21
Usage: hl [-h|-eidD][-E][-rgybmcwRGYBMCW] regexp ...
  -h : help
  -v : version
  -u : do not bufferize output on stdout
  -e : extended regular expressions
  -i : ignore case
  -E : print on stderr
  -r : red
  -g : green
  -y : yellow
  -b : blue
  -m : magenta
  -c : cyan
  -w : white
  -R : red     (reverse video)
  -G : green   (reverse video)
  -Y : yellow  (reverse video)
  -B : blue    (reverse video)
  -M : magenta (reverse video)
  -C : cyan    (reverse video)
  -W : white   (reverse video)
  -d : debug
  -D : display regular expressions

To colorize the word "red" in red, and the word "blue" in blue, you just have to type in the following command :

要将单词“red”着色为红色,将单词“blue”着色为蓝色,您只需输入以下命令:

echo "red blue red blue" | hl -r red -b blue

and here is an example to highlight the output of the ifconfig command :

这是一个突出显示 ifconfig 命令输出的示例:

hl -ei -m '^(eth|(vir)?br|vnet)[0-9.]*:[0-9]+\>'             \
       -b '^(eth|(vir)?br|vnet)[0-9.]*\.[0-9]+\>'            \
       -c '([0-9a-f]{2}:){5}[0-9a-f]{2}'                     \
       -g '\<UP\>|\<RUNNING\>|([0-9]{1,3}\.){3}[0-9]{1,3}\>' \
       -y '^(eth|(vir)?br|vnet)[0-9.:]*\>'                   \
       -W '[0-9a-f]{4}::[0-9a-f]{4}\:[0-9a-f]{4}:[0-9a-f]{4}:[0-9a-f]{4}' \
       -r ' (errors|dropped|overruns):[^0][0-9]*'

The order in which you specify the colors is important : if a string matches several regular expressions, the 1st one as a higher priority than the 2nd one, the 2nd one as a higher priority than the 3rd one, and so on ...

指定颜色的顺序很重要:如果一个字符串匹配多个正则表达式,第一个的优先级高于第二个,第二个的优先级高于第三个,依此类推......

Hope This Helps.

希望这可以帮助。

回答by cray

Try the wonderful rpen http://github.com/rtulke/rpen

尝试美妙的 rpen http://github.com/rtulke/rpen

require egrep or grep

Now you need to copy the rpen.pyfile into the right folder and give it appropraite rights

现在您需要将rpen.py文件复制到正确的文件夹中并赋予它适当的权限

cp rpen.py /usr/local/bin/rpen
chmod a+rx /usr/local/bin/rpen

then try

然后尝试

ps xau |rpen Ss "S\+" "\?\?" 

回答by mikeymouse

So here's the version(s) using sed. Let's say you want the word "FAILED" to be colored red, that would be:

所以这是使用 sed 的版本。假设您希望将“失败”一词涂成红色,那就是:

sed 's/\(ERROR\)/\o033[31m\o033[39m/'

To have the whole line containing the word "FAILED" marked in red:

要将包含“FAILED”一词的整行标记为红色:

sed 's/\(.*ERROR.*\)/\o033[31m\o033[39m/'

To have multiple words marked red:

要将多个单词标记为红色:

sed 's/\(ERROR\|FAILED\)/\o033[31m\o033[39m/'

To have multiple words and their wohle lines marked in red:

将多个单词及其整行标记为红色:

sed 's/\(.*FAILED.*\|.*ERROR.*\)/\o033[31m\o033[39m/'

To have multiple colors (ERROR=red/FAILED=blue)

有多种颜色(错误=红色/失败=蓝色)

sed -e 's/\(ERROR\)/\o033[31m\o033[39m/' -e 's/\(FAILED\)/\o033[34m\o033[39m/'

To use all this over ssh:

要通过 ssh 使用所有这些:

ssh user@host -t "tail -n 1024 -f /some/log/file | sed --unbuffered -e 's/\(.*ERROR.*\|.*FAILED.*\)/\o033[31m\o033[39m/' -e 's/\(.*postfix.*\|.*dhcpd.*\)/\o033[32m\o033[39m/'"

And for not having to type this everytime, just declare it as a function in your bashrc.

而且不必每次都输入,只需在 bashrc 中将其声明为函数即可。

For other colors look up ANSI codes.

对于其他颜色,请查找 ANSI 代码。

回答by treulz

cxpgrepis right for this purpose - highlight patterns in different colors.

cxpgrep适合此目的 - 以不同颜色突出显示图案。

cxpgrep 'PAT1|PAT2|...' FILE_OR_DIRECTORY

回答by Ramin Ismayilli

My answer is below:

我的回答如下:

tailf logfile awk '/MALE/' {print "3[34m" 
echo "you make it" > tmp
echo "you take it" >> tmp
echo "you cake it" >> tmp
echo "you bake it" >> tmp
echo "you wake it" >> tmp

GREP_COLOR='1;32' grep -P "(take|cake|bake)" --color=always tmp | GREP_COLOR='1;33' grep -P "(cake|bake)" --color=always -A10000 -B10000 | GREP_COLOR='1;34' grep -P "(bake)" --color=always -A10000 -B10000 
"3[39m"} '/MALE/' {print "3[33m" ##代码## "3[39m"}

回答by user22097

How about this? You can do it with grep ONLY! Tricks here are (1) you take /OR/ of the words you are searching; (2) you use -A and -B options with large enough numbers (larger then the line count of your file).

这个怎么样?您只能使用 grep 来完成!这里的技巧是 (1) 你对你正在搜索的词进行 /OR/; (2) 您使用 -A 和 -B 选项具有足够大的数字(大于文件的行数)。

##代码##