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 ?

  • 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, 2014 at 20:45

4 Answers 4


You can do the trick with -E option:

grep -E '(^|pattern)' file

Here is a simple output:

enter image description here

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


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.


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

Not the answer you're looking for? Browse other questions tagged .