Take the 2-minute tour ×
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.

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 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 at 20:45
add comment

4 Answers 4

up vote 6 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
1  
Oooh, nice one! –  Andreas Wiese Apr 29 at 13:56
1  
Sneaky, +1! You can do it without -E also: grep '^\|grep' cat –  terdon Apr 29 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 at 14:26
    
@terdon: Good point. use -E is only my habits. –  Gnouc Apr 29 at 14:31
    
@Gnouc, good habit given that grep '\|' is not standard/portable. –  Stéphane Chazelas Apr 29 at 14:37
show 3 more comments

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

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;
getopts('hic:l:',\%opts);
    if ($opts{h}){
      print<<EoF; 
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;

EoF
      exit(0);
    }

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}) {
   @color=split(/,/,$opts{c});
}
my @patterns;
if($opts{l}){
     @patterns=split(/,/,$opts{l});
}
else{
    $patterns[0]='\*';
}

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

while (my $line=<>) 
{ 
    for (my $c=0; $c<=$#patterns; $c++){
    if($case_sensitive){
        if($line=~/$patterns[$c]/){
           $line=~s/($patterns[$c])/color("$color[$c]").$1.color("reset")/ge;
        }
    }
    else{
        if($line=~/$patterns[$c]/i){
          $line=~s/($patterns[$c])/color("$color[$c]").$1.color("reset")/ige;
        }
      }
    }
    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 at 14:27
1  
@pawel7318 I'm sharing it here :) I also have it on github actually: github.com/terdon/scripts/blob/master/color –  terdon Apr 29 at 14:47
add comment

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

share|improve this answer
add comment

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