How to add '--color' with pipes?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to add '--color' with pipes?
# 1  
Old 05-26-2014
How to add '--color' with pipes?

Hi guys - I was wondering if there is a way to add 'color' to a grep I do like this below:

Code:
fgrep -i "XYZ-1124354-P" mylog.log | tr "\001" " " | sed G

# 2  
Old 05-26-2014
if you're using GNU-ed fgrep, man fgrep yields:
Code:
       --color[=WHEN], --colour[=WHEN]
              Surround  the matched (non-empty) strings, matching lines, context lines, file names,
              line numbers, byte offsets, and separators (for fields and groups of  context  lines)
              with  escape  sequences  to  display  them  in color on the terminal.  The colors are
              defined by the environment variable GREP_COLORS.  The deprecated environment variable
              GREP_COLOR  is  still  supported,  but  its  setting does not have priority.  WHEN is
              never, always, or auto.

# 3  
Old 05-27-2014
Thanks for posting MAN output.

Can anyone provide an example from my above question, it would be best way to understand.

Thanks
# 4  
Old 05-27-2014
vgersh99 did not just post man output, he posted the answer. What part of it did you not understand?
Did you even make an attempt.to put that man quote into practice? If so, show us the code and the error.

Regards,
Alister
# 5  
Old 05-28-2014
Read my response again:

Quote:
Can anyone provide an example from my above question, it would be best way to understand.
With that being said, to answer you: Yes this is how I tried (see below).

Now, since I couldnt properly interpret the 'man output' that was pasted here, an example was requested so that I could understand better.

Code:
fgrep -i "XYZ-1124354-P" mylog.log | tr "\001" " " | sed G --color
 
&
 
fgrep -i "XYZ-1124354-P" mylog.log | tr "\001" " " | sed G | --color

There is no "error" that comes from using above examples. It just shows no color.

Thank You

Last edited by DallasT; 05-28-2014 at 02:53 PM..
# 6  
Old 05-28-2014
Code:
fgrep -i "XYZ-1124354-P" --color mylog.log

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 05-28-2014
When you are shown that fgrep on some systems has a --color option (as in the fgrep man page excerpt vgersh99 provided), the way to get color output from fgrep would be:
Code:
fgrep --color -i "XYZ-1124354-P" mylog.log

but, you haven't told us what system you're using, so we don't know if fgrep on your system supports the --color option.

Furthermore, many utilities that have color options use the file descriptor associated with the utility's standard output to determine terminal characteristics to determine what escape sequences are needed to produce color on the terminal you're using. If your system's grep family of utilities supports a --color option and uses stdout to determine terminal type, piping the output from fgrep into another utility will disable color output. If your fgrep can't produce color output when stdout is connected to a pipe, you could still translate ASCII soh control characters to spaces using:
Code:
tr '\001' ' ' < mylog.log | fgrep --color -i 'XYZ-1124354-P'

but using sed G to produce double spaced output will be more complex:
Code:
fgrep -i 'XYZ-1124354-P' mylog.log | tr '\001' ' ' | sed G | grep --color -i -e 'XYZ-1124354-P' -e '^$'

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add 'color' in a grep?

Hello friends - I use various grep commands to search for data in a file. However, to add 'color' seems to not work. Is there a way to add color to two items that i search? so that i can easily identify in hundreds of lines of output what i am looking for? zegrep abcdefg... (7 Replies)
Discussion started by: DallasT
7 Replies

2. Shell Programming and Scripting

Add Color To html Doc

I have a script which converts a .csv file to html nicely. Trying to add 3 colors, green, yellow and red to the output depending upon the values in the cells. Tried some printf command but just can't seem to get any where. Any ideas would be appreciated. nawk 'BEGIN{ FS="," print ... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

Add color in CSV cells

hi, i have text file that file contains below information. Name,Roll,Mark,Total Sivasankar,2120,89,410 Raja,2212,87,425 i need to convert text file to CSV file also the heading(Name,Roll,Mark,Total) font should be BOLD and color should be RED. how can i set fonts in csv (5 Replies)
Discussion started by: rsivasan
5 Replies

4. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question