LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using grep / awk to search for coloured text (http://www.linuxquestions.org/questions/linux-general-1/using-grep-awk-to-search-for-coloured-text-832903/)

s7upify 09-17-2010 05:28 PM

Using grep / awk to search for coloured text
 
Hi there.

I have done a bunch of searches on this but the terms seem to get tangled in the more popular search of "colouring the output of grep / awk".

I am trying to find a way to grep/awk through the output of a command to find text of a specific colour. The command's output has a range of colours signifying too many different things to specify using text, with colour being the only form of grouping.

What do you think?

kurumi 09-17-2010 08:32 PM

show some of that output.

s7upify 09-20-2010 09:59 AM

I can't get the output right now to post sorry. For the most part it is white text on black background, but each line has a status section that is either green, blue, or red text on black background. The problem is that the coloured part has a very large number of variants for the text itself so I cannot search for the characters. It is practical for me only to search for the colouring.
I'm trying something like this

runit | grep \033[032

But it usually says something like "unmatched ["

druuna 09-20-2010 10:15 AM

Hi,

The [ is special, it needs to be escaped: grep '\033\[032'

Hope this helps.

Kenhelm 09-20-2010 01:22 PM

Commands which can give output to a terminal in colour (e.g. ls or grep) usually only use colour escape codes when they are outputting to a terminal. This means that you can't directly grep for the escape codes because they are not there.
If you are working in a terminal rather than scripting, a way around this is to use the 'script' command to make a record of your command's terminal output. The resulting file contains the escape codes.
http://linux.die.net/man/1/script

To use 'script':
Type 'script' into a terminal.
There should be a message
'Script started, file is typescript'
Execute your command so that it gives the coloured output to the terminal.
Use CTRL-D to exit 'script', getting this message
'Script done, file is typescript'

Then grep the file 'typescript' for the colour escape codes.

My version of GNU grep doesn't support '\033' in basic or extended regular expressions.
To grep for bold green in the output of 'ls' I use this:
grep '\[01;32m' typescript

'\033' works with Perl regular expressions:
grep -P '\033\[01;32m' typescript

s7upify 09-21-2010 12:00 PM

Ok awesome guys I combined what you said for a solution.
I ran script with the option -c to run the command, then grep'd through the file using an escaped [ and the color code.

For example, to search through the output of the command "runit" for any red foreground text (31):

script -c "runit" out.tmp
grep -P '\[31m' out.tmp
if [[ $? -eq 0 ]];then
# FOUND
else
# NOT FOUND
fi
rm out.tmp


All times are GMT -5. The time now is 09:40 AM.