grep通过彩色文本,例如gcc | colorgcc | grep正则表达式 [英] grep through colored text , e.g. gcc | colorgcc | grep regexp

查看:107
本文介绍了grep通过彩色文本,例如gcc | colorgcc | grep正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在greipe管道输出时让grep尊重ANSI颜色转义?
我很乐意使用别的东西(perl?)而不是grep。

How do I make grep respect ANSI color escapes when grepping piped output ? I am happy to use something else (perl?) instead of grep.

我的用户名:我要

 gcc foobar.c | colorgcc | grep regexp
 ls --color | grep filename

可以很好地处理颜色(在使用ANSI转义的unix终端上)。

work nicely with colors (on a unix terminal using ANSI escapes).

我想要的行为的测试示例:

The test examples of behaviour I want :

echo -e "he\e[35mllo\e[00m" world |grep hell ==> he\e[35mllo\e[00m world 
echo -e "\e[35m removed line\nhello\e[00m" world |grep hell ==> \e[35mhello\e[00m world
echo -e "\e[35m rem\e[1moved line\nhello\e[00m" world | grep hell ==> \e35m\e1mhello\e[00m world

目前第一行给出空字符串,和第二个给uncolorised字符串
'hello \e [00m世界'。这里\e [35m和\e00m是颜色(属性)修饰符:一个字母的颜色由形式\ e [P1; P2; ..其中P1,P2等是数字序列; \e [P1m \e [P2m等同于\e [P1; P2m。 \e [0m使颜色默认并忘记所有以前的\ e [.. m序列:\ e [34m \e [0m相当于\ee [0m。有几个独立的属性(粗体,背景的颜色,前景/字母的颜色);转义序列中的每个数字只影响其中的一个。因此\\ \\ [35] [35米等同于\\ [35米,但不等于35米;] [35米;然而,[34m] [35m等同于[35m,因为它们都影响相同的attirbute(即字母/ foregrnound的颜色)。

Currently the first line gives the empty string, and the second one gives uncolorised string 'hello\e[00m world'. Here \e[35m and \e00m are color (attribute) modifierds: the colour of a letter is determined by the last few color (attribute) escape sequences of form \e[P1; P2; .. m where P1,P2, etc are sequence of digits; \e[P1m\e[P2m is equivalent to \e[P1;P2m . \e[0m makes the color default and forgets all the previous \e[..m sequences: \e[34m\e[0m is equivalent to \e[0m. There are several independent attributes (boldness, colour of the background, colour of foreground/letter); each number in an escape sequence affects only one of them. Thus \e[1m\e[35m is equivalent to \e[1;35m but not \e[35;1m nor \e[35m ; however, \e[34m\e[35m are equivalent to \e[35m because they both affect the same attirbute (namely, color of the letter/foregrnound).

推荐答案

这是一个非常有趣的问题,这里是我想出的。这是非常丑陋的,但它似乎完成了工作:

This is a really interesting problem, here is what I came up with. It is pretty ugly but it seems to get the job done:

sed -n '1s/^/\x1b[0m/;H;x;s/\n//;p;s/.*\(\x1b\[[0-9]*m\(;[0-9]*m\)*\).*/\1/;h' |
  grep `sed 's/./\0\\\\(\x1b\\\\[[0-9]*m\\\\(;[0-9]*m\\\\)*\\\\)*/g' <<< hell`

您正在寻找的术语将被置于最后(代替地狱),下面是你提供的文本(使用hexdump显示颜色)的一些例子:

The term you are searching for would be placed at the very end (in place of "hell"), here are a few examples with the text you provided (using hexdump to show colors):

$ echo -e "he\e[35mllo\e[00m" world |
> sed -n '1s/^/\x1b[0m/;H;x;s/\n//;p;s/.*\(\x1b\[[0-9]*m\(;[0-9]*m\)*\).*/\1/;h' |
> grep `sed 's/./\0\\\\(\x1b\\\\[[0-9]*m\\\\(;[0-9]*m\\\\)*\\\\)*/g' <<< hell` |
> hexdump -C
00000000  1b 5b 30 6d 68 65 1b 5b  33 35 6d 6c 6c 6f 1b 5b  |.[0mhe.[35mllo.[|
00000010  30 30 6d 20 77 6f 72 6c  64 0a                    |00m world.|
0000001a

$ echo -e "\e[35m removed line\nhello\e[00m" world |
> sed -n '1s/^/\x1b[0m/;H;x;s/\n//;p;s/.*\(\x1b\[[0-9]*m\(;[0-9]*m\)*\).*/\1/;h' |
> grep `sed 's/./\0\\\\(\x1b\\\\[[0-9]*m\\\\(;[0-9]*m\\\\)*\\\\)*/g' <<< hell` |
> hexdump -C
00000000  1b 5b 33 35 6d 68 65 6c  6c 6f 1b 5b 30 30 6d 20  |.[35mhello.[00m |
00000010  77 6f 72 6c 64 0a                                 |world.|
00000016

第一个sed命令将当前颜色设置预先设置到每行的开头,这对你的第二个例子来说是必要的,其中颜色设置在grep将跳过的一行上。作为grep参数的sed命令会插入一个正则表达式,它将匹配搜索词中每个字符之间的任意数量的颜色转义。

The first sed command prepends the current color setting to the beginning of each line, which is necessary for your second example where the color is set on a line that grep will skip. The sed command that is the argument to grep inserts a regex that will match any number of color escapes between each character in the search term.

以下是egrep版本:

Here is the egrep version:

sed -n '1s/^/\x1b[0m/;H;x;s/\n//;p;s/.*\(\x1b\[[0-9]*m\(;[0-9]*m\)*\).*/\1/;h' |
  egrep `sed 's/./\0(\x1b\\\\[[0-9]*m(;[0-9]*m)*)*/g' <<< hell`

这篇关于grep通过彩色文本,例如gcc | colorgcc | grep正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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