Using grep / awk to search for coloured text
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?
show some of that output.
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 ["
The [ is special, it needs to be escaped: grep '\033\[032'
Hope this helps.
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.
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
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
# NOT FOUND
|All times are GMT -5. The time now is 01:33 PM.|