13.2. Searching for Text with grep
There are many well-known benefits provided by grep to the user who doesn't remember what his files contain. Even users of non-Unix systems 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 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:
".." Section 27.12
$ 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 HTML tag in a file. The following command will list top-level (<H1> or <h1>) and second-level (<H2> or <h2>) headings that have the starting tag at the beginning (^) of the line:
$ grep "^<[Hh]>" ch0.html ch01.html:<h1>Introduction</h1> ch01.html:<h1>Windows, Screens, and Images</h1> ch01.html:<h2>The Standard Screen-stdscr</h2> ch01.html:<h2>Adding Characters</h2> ch02.html:<H1>Introduction</H1> ch02.html:<H1>What Is Terminal Independence?</H1> ch02.html:<H2>Termcap</H2> ch02.html:<H2>Terminfo</H2>
In effect, it produces a quick outline of the contents of these files.
grep is also often used as a filter (Section 1.5), to select from the output of some other program. For example, you might want to find the process id of your inetd, if you just changed the configuration file and need to HUP inetd to make it reread the configuration file. Using ps ( Section 24.5) and grep together allows you to do this without wading through a bunch of lines of output:
% ps -aux | grep inetd root 321 0.0 0.2 1088 548 ?? Is 12Nov01 0:08.93 inetd -wW deb 40033 0.0 0.2 1056 556 p5 S+ 12:55PM 0:00.00 grep inetd % kill -HUP 321
There are several options commonly used with grep. The -i option specifies that the search ignore the distinction between upper- and lowercase. The -c option 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 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 (Section 13.3) reverses the normal action, and only prints out lines that don't match the search pattern. In the previous example, you can use the -v option to get rid of the extra line of output:
% ps -aux | grep inetd | grep -v grep root 321 0.0 0.2 1088 548 ?? Is 12Nov01 0:08.93 inetd -wW % kill -HUP 321
Copyright © 2003 O'Reilly & Associates. All rights reserved.