LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 09-17-2010, 05:28 PM   #1
s7upify
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Rep: Reputation: 0
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?
 
Old 09-17-2010, 08:32 PM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
show some of that output.
 
Old 09-20-2010, 09:59 AM   #3
s7upify
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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 ["
 
Old 09-20-2010, 10:15 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

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

Hope this helps.
 
1 members found this post helpful.
Old 09-20-2010, 01:22 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
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
 
1 members found this post helpful.
Old 09-21-2010, 12:00 PM   #6
s7upify
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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

Last edited by s7upify; 09-21-2010 at 12:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 02:34 PM
Sed, Awk, grep,Search,delete joyds219 Linux - Newbie 6 04-03-2008 07:15 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 01:34 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 03:57 PM


All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration