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

bashcommand linemacos

On the Mac, I can't seem to use grep and have the filename highlighted (or in) a certain color, and have the matching string in a different color. Is there an easy way?

Best Answer

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