When I use e.g. cat file.txt | grep --color=tty "pattern" I get the pattern I search highlighted. When I want some more context around each hit, grep has the -A, -B and -C parameters.

However, I want to display the whole file (or whatever command output) and highlight a certain pattern. Does such a highlight command or tool exist?

link|improve this question

How is what you want to do now different from what you already do? – honk Oct 13 '10 at 17:01
feedback

6 Answers

up vote 4 down vote accepted

There was an answer on unix.stackexchange.com that had this neat trick:

grep -E --color 'pattern|$' file.txt

or

grep --color 'pattern\|$' file.txt

which matches your pattern or the end-of-line on each line. Only the pattern is highlighted.

link|improve this answer
+1 Simple, and easily adaptable to zgrep, bzgrep, etc. Nice! – Stefan Lasiewski Oct 14 '10 at 3:47
feedback

I like ack.

ack --passthru somepattern filename

It's like grep, but better. It highlights by default, and with the --passthru option it shows the entire file (or standard input) instead of only the matching lines.

link|improve this answer
feedback

I also recommend the use of less (or such), but I want to show you a more didactical solution. Once you have defined this function:

function highlight()
{
    sed "s/$1/`tput smso`&`tput rmso`/g" "${2:--}"
}

You can use it this way:

command | highlight pattern
highlight pattern file.txt

Note: This version is case sensitive, to change that just append i after g in the sed expression.

link|improve this answer
1  
I'd invoke tput instead of hardcoding the values. This way you also avoid using echo -e, which can have other effects. – Ignacio Vazquez-Abrams Oct 13 '10 at 18:25
You're absolutely right, I searched it with no luck. Thanks. – cYrus Oct 13 '10 at 18:37
This version doesn't need the capturing parentheses and can be run either by prog | highlight "pattern" or highlight "pattern" file: highlight () { local file=${2:--}; sed "s/$1/$(tput smso)&$(tput rmso)/gi" "$file"; } – Dennis Williamson Oct 14 '10 at 4:45
My sed skills are basically not existent. How would I have to change that in order to match the pattern case sensitive. Anyway, I will add that one to my bash_functions include. Thank you very much. – DerMike Oct 14 '10 at 7:31
@Dennis: Thanks for the tips. Edited. – cYrus Oct 14 '10 at 12:45
show 1 more comment
feedback

Passing -C to grep with a sufficiently high value will do this.

link|improve this answer
I Could have sworn I got some thing else last time I tried that. (Each match the context with '-----' separators in between.) Now that you told me, it runs fine :-) Thank you. – DerMike Oct 14 '10 at 7:22
It will separate each match, if there isn't enough context. – Ignacio Vazquez-Abrams Oct 14 '10 at 7:23
feedback

less is highlighting pattern, so

less -p 'mypattern' file.txt

or

ridiculously | long | pipe | less

and then in less / to search for a pattern which will be highlighted.

Side note:

You can improve your command, for example
set alias alias grep="grep --color=tty" and use it like that:

grep 'mypattern' file.txt;
link|improve this answer
1  
you should refrain from using GREP_OPTIONS, use alias instead. see: bugs.launchpad.net/ubuntu/+bug/67141 – lesmana Oct 13 '10 at 18:29
Primum non nocere. removed. – Casual Coder Oct 13 '10 at 18:43
GREP_OPTIONS is used by many forms of grep (zgrep, bzgrep, bzfgrep, etc.) So, if you want all forms of grep to behave consistently, you'd need to set an alias for each form of grep. @Casual's new answer above won't for with something like bzgrep pattern /var/log/messages.?.gz, which is an example where color highlighting be VERY helpful. – Stefan Lasiewski Oct 14 '10 at 3:24
feedback

There's a nice supercat tool, which serves exactly that purpose. It can both colorize your console output and produce a "highlighted" html from your input.

link|improve this answer
Thanks, that looks promissing. However, I can't seem to get it working at the moment. – DerMike Oct 6 '11 at 9:16
1  
Take a look at its config files (in /etc/supercat on Ubuntu), which set up the coloring rules, they are basically color/regexp pairs (with some extra rules); man page is also short and nice. – ivant Oct 6 '11 at 17:29
Ahh, learning by example. Thank you. – DerMike Oct 7 '11 at 8:37
I found a spcrc vim syntax file at vim.org/scripts/script.php?script_id=2676 – DerMike Oct 7 '11 at 8:39
feedback

Your Answer

 
or
required, but never shown

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