Colour highlighting output based on regex in shell

2019-01-10 08:08发布

问题:

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.

回答1:

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]
}


回答2:

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..."



回答3:

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


回答4:

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

BLUE="$(tput setaf 4)"
BLACK="$(tput sgr0)"
command | sed "s/^ERROR /${BLUE}ERROR ${BLACK}/g"


回答5:

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.



回答6:

Try

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

where DEBUG is the text you want to highlight.



回答7:

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.



回答8:

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.



标签: linux bash shell