Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've written a bunch of scripts to colorize matches, like this:

$ echo abcdef | grepg c
ab[green]c[/green]def
$ echo abcdef | grepr e
abcd[red]e[/red]f

It also works when I chain it:

$ echo abcef | grepr a | grepg b | grepg c | grepp e | grepl f
abcdef   [each letter have it's own style]

Hovewer when I try this for digits, the result is wrong:

$ echo 12345 | grepr 1 | grepg 2 | grepb 3 | grepp 4 | grepl 5
[01;[01;3[01;35m4m31m1[01;[01;3[01;35m4m32m2[01;3[01;35m4m3[01;35m45

because if subsequent greps also match inside the escape sequences produced by earlier greps.

How to do it right? I need either to stop grep matching inside terminal escale sequences or make it match more than one pattern and colorize them to different colours.

share|improve this question
add comment

1 Answer

Take a look at lookkbehind and lookahead in regexes.

Something like

(?<!\[\d\d;\d\d?m)yourregexhere

could work (I'm not sure about the format of the colorizing strings)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.