Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Looking for Words

The basic syntax of the grep command is

grep word file

Here file is the name of a file in which you want to search for word. The grep command displays every line in file that contains word. When you specify more than one file, grep precedes each of the output lines with the name of the file that contains that line.

As an example, the following command locates all the occurrences of the word pipe in file ch15.doc (this chapter):

$ grep pipe ch15.doc
I've broken the command into two lines, with the pipe character
⇒as the
the right thing and use the next line as the command to pipe to. It's
The first few lines look like (ten actually, I piped the output to

If I specify more than one file the output changes as follows:

$ grep pipe ch15.doc ch15-01.doc
ch15.doc:I've broken the command into two lines, with the pipe
⇒character as the
ch15.doc:the right thing and use the next line as the command to
⇒pipe to. It's
ch15.doc:The first few lines look like (ten actually, I piped the
⇒output to
ch15-01.doc:I've broken the command into two lines, with the pipe
⇒character as the
ch15-01.doc:the right thing and use the next line as the command to
⇒pipe to.  It's
ch15-01.doc:The first few lines look like (ten actually, I piped
⇒the output to

As you can see, the name of the file precedes each line that contains the word pipe.

If grep cannot find a line in any of the specified files that contains the requested word, no output is produced. For example,

$ grep utilities ch16.doc

produces no output because the word utilities does not appear in the file ch16.doc.

Case Independent Matching

One of the features of grep is that it matches the specified word according to the case that you specify. In grep, the word Apple is different than the word apple.

Sometimes you want to match words regardless of the case that you specify. To do this, use the -i option. For example, the command

$ grep unix ch16.doc

produces the output:

all unix users.  The GNU versions of these commands support all the
unix has several additional pieces of information associated with it.
unix counterparts, but implement a few nice options which makes their
unix files names, but they are, and handling them correctly is

On the other hand, the command

$ grep UNIX ch16.doc

produces different output:

GNU stands for GNU's not Unix and is the name of a Unix-compatible
Project utilities are the GNU implementation of familiar Unix
⇒programs

By using the -i option, you get the sum of both of these commands:

$ grep -i unix ch16.doc
GNU stands for GNU's not Unix and is the name of a Unix-compatible
Project utilities are the GNU implementation of familiar Unix
⇒programs
all unix users.  The GNU versions of these commands support all the
unix has several additional pieces of information associated with it.
unix counterparts, but implement a few nice options which makes their
unix files names, but they are, and handling them correctly is

Reading From STDIN

When no files are specified, grep looks for matches on the lines that are entered on STDIN. This makes it perfect for attaching to pipes.

For example, the following command looks for all users named ranga in the output of the who command:

$ who | grep ranga
ranga    tty1     Aug 26 14:12
ranga    ttyp2    Nov 23 14:15 (rishi.bosland.u)

The -v Option

Most of the time you use grep to search through a file looking for a particular word, but sometimes you want to acquire a list of all the lines that do not match a particular word.

Using grep, this is simple—specify the -v option. For example, the following command produces a list of all the lines in /etc/passwd that do not contain the word home:

$ grep -v home /etc/passwd

On my system, the output looks like the following:

root:*:0:3::/:/sbin/sh
daemon:*:1:5::/:/sbin/sh
bin:*:2:2::/usr/bin:/sbin/sh
sys:*:3:3::/:
adm:*:4:4::/var/adm:/sbin/sh
uucp:*:5:3::/var/spool/uucppublic:/usr/lbin/uucp/uucico
lp:*:9:7::/var/spool/lp:/sbin/sh
nobody:*:-2:-2::/:

One common use of the -v option is to parse the output of the ps command. For example, if I were looking for all instances of bash that were running on a system, I could use the following command:

$ /bin/ps -ef | grep bash

Sometimes the output looks like the following:

ranga  3277  3276  2 13:41:45 pts/t0    0:02 -bash
ranga  3463  3277  4 18:38:26 pts/t0    0:00 grep bash

The second process in this list is the grep that I just ran. Because it is not really an instance of bash, I can get rid of it as follows:

$ /bin/ps -ef | grep bash | grep -v grep

This removes the extraneous output:

ranga  3277  3276  0 13:41:45 pts/t0    0:02 -bash

Line Numbers

As grep looks through a file for a given word, it keeps track of the line numbers that it has examined. You can have grep list the line numbers along with the matching lines by specifying the -n option. With this option the output format is

file:line number:line

Here file is the name of the file in which the match occurs, line number is the line number in the file on which the matching line occurs, and line is the complete line that contains the specified word. For example, the command

$ grep -n pipe ch15.doc ch15-01.doc

produces the following output:

ch15.doc:969:I've broken the command into two lines, with the pipe
⇒character as the
ch15.doc:971:the right thing and use the next line as the command
⇒to pipe to.  It's
ch15.doc:1014:The first few lines look like (ten actually, I piped
⇒the output to
ch15-01.doc:964:I've broken the command into two lines, with the
⇒pipe character as the
ch15-01.doc:966:the right thing and use the next line as the command
⇒to pipe to.  It's
ch15-01.doc:1009:The first few lines look like (ten actually, I
⇒piped the output to

As you can see, the lines might be the same in both files, but the line numbers are different.


Previous Table of Contents Next