[Chapter 27] 27.20 A Highlighting grep
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:44:38Z

#

| Chapter 27 Searching Through Files |

## 27.20 A Highlighting grep

Does it happen to you that you grep for a word, lines scroll down your screen, and it's hard to find the word on each line? For example, suppose I'm looking for any mail messages I've saved that say anything about the perl programming language. But when I grep the file, most of it seems useless:

>     % grep perl ~/Mail/save
>     > and some of it wouldn't compile properly.  I wonder if
>     Subject: install script, for perl scripts
>     perl itself is installed?
>     > run but dies with a read error because it isn't properly
>     > if I can get it installed properly on another machine I
>     > run but dies with a read error because it isn't properly
>     > if I can get it installed properly on another machine I

hgrep | Well, as described on its own manual page, here's a program that's "trivial, but cute." hgrep runs a grep and highlights the string being searched for, to make it easier for us to find what we're looking for.

>     % hgrep perl ~/Mail/save
>     > and some of it wouldn't compile pro"perl"y.  I wonder if
>     Subject: install script, for  "perl" scripts
>      perl itself is installed?
>     > run but dies with a read error because it isn't properly
>     > if I can get it installed properly on another machine I
>     > run but dies with a read error because it isn't properly
>     > if I can get it installed properly on another machine I

And now we know why the output looked useless: because most of it is! Luckily, hgrep is just a front-end; it simply passes all its arguments to grep. So hgrep necessarily accepts all of grep's options, and I can just use the -w option (27.4) to weed the output down to what I want:

>     % hgrep -w perl ~/Mail/save
>     Subject: install script, for
>      perl scripts
>      perl itself is installed?

- LM

27.19 Finding Words Inside Binary Files |  | 28. Comparing Files