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?

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

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

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


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

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

  • +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 – Srujan Kumar Gulla 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

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.

  • 1
    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

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.

  • 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

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

  • 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

less is highlighting pattern, so

less -p 'mypattern' file.txt


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;
  • 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

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.

  • 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. – Ivan Tarasov Oct 6 '11 at 17:29
  • I found a spcrc vim syntax file at vim.org/scripts/script.php?script_id=2676 – DerMike Oct 7 '11 at 8:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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