2

I have some output of my program: # php check.php | grep -E "rule_1|rule_4"

I would like results equaling to rule_1 to be red and results equaling to rule_4 to be blue.

Is this possible, because at this moment, everything is colored red.

Lucas Kauffman
  • 16,880
  • 9
  • 58
  • 93
user1081056
  • 123
  • 4

4 Answers4

1

Try this:

alias egrep="grep -E --color=never"

alias egrep-grey="   GREP_COLOR='1;30' grep -E --color=always"
alias egrep-red="    GREP_COLOR='1;31' grep -E --color=always"
alias egrep-green="  GREP_COLOR='1;32' grep -E --color=always"
alias egrep-yellow=" GREP_COLOR='1;33' grep -E --color=always"
alias egrep-blue="   GREP_COLOR='1;34' grep -E --color=always"
alias egrep-magenta="GREP_COLOR='1;35' grep -E --color=always"
alias egrep-cyan="   GREP_COLOR='1;36' grep -E --color=always"
alias egrep-white="  GREP_COLOR='1;37' grep -E --color=always"

php check.php | egrep "rule_1|rule_4" | egrep-red "rule_1|$" | egrep-blue "rule_4|$"
  • Hello and welcome on serverfault. Please take a look at this: https://serverfault.com/help/how-to-answer – Marco Sep 29 '17 at 15:21
0

You cannot use grep itself as a generic colorizing tool. Something like colortail may do what you want.

larsks
  • 43,623
  • 14
  • 121
  • 180
0

You can do this with a regex parser like flex. But keep in mind that some patterns can be interpreted in unintuitive ways (e.g. if your parser is matching the longest pattern vs the shortest). For example the pattern ..1|aaa1 and the input aaaa1 will usually match ..1.

Mircea Vutcovici
  • 17,619
  • 4
  • 56
  • 83
0

Check out colorize.pl, which will print different colors for user-defined strings.

Here is a description from the colorize.pl project:

Colorize.pl is a short script that reads from stdin and writes to stdout. Rows that match a user's search strings will be colorized with user-defined colors. Command line options are available. Colorization is done via ANSI escape codes.

On my Apple MacBook Pro, the following will print 'login' in read, and 'apple' in green:

tail /var/log/system.log | colorize.pl +:login +:apple
Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186