0

I need a command that colorizes some symbol sequence in output of a certain command. I'd like it like grep make it.

$ some-command | grep symbol-sequence

However grep removed lines that isn't contained symbol-sequence. I'd like to have all original lines with highlighted symbol-sequence. How it can be done?


I have alias grep='grep --color=auto' in ~/.bashrc

Loom
  • 3,953

1 Answers1

0

You can do that using the ANSI control characters:

some-command | sed 's/symbol-sequence/^[[0;32;2m symbol-sequence^[[0;0m/'

The characters ^[ are not a regular ^ followed by a regular [. It is an unprintable character. You can get it on most terminal by typing Ctrl-v Ctrl-Esc. The second [ is a real [ though.

If you prefer full-ascii, here is a copy-pastable version which has the same effect:

some-command | sed 's/symbol-sequence/\x1B[0;32;2m symbol-sequence\x1B[0;0m/'

You can change the "32" and the "2" in the above command to change the colors and style in the output. Look for "ANSI escape sequences" in any search engine for details and color codes.

lgeorget
  • 13,914