8

How can I configure grep so that I could highlight in color smarter? By using different colors?

Problem: grep --color defaults to using red to highlight matches, but it still difficult to read for consecutive runs. If I run grep three times, I would like the first run to use red, the second to use some other color (say, green), the third to use red again, and so on.

I read before there is a hack to swap the color in terminal use every another run, so that output will be clean and easier to read. But I forget what that is; is it zsh or bash-script, or some other shell?

4 Answers 4

3

You could add the following to your .bashrc file:

my_grep() {
  if $GREP_USE_FIRST_COLOR
  then
    export GREP_USE_FIRST_COLOR=false
    export GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
  else
    export GREP_USE_FIRST_COLOR=true
    export GREP_COLORS='ms=01;33:mc=01;33:sl=:cx=:fn=35:ln=32:bn=32:se=36'
  fi
  grep --color=auto "$@"
}
alias grep=my_grep

This will alter the match highlighting color of subsequent grep calls in an interactive shell between bold red (ms=01;31) and bold yellow (ms=01;33).

See the grep manual for more information on how to use GREP_COLORS to set the particular highlighting colors you like.

0

Inspired by answer of @Chriki

By using PROMPT_COMMAND as a hook and update the GREP_COLOR and git config for every prompt.

# .bashrc
# ...
# your alias, git prompt, git completetion, exports... etc
# ...
__grep_swap_color ()
{
  if [ -z "$GREP_COLOR" ]
  then
    export GREP_COLOR='1;32'
    git config --global color.grep.match blue
  else
    export GREP_COLOR=''
    git config --global color.grep.match red
  fi
}
PROMPT_COMMAND=$PROMPT_COMMAND';__grep_swap_color'
# End of file
1
  • 1
    Thanks for letting me inspire you :-) Please let me point you to a few drawbacks of your solution: (1) You won’t get the color alteration whenever you execute another shell command between two grep runs or when you just hit Enter after a grep run; (2) Your solution is a bit expensive in that it runs quite a bit of code for every new prompt; (3) Your bash will complain on each prompt when PROMPT_COMMAND is not set because the prompt command will then start with a ;; (4) GREP_COLOR is deprecated.
    – Chriki
    Nov 19, 2013 at 11:40
0

You are looking for a Simple way to colour alternate output lines in bash.

If the answer seems a bit confusing, follow this.

  1. You have to use a script that automatically does this for you. Open up "terminal". (In many Ubuntu variations and itself, you press Alt+Ctrl+T to invoke that.) I'm assuming you to be using Ubuntu.
  2. Use your favorite editor to open a text file for writing the script. I'm using 'nano'. Like this nano ~/grepc.sh. This opens a file called grepc.sh in my /home (~/).
  3. Paste this bash script into that.

    #!/bin/sh
    while read line
    do
      echo -e '\e[1;31m'"$line"
      read line
      echo -e '\e[1;32m'"$line"
    done
    echo -en '\e[0m'
    
  4. If you're in nano, pressing Ctrl+X will attempt to close nano, but before that it will ask if you want to save the file. Do so, by pressing y on your keyboard.

  5. Now you have to make this script executable. Type the command chmod +x ~/grepc.sh.
  6. That's it. Now use this with grep commands like this: ls ~/ | grep "test" | ./grepc.sh (Assuming the command is executed in /home).
1
  • I think you read my question wrong.
    – Dennis C
    Nov 19, 2013 at 0:48
0

This is an adaptation of Kimvais’ solution (which Unrealist also used).  Create grepc.bash:

while read line
do
     echo "$line"
     read line && echo "$line" | sed $'s/\(\e\[\([0-9]+;\))*\)31m/\\132m/g'
done

This uses sed to change the escape sequence for red to the escape sequence for green in alternate lines of its input.  Use, for example, as

grep --color=always (whatever) (whatever) | (path to the script)/grepc.bash

It seems that grep --color actually uses color only if it’s writing to the terminal (window), and you need to use =always to get it to use color when it’s writing to a pipe.  And BTW, it’s conventional to put personal utilities like this into ~/bin, and then add that to your PATH, so you don’t need to specify it when you run the utility.

1
  • 1
    I was not asking every another line, but every another run
    – Dennis C
    Aug 14, 2014 at 23:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .