Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

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.

share|improve this answer

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
share|improve this answer
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 '13 at 11:40

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).
share|improve this answer
    
I think you read my question wrong. –  Dennis Cheung Nov 19 '13 at 0:48

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.

share|improve this answer
    
I was not asking every another line, but every another run –  Dennis Cheung Aug 14 at 23:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.