grep color

When you move away from commercial UNIX to Linux, some goodies are just fun, even if they are simple and old.

Let’s look at grep. By default, the matched pattern is red. But the color could be changed. Some magic regexp could be used to get more than one color


$ tnsping DB01 |
   egrep '^TNS-[0-9]*'
TNS-03505: Failed to resolve name

The color could be changed to green

$ tnsping DB02 |
   GREP_COLORS="ms=1;32" egrep OK
OK (10 msec)

Now I want to get both, RED and GREEN, so I need to grep for “OK” and “TNS” and apply a different color. Pattern ‘OK|^’ returns always true but only OK will be highlighted

$ tnsping DB01 |
   egrep 'OK|TNS-'|
   GREP_COLORS="ms=1;32" egrep --color=always 'OK|^'|
   egrep 'TNS-[0-9]+|^'
TNS-03505: Failed to resolve name
$ tnsping DB02 |
   egrep 'OK|TNS-'|
   GREP_COLORS="ms=1;32" egrep --color=always 'OK|^'|
   egrep 'TNS-[0-9]+|^'
OK (10 msec)