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 .
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
.H1) and second-level (
In effect, it produces a quick outline of the contents of these files.
grep is also often used as a, to select from the output of some other program. For example, 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 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.
grep if would match words like cliff or
grep -w if wouldn't.
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.
reverses the normal action, and only prints out lines that don't
match the search pattern.
- from UNIX Text Processing, Hayden Books, 1987, Chapter 11