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 '14 at 20:45
4 Answers
You can do the trick with -E
option:
grep -E '(^|pattern)' file
Here is a simple output:

- 153,898
-
1
-
1
-
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 '14 at 14:26 -
-
@Gnouc, good habit given that
grep '\|'
is not standard/portable. – Stéphane Chazelas Apr 29 '14 at 14:37 -
-
Why the parentheses though?
grep
colours the matched string, not capture groups. – Stéphane Chazelas Apr 29 '14 at 14:45 -
@StephaneChazelas: I just want to make it clearly for the OP. I think look at
^|pattern
can make it more difficult to understand. – cuonglm Apr 29 '14 at 14:52
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").

- 242,166

- 10,400
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:
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.

- 242,166
-
-
1@pawel7318 I'm sharing it here :) I also have it on github actually: https://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.

- 284