views:

702

answers:

6

I find grep's --color=always flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could.

I'd really like to cat a file and see the entire file with the pattern matches highlighted.

Is there some way I can tell grep to print every line being read regardless of whether there's a match? I know I could write a script to run grep on every line of a file, but I was curious whether this was possible with standard grep.

Thanks!

A: 

Ok, this is one way,

wc -l filename

will give you the line count -- say NN, then you can do

grep -C NN --color=always filename
nik
+20  A: 
egrep --color "pattern|$" file

or if you insist on using grep

grep --color -E "pattern|$" file
Ryan Oberoi
Hey! this is better :-)
nik
That |$ trick is neat! Well done, I'll have to remember that.For those of you that aren't regular expression savvy, "pattern|$" will match lines that have the pattern you're searching for AND lines that have an end -- that is, all of them. Because the end of a line isn't actually any characters, the colorized portion of the output will just be your pattern.Thanks Ryan!
Zack
Nice answer! short and sweet.
seth
+1  A: 

Here is a shell script that uses Awk's gsub function to replace the text you're searching for with the proper escape sequence to display it in bright red:

#! /bin/bash
awk -vstr=$1 'BEGIN{repltext=sprintf("%c[1;31;40m&%c[0m", 0x1B,0x1B);}{gsub(str,repltext); print}' $2

Use it like so:

$ ./cgrep pattern [file]

Unfortunately, it doesn't have all the functionality of grep.

For more information , you can refer to an article "So You Like Color" in Linux Journal

iWerner
+3  A: 

Here's something along the same lines. Chances are, you'll be using less anyway, so try this:

less -p pattern file

It will highlight the pattern and jump to the first occurrence of it in the file.

Dennis Williamson
+5  A: 

I'd like to recommend ack -- better than grep, a power search tool for programmers.

$ ack --color --passthru --pager="${PAGER:-less -R}" pattern files
$ ack --color --passthru pattern files | less -R
$ export ACK_PAGER_COLOR="${PAGER:-less -R}"
$ ack --passthru pattern files

I love it because it defaults to recursive searching of directories (and does so much smarter than grep -r), supports full Perl regular expressions (rather than the POSIXish regex(3)), and has a much nicer context display when searching many files.

ephemient
A: 

I use rcg from "Linux Server Hacks", O'Reilly. It's perfect for what you want and can highlight multiple expressions each with different colours.

!/usr/bin/perl -w

#

regexp coloured glasses - from Linux Server Hacks from O'Reilly

#

eg .rcg "fatal" "BOLD . YELLOW . ON_WHITE" /var/adm/messages

# use strict; use Term::ANSIColor qw(:constants);

my %target = ( );

while (my $arg = shift) { my $clr = shift;

    if (($arg =~ /^-/) | !$clr) {
            print "Usage: rcg [regex] [color] [regex] [color] ...\n";
            exit(2);
    }

    #
    # Ugly, lazy, pathetic hack here. [Unquote]
    #
    $target{$arg} = eval($clr);

}

my $rst = RESET;

while(<>) { foreach my $x (keys(%target)) { s/($x)/$target{$x}$1$rst/g; } print }