There are many well-known benefits provided by
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 (
The main function of
is to look for strings matching a regular
expression and print only the lines found.
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
that contain either
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
in a file.
In a file coded with mm macros, the following command will list
) and second-level (
grep "^\.H" ch0
ch01:.H1 "Windows, Screens, and Images"
ch01:.H2 "The Standard Screen-stdscr"
ch01:.H2 "Adding Characters"
ch02:.H1 "What Is Terminal Independence?"
In effect, it produces a quick outline of the contents of these files.
is also often used as a
to select from the output of some other program.
not all versions of
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
ps -aux | grep jerry
There are several options commonly used with
that the search ignore the distinction between uppercase and lowercase.
to return only a count of the number of lines matched.
option searches for the pattern "as a word."
would match words like
grep -w if
returns only the name of the file when
finds a match.
This can be used to prepare a list of files for another command.
reverses the normal action, and only prints out lines that don't
match the search pattern.
UNIX Text Processing
, Hayden Books, 1987, Chapter 11