Colour Highlighting Output Based on Regex in Shell

Colour highlighting output based on regex in shell

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

how to color the output for two different strings in two different files in Bash

You can use diff and set color for changed lines:

diff --old-group-format=$'\e[0;31m%<\e[0m' 
--new-group-format=$'\e[0;31m%>\e[0m'
--unchanged-group-format=$'\e[0;32m%=\e[0m' file1 file2

Highlight grep results in different colors, depending on output

The middle grep here it will find all "=xxxx" and colour the xxxx part in red.

The last grep looks for "localhost" or start of line, and marks them in green. Because "start of line" is not a displayable character, it is not coloured, but because all lines have a "start of line", all of the ones from the middle grep are passed through to the output.

grep -H -i "^mail.hostname" WEB-INF/config/mail.props | GREP_COLOR='01;31' grep --color=always -P '=\K.*' | GREP_COLOR='01;32' grep -E --color=always 'localhost|^'

Thanks to Sundeep for the "grep -P" hint.

And I like the OP's [^=]*$ to match everything after the "=" sign!

Colorized grep -- viewing the entire file with highlighted matches

Here are some ways to do it:

grep --color 'pattern\|$' file
grep --color -E 'pattern|$' file
egrep --color 'pattern|$' file

The | symbol is the OR operator. Either escape it using \ or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the egrep command instead of grep.

The search text "pattern|$" is actually a trick, it will match lines that have pattern OR lines that have an end. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.

To also pass the colored parts through pipes, e.g. towards less, provide the always parameter to --color:

grep --color=always 'pattern\|$' file | less -r
grep --color=always -E 'pattern|$' file | less -r
egrep --color=always 'pattern|$' file | less -r

Grep output with multiple Colors?

You can cascade greps with different colors by specifying --color=always and using the regular expression 'foo|$' to pass all lines.

For example:

tail -f myfwlog | GREP_COLOR='01;36' egrep --color=always 'ssh|$' | GREP_COLOR='01;31' egrep -i --color=always 'drop|deny|$'

If you want the entire line to be highlighted, update your regular expression accordingly:

.... GREP_COLOR='01;31' egrep -i --color=always '^.*drop.*$|^.*deny.*$|$'

How to parse commands and color it?

I'm not sure that you can do this in bash. But it is possible in other shells, e.g. in zsh (see: https://github.com/zsh-users/zsh-syntax-highlighting ) or in fish (see: http://fishshell.com/ ).

grep: highlight only first match of each line when using --color

It can be easily done using sed:

sed 's/hello/\x1b[31m&\x1b[0m/' file

This will only color first match of hello word in each line. In replacement we are putting matched word back using & surrounded with escape code for color red.

Similarly you can do this in awk as well:

awk '{sub(/hello/, "\x1b[31m&\x1b[0m")} 1' file


Related Topics



Leave a reply



Submit