Unix Power ToolsUnix Power ToolsSearch this book

Chapter 13. Searching Through Files

Contents:

Different Versions of grep
Searching for Text with grep
Finding Text That Doesn't Match
Extended Searching for Text with egrep
grepping for a List of Patterns
Approximate grep: agrep
Search RCS Files with rcsgrep
GNU Context greps
A Multiline Context grep Using sed
Compound Searches
Narrowing a Search Quickly
Faking Case-Insensitive Searches
Finding a Character in a Column
Fast Searches and Spelling Checks with "look"
Finding Words Inside Binary Files
A Highlighting grep

13.1. Different Versions of grep

Summary Box

grep is one of Unix's most useful tools. As a result, all users seem to want their own, slightly different version that solves a different piece of the problem. (Maybe this is a problem in itself; there really should be only one grep, as the manpage says.) Three versions of grep come with every Unix system; in addition, there are six or seven freely available versions that we'll mention here, as well as probably dozens of others that you can find kicking around the Net.

 

Here are the different versions of grep and what they offer. We'll start with the standard versions:

 
Plain old grep
Great for searching with regular expressions (Section 13.2).

   
Extended grep (or egrep)
Handles extended regular expressions. It is also, arguably, the fastest of the standard greps (Section 13.4).

   
Fixed grep (or fgrep)
So named because it matches fixed strings. It is sometimes inaccurately called "fast grep"; often it is really the slowest of them all. It is useful to search for patterns with literal backslashes, asterisks, and so on that you'd otherwise have to escape somehow. fgrep has the interesting ability to search for multiple strings (Section 13.5).

 
 

Of course, on many modern Unixes all three are the same executable, just with slightly different behaviors, and so you may not see dramatic speed differences between them. Now for the freeware versions:

 
agrep, or "approximate grep"
A tool that finds lines that "more or less" match your search string. A very interesting and useful tool, it's part of the glimpse package, which is an indexing and query system for fast searching of huge amounts of text. agrep is introduced in Section 13.6.

   
Very fast versions of grep, such as GNU grep/egrep/fgrep
Most free Unixes use GNU grep as their main grep.

   
rcsgrep
Searches through RCS files (Section 39.5) (Section 13.7).

 
 

In addition, you can simulate the action of grep with sed, awk, and perl. These utilities allow you to write such variations as a grep that searches for a pattern that can be split across several lines (Section 13.9) and other context grep programs (Section 41.12), which show you a few lines before and after the text you find. (Normal greps just show the lines that match.)

 

-- ML



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.