27.2 Searching for Text with grepThere 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 :
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 (
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 :
%
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, - from UNIX Text Processing , Hayden Books, 1987, Chapter 11 |
|