7

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 ?

Braiam
  • 35,991
pawel7318
  • 2,000
  • 3
  • 16
  • 15
  • 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

4 Answers4

12

You can do the trick with -E option:

grep -E '(^|pattern)' file

Here is a simple output:

enter image description here

cuonglm
  • 153,898
6

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").

terdon
  • 242,166
Andreas Wiese
  • 10,400
3

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.

terdon
  • 242,166
3

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