Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have an alias: alias grep='grep --color=auto'. I wish to have some way to print not only matching row but all of them and just color matching patterns. I doubt is there an option to do that in the grep itself. Any other tool ? Or idea how to achieve this ?

share|improve this question

marked as duplicate by Gilles, Anthon, slm, Ramesh, Braiam Apr 29 '14 at 15:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

looks that your're right and this is a duplicate but I believe answers here are better same as the question - it's sharper without long, boring and unclear example. – pawel7318 Apr 29 '14 at 20:45
up vote 8 down vote accepted

You can do the trick with -E option:

grep -E '(^|pattern)' file

Here is a simple output:

enter image description here

share|improve this answer
Oooh, nice one! – Andreas Wiese Apr 29 '14 at 13:56
Sneaky, +1! You can do it without -E also: grep '^\|grep' cat – terdon Apr 29 '14 at 14:12
function ggrep() { grep --color=auto -E "(^|$@)"; } and now it can be used that way: echo -e "foo\nbar\nbaz"|ggrep bar – pawel7318 Apr 29 '14 at 14:26
@terdon: Good point. use -E is only my habits. – cuonglm Apr 29 '14 at 14:31
@Gnouc, good habit given that grep '\|' is not standard/portable. – Stéphane Chazelas Apr 29 '14 at 14:37

You could do this using a bit sed and terminal magic:

$ sed -e 's/<pattern>/\x1b[31;01m&\x1b[m/g' <file>

This replaces each occurrence of <pattern> with the matched text surrounded by \x1b[31;01m (which is the terminal escape sequence for "set foreground color to bold red") and \x1b[m (same for "set foreground color to default").

share|improve this answer

I've written a little script that will color whatever string you give it:

#!/usr/bin/env perl
use Getopt::Std;
use strict;
use Term::ANSIColor; 

my %opts;
    if ($opts{h}){
Use -l to specify the pattern(s) to highlight. To specify more than one 
pattern use commas. 

-l : A Perl regular expression to be colored. Multiple expressions can be
     passed as comma separated values: -l foo,bar,baz
-i : makes the search case sensitive
-c : comma separated list of colors;


my $case_sensitive=$opts{i}||undef;
my @color=('bold red','bold blue', 'bold yellow', 'bold green', 
           'bold magenta', 'bold cyan', 'yellow on_magenta', 
           'bright_white on_red', 'bright_yellow on_red', 'white on_black');
if ($opts{c}) {
my @patterns;

# Setting $| to non-zero forces a flush right away and after 
# every write or print on the currently selected output channel. 

while (my $line=<>) 
    for (my $c=0; $c<=$#patterns; $c++){
    print STDOUT $line;

If you save it as color in a directory that is in your $PATH and make it executable (chmod +x /usr/bin/color), you can color the matched pattern like this:

echo -e "foo\nbar\nbaz\nbib" | color -l foo,bib 

That will produce:

  enter image description here

As written, the script has predefined colors for 10 different patterns, so giving it a comma separated list as I have in the example above will color each of the patterns matched in a different color.

share|improve this answer
Nice one - you should share it through the Github – pawel7318 Apr 29 '14 at 14:27
@pawel7318 I'm sharing it here :) I also have it on github actually: github.com/terdon/scripts/blob/master/color – terdon Apr 29 '14 at 14:47

This is built into ack -- colored output is the default, and the --passthru option prints all lines.

share|improve this answer

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