UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 27.1 Different Versions of grep Chapter 27
Searching Through Files
Next: 27.3 Finding Text That Doesn't Match
 

27.2 Searching for Text with grep

There are many well-known benefits provided by grep to the user who doesn't remember what his or her files contain. Even users of non-UNIX systems who make fun of its obscure name wish they had a utility with its power to search through a set of files for an arbitrary text pattern, known as a regular expression (26.4).

The main function of grep is to look for strings matching a regular expression and print only the lines found. Use grep when you want to look at how a particular word is used in one or more files. For example, here's how to list the lines in the file ch04 that contain either run-time or run time:

".." 






$ grep "run[- ]time" ch04
This procedure avoids run-time errors for not-assigned
and a run-time error message is produced.
run-time error message is produced.
program aborts and a run-time error message is produced.
DIMENSION statement in BASIC is executable at run time.
This means that arrays can be redimensioned at run time.
accessible or not open, the program aborts and a run-time

Another use might be to look for a specific nroff/troff macro (43.14) in a file. In a file coded with mm macros, the following command will list top-level (.H1) and second-level (.H2) headings:

[..] 







$ grep "^\.H[12]" ch0[12]
ch01:.H1 "Introduction"
ch01:.H1 "Windows, Screens, and Images"
ch01:.H2 "The Standard Screen-stdscr"
ch01:.H2 "Adding Characters"
ch02:.H1 "Introduction"
ch02:.H1 "What Is Terminal Independence?"
ch02:.H2 "Termcap"
ch02:.H2 "Terminfo"

In effect, it produces a quick outline of the contents of these files.

grep is also often used as a filter (1.3), to select from the output of some other program. For example, not all versions of ps (38.5) allow you to print out the processes belonging to another user, but it's easy to simulate this behavior by listing all processes and piping the output to grep:

% ps -aux | grep jerry

There are several options commonly used with grep. The -i option specifies that the search ignore the distinction between uppercase and lowercase. The -c option (15.8) tells grep to return only a count of the number of lines matched. The -w option searches for the pattern "as a word." For example, grep if would match words like cliff or knife, but grep -w if wouldn't. The -l option (15.7) returns only the name of the file when grep finds a match. This can be used to prepare a list of files for another command. The -v option (27.3) reverses the normal action, and only prints out lines that don't match the search pattern.

- DD from UNIX Text Processing, Hayden Books, 1987, Chapter 11


Previous: 27.1 Different Versions of grep UNIX Power ToolsNext: 27.3 Finding Text That Doesn't Match
27.1 Different Versions of grep Book Index27.3 Finding Text That Doesn't Match

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System