How to add 'color' in a grep?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to add 'color' in a grep?
# 1  
Old 01-08-2019
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?

Code:
 zegrep abcdefg logs/03202018/myfile.log.gz | egrep -i "abcd" | tr "\001" " " | sed G

In this above example, is there a way to add color to abcdefg and abcd?

Thank you
DallasT

Last edited by DallasT; 01-08-2019 at 12:36 PM..
# 2  
Old 01-08-2019
The --color needs to be given to zgrep, not sed.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-08-2019
I'm not sure what is going on for you. Try two separate grep actions (Linux example).
Ex:
grep -o shows ONLY the matching text nothing else on the line
You are looking for 10-JUL in a bunch of logfiles, then you want specific error code ERR 1901a

Code:
grep -f '10-JUL' logfiles*| grep -o 'ERR 1901a'

If you supply us an exact example of your problem we can help you apply this concept, otherwise this is about as good as I can do with what I have. An OS name and a shell name would help improve our answers immensely.
# 4  
Old 01-08-2019
Thank you for replying.

here is the type of grep i used to search two items in a file.

My question is, is there a way to add 'color' to see both search items in output?

Code:
zegrep abcdefg logs/03202018/myfile.log.gz | egrep -i "abcd" | tr "\001" " " | sed G

what can i add in my cmd line to actually see 'abcdefg' and 'abcd' both in color?

oh and the version: Linux version 2.6.32-754.6.3.el6.x86_64
# 5  
Old 01-08-2019
Quote:
Originally Posted by DallasT
Thank you for replying.

here is the type of grep i used to search two items in a file.

My question is, is there a way to add 'color' to see both search items in output?

Code:
zegrep abcdefg logs/03202018/myfile.log.gz | egrep -i "abcd" | tr "\001" " " | sed G

what can i add in my cmd line to actually see 'abcdefg' and 'abcd' both in color?

oh and the version: Linux version 2.6.32-754.6.3.el6.x86_64
As zgrep and egrep are the things doing the matching, do zgrep --color and egrep --color.
# 6  
Old 01-08-2019
By default, color is added / applied to screen output, not when writing to files or pipes, so additional measures need to be taken. With some more detail info (sample data) on the problem, sensible proposals might be givven.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-09-2019
Hi Guys,

Just for reference, here is a little shell script that I sometimes use to check whats available if I want to use colo(u)r.

Code:
#!/bin/bash
##################################################################################
#
# Test script to show the available colours on a monitor, this allows the generation
# of highlighted output from ordinary shell scripts - to be integrated with the
# logger shell to make pretty output.
#
##################################################################################

T='XXX'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done

Feel free to use;

Regards

Gull04
These 2 Users Gave Thanks to gull04 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

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: fgrep -i "XYZ-1124354-P" mylog.log | tr "\001" " " | sed G (7 Replies)
Discussion started by: DallasT
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. UNIX for Dummies Questions & Answers

Grep ruins color

When I run: git branch -a I git a nice green and red colored output, but if I pipe the above command through grep: git branch -a | grep -v "master" I lose my colored output. Is there a way around this? (2 Replies)
Discussion started by: blasto333
2 Replies

5. UNIX for Dummies Questions & Answers

color highlighting with 'more','grep' and 'vi'

Hi all, i would to find out how can i turn on color hightlighting with the 'more' command. When i view a big file, i tend to use the 'more' command and i would search for a interested string with the '/' command. Something the search returns more than 1 line found on the screen, how can i... (0 Replies)
Discussion started by: new2ss
0 Replies
Login or Register to Ask a Question