How to grep with filename highlighted in one color, and the match highlighted in another color?

Pipe your grep through awk, then have awk color the fields.

Example:

grep -i "stuff" * | awk -F: '{ printf "\033[1;31m" $1 " \033[0m"; for(i=2;i<=NF;i++){out=out":"$i}; print out}'

OS X grep is too outdated: it does not support this.

You need to install a newer version, for example from HomeBrew

brew install grep

By default, the newly installed grep is called ggrep. You can change this behavior by adding the option --with-default-names.

For more information, see https://superuser.com/a/419527.


This is already answered on stackoverflow:

https://stackoverflow.com/questions/17236005/grep-output-with-multiple-colors

but it only works if you can match the filenames using a pattern or several patterns.