Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colour highlighting output based on regex in shell

Tags:

linux

bash

shell

I'd like to know if I can colour highlight the output of a shell command that matches certain strings.

For example, if I run myCommand, with the output below:

> myCommand
DEBUG foo bar
INFO bla bla
ERROR yak yak

I'd like all lines matching ^ERROR\s.* to be highlighted red.

Similarly, I'd like the same highlighting to be applied to the output of grep, less etc...

EDIT: I probably should mention that ideally I'd like to enable this feature globally via a 'profile' option in my .bashrc.

like image 215
Joel Avatar asked Nov 24 '10 13:11

Joel


8 Answers

There is an answer in superuser.com:

your-command | grep -E --color 'pattern|$'

or

your-command | grep --color 'pattern\|$'

This will "match your pattern or the end-of-line on each line. Only the pattern is highlighted..."

like image 59
E.Z. Avatar answered Oct 05 '22 12:10

E.Z.


You can use programs such as:

  • spc (Supercat)
  • grc (Generic Colouriser)
  • highlight
  • histring
  • pygmentize
  • grep --color

You can do something like this, but the commands won't see a tty (some will refuse to run or behave differently or do weird things):

exec > >(histring -fEi error)    # Bash
like image 44
Dennis Williamson Avatar answered Oct 05 '22 11:10

Dennis Williamson


If you want to enable this globally, you'll want a terminal feature, not a process that you pipe output into, because a pipe would be disruptive to some command (two problems are that stdout and stderr would appear out-of-order and buffered, and that some commands just behave differently when outputting to a terminal).

I don't know of any “conventional” terminal with this feature. It's easily done in Emacs, in a term buffer: configure font-lock-keywords for term-mode.

However, you should think carefully whether you really want that feature all the time. What if the command has its own colors (e.g. grep --color, ls --color)? Maybe it would be better to define a short alias to a colorizer command and run myCommand 2>&1|c when you want to colorize myCommand's output. You could also alias some specific always-colorize commands.

Note that the return status of a pipeline is its last command, so if you run myCommand | c, you'll get the status of c, not myCommand. Here's a bash wrapper that avoids this problem, which you can use as w myCommand:

w () {
  "$@" | c
  return $PIPESTATUS[0]
}
like image 29
Gilles 'SO- stop being evil' Avatar answered Oct 05 '22 11:10

Gilles 'SO- stop being evil'


You could try (maybe needs a bit more escaping):

BLUE="$(tput setaf 4)"
BLACK="$(tput sgr0)"
command | sed "s/^ERROR /${BLUE}ERROR ${BLACK}/g"
like image 40
Benoit Avatar answered Oct 05 '22 13:10

Benoit


Try

tail -f yourfile.log | egrep --color 'DEBUG|'

where DEBUG is the text you want to highlight.

like image 34
userM1433372 Avatar answered Oct 05 '22 12:10

userM1433372


You can use the hl command avalaible on github :
git clone http://github.com/mbornet-hl/hl

Then :
myCommand | hl -r '^ERROR.*'

You can use the $HOME/.hl.cfg configuration file to simplify the command line.
hl is written in C (source is available). You can use up to 42 differents colors of text.

like image 44
Bush Avatar answered Oct 05 '22 13:10

Bush


Use awk.

 COLORIZE_AWK_COMMAND='{ print $0 }'
 if [ -n "$COLORIZE" ]; then
     COLORIZE_AWK_COMMAND='
     /pattern1/ { printf "\033[1;30m" }
     /pattern2/ { printf "\033[1;31m" }
     // { print $0 "\033[0m"; }'
 fi

then later you can pipe your output

... | awk "$COLORIZE_AWK_COMMAND"

printf is used in the patterns so we don't print a newline, just set the color.

like image 27
skensell Avatar answered Oct 05 '22 13:10

skensell


You could probably enable it for specific commands using aliases and user defined shell functions wihtout too much trouble. If your coloring errors I assume you want to process stderr. Since stderr in unbuffered you would probably want to line buffer it by sending through a fifo.

like image 39
dietbuddha Avatar answered Oct 05 '22 12:10

dietbuddha