Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .