FuriousGeorge
FuriousGeorge

Reputation: 4681

Grep with color and multiple excludes

I would like to do a grep to dig through my code hierarchy and look for the term "x", but color the results and exclude annoying terms. Right now I do:

grep -Rn --color x * | grep -v -e html -e svn -e test -e doc -e y

The problem is that this loses the matching color because of the pipe. Is there anyway to make this one statement so that the coloring isn't lost?

Upvotes: 2

Views: 1510

Answers (2)

sampson-chen
sampson-chen

Reputation: 47357

Specify --color=always to preserve color formatting through pipes:

grep --color=always x * | grep -v -e html -e svn -e test -e doc -e y

And later on if you happen to need to pipe the result into a file and need to remove the escape characters that format color, here's a nifty sed script you can pipe your results through to remove the escape charaters:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" 

(Note that you need -E option instead of -r for OS X)

Upvotes: 4

alestanis
alestanis

Reputation: 21863

You can try repeating the color search:

grep -Rn --color x * | grep -v -e html -e svn -e test -e doc -e y | grep --color x

Upvotes: 1

Related Questions