Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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

share|improve this answer

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.

share|improve this answer

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
share|improve this answer

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.