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.

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?

share|improve this question
    
How is what you want to do now different from what you already do? –  Benjamin Bannier Oct 13 '10 at 17:01
add comment

6 Answers

up vote 13 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.

share|improve this answer
    
+1 Simple, and easily adaptable to zgrep, bzgrep, etc. Nice! –  Stefan Lasiewski Oct 14 '10 at 3:47
    
Is there any way to avoid typing --color all the time? I think its pretty basic to embed color option into grep, not sure why we have explicitly type in color every single time. I tried to create scripts say grepd files has a bash saying grep --color $1 $2 but it doesnt work. Any clue here to fix this please –  lordlupine May 2 '13 at 17:47
1  
@lordlupine: Add GREP_OPTIONS='--color' to your ~/.bashrc (or modify the setting to include --color). You will need to start a new terminal or execute that assignment at the command line in the current terminal to have it take effect. –  Dennis Williamson May 2 '13 at 18:20
    
thanks dennis . –  lordlupine May 2 '13 at 18:23
add comment

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;
share|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
add comment

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.

share|improve this answer
    
If you're trying to pipe output into ack, e.g. from tail, then you need to use the --filter option too. For example: tail -f myLogFile | ack --filter --passthru pattern –  sparrowt Nov 28 '13 at 13:58
add comment

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

share|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
add comment

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.

share|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

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.

share|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
add comment

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.