Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve colouring after piping grep to grep

People also ask

How can you preserve colors in stdout while piping to tee?

Simply insert unbuffer before any command to make it think it is writing to an interactive output even if it is actually piping into another executable. This will preserve color in the case of ls .

Can you pipe to grep?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

Does grep return a string?

The grep command searches a text file based on a series of options and search string and returns the lines of the text file which contain the matching search string.


grep sometimes disables the color output, for example when writing to a pipe. You can override this behavior with grep --color=always

The correct command line would be

grep --color=always WORD * | grep -v AVOID

This is pretty verbose, alternatively you can just add the line

alias cgrep="grep --color=always"

to your .bashrc for example and use cgrep as the colored grep. When redefining grep you might run into trouble with scripts which rely on specific output of grep and don't like ascii escape code.


A word of advice:

When using grep --color=always, the actual strings being passed on to the next pipe will be changed. This can lead to the following situation:

$ grep --color=always -e '1' * | grep -ve '12'
11
12
13

Even though the option -ve '12' should exclude the middle line, it will not because there are color characters between 1 and 2.


The existing answers only address the case when the FIRST command is grep (as asked by the OP, but this problem arises in other situations too).

More general answer

The basic problem is that the command BEFORE | grep, tries to be "smart" by disabling color when it realizes the output is going to a pipe. This is usually what you want so that ANSI escape codes don't interfere with your downstream program.

But if you want colorized output emanating from earlier commands, you need to force color codes to be produced regardless of the output sink. The forcing mechanism is program-specific.

Git: use -c color.status=always

git -c color.status=always status | grep -v .DS_Store

Note: the -c option must come BEFORE the subcommand status.

Others

(this is a community wiki post so feel free to add yours)


Simply repeat the same grep command at the end of your pipe.
grep WORD * | grep -v AVOID | grep -v AVOID2 | grep WORD