Say I have output from a command that is colorized for the terminal. I want to match any line that contains the color yellow. How can I do this in grep, eg: mycommand -itSomtimesPrintsLinesWithYellowColorCodes | grep -e "?????"
Note: This is NOT about colorizing the output of grep, or adding any colors. It's only about how to filter/match colored of input coming into grep.
Best Answer
Let's use
tput
to generate the color code for your terminal for yellow and black:Let's examine what the yellow code actually includes:
Now, we can use
grep
to search for the yellow color code and print the string that matches from the beginning of the yellow code to the next code, whatever that code is:Note that the color code for yellow includes
[
whichgrep
considers to be a regex active character. Thus, to match a literal[
, we need to escape it forgrep
. To do this, we use bash's$'...'
to define the color code with [` escaped.It is likely that there is more than one code to produce yellow on your terminal. You will want to examine the output of whatever command you are using to determine what codes are being used and include them in your
grep
command.