monk
monk

Reputation: 2115

How to highlight certain words over terminal always from all commands

I need to highlight certain keywords like "fail, failed, error, fatal, missing" over my terminal.

I need this with the output of ALL the commands, not any specific command. I assume I need to tweak my bashrc file for this.

To color I can use:

<input coming to terminal>|grep -P --color=auto 'fail|failed|error|fatal|missing|$'

I tried the following command but not helped:

tail -f $(tty) |grep -P --color=auto 'fail|failed|error|fatal|missing|$' &
[1]+  Stopped(SIGTTIN)        tail -f $(tty) | grep -P --color=auto 'fail|failed|error|fatal|missing|$' 

Searched SO for answers but could not find any question which provides desired an answer.

Upvotes: 1

Views: 1530

Answers (1)

Kevin Boone
Kevin Boone

Reputation: 4307

I don't think there's really an elegant way to do this using the shell. Ideally, you'd get a terminal emulator with this kind a keyword highlighting built in. You can get some of the way by piping the output of bash through a filter that adds ANSI colour escapes. Here is a sed script, that replaces "fail" with (red)fail(normal):

s/fail/\x1B[31m&\x1B[0m/
t done

:done

Run bash with its output piped through sed like this:

$bash | sed -f color.sed

This mechanism is not without problems, but it works in some cases. Usually it's better just to collect up the output you want, and then pipe it through sed, rather than working directly with the bash output.

Upvotes: 2

Related Questions