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

The --color needs to be given to zgrep, not sed.

1 Like

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

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?

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.

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.

1 Like

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.

#!/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

2 Likes

In addition to post 6, I've always noticed this and have to add an extra grep to the end of lines pipe connected sequences (probably badly written :o) to get the output I want highlighted. I usually use use an expression that matches every line too, so in your case I would append the following:-

........ | egrep --color -Ei "|abcd"

... so I'm matching your string abcd or a null (every line, but no character) to get all the output and colour the important characters.

You might be able to remove your existing grep -Ei 'abcd' and just use the above at the end, but it depends if that might leave too many records to be processed by your tr. By 'too many', I'm thinking of over 100,000 unneeded records, such a volume that might slow your overall pipeline.

I hope that this helps,
Robin