I'm on OSX, but I suspect this doesn't make a big difference for this question.
In my .bash_profile
, I aliased grep
to get color outputs by default:
alias grep='grep --color=always'
I commonly run searches for content within files in my repositories in ways similar to:
grep --include=*.cpp -Ern . -e "(foo|bar)"
but I often want to further refine the results, typically piping with say grep -v "colorbar"
.
The problem is that the second grep
command then runs on the colored ouput, and doesn't seem to be able to match the exclusion patterns because of this.
Obviously I could run the first search without colors and then everything would work fine, but I would prefer to keep them if there is a way to work around this?
grep --include=*.cpp -Ern . -e "(foo|bar)" | sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | grep -v "colorbar"
And it preserves the colors too! :) Should I close the OP then?