Previous Section  < Day Day Up >  Next Section

4.3. grep with Options

The grep command has a number of options that control its behavior. Not all versions of UNIX support exactly the same options, so be sure to check your man pages for a complete list.

The following datafile, used for the examples in this section, is repeated periodically for your convenience.

% cat datafile

northwest

NW

Charles Main

3.0

.98

3

34

western

WE

Sharon Gray

5.3

.97

5

23

southwest

SW

Lewis Dalsass

2.7

.8

2

18

southern

SO

Suan Chin

5.1

.95

4

15

southeast

SE

Patricia Hemenway

4.0

.7

4

17

eastern

EA

TB Savage

4.4

.84

5

20

northeast

NE

AM Main Jr.

5.1

.94

3

13

north

NO

Margot Weber

4.5

.89

5

9

central

CT

Ann Stephens

5.7

.94

5

13


Example 4.22.

% grep –n '^south' datafile

3:southwest           SW        Lewis Dalsass           2.7   .8    2   18

4:southern            SO        Suan Chin               5.1   .95  4    15

5:southeast           SE        Patricia Hemenway       4.0   .7    4   17


EXPLANATION

The –n option precedes each line with the number of the line where the pattern was found, followed by the line.

Example 4.23.

% grep –i 'pat' datafile

southeast           SE        Patricia Hemenway       4.0   .7    4   17


EXPLANATION

The –i option turns off case sensitivity. It does not matter if the expression pat contains any combination of upper- or lowercase letters.

% cat datafile

northwest

NW

Charles Main

3.0

.98

3

34

western

WE

Sharon Gray

5.3

.97

5

23

southwest

SW

Lewis Dalsass

2.7

.8

2

18

southern

SO

Suan Chin

5.1

.95

4

15

southeast

SE

Patricia Hemenway

4.0

.7

4

17

eastern

EA

TB Savage

4.4

.84

5

20

northeast

NE

AM Main Jr.

5.1

.94

3

13

north

NO

Margot Weber

4.5

.89

5

9

central

CT

Ann Stephens

5.7

.94

5

13


Example 4.24.

% grep –v 'Suan Chin' datafile

northwest             NW      Charles Main              3.0   .98   3  34

western               WE      Sharon Gray               5.3   .97   5  23

southwest             SW      Lewis Dalsass             2.7   .8    2  18

southeast             SE      Patricia Hemenway         4.0   .7    4  17

eastern               EA      TB Savage                 4.4   .84   5  20

northeast             NE      AM Main Jr.               5.1   .94   3  13

north                 NO      Margot Weber              4.5   .89   5  9

central               CT      Ann Stephens              5.7   .94   5  13


EXPLANATION

Here, the –v option prints all lines not containing the pattern Suan Chin. This option is used when deleting a specific entry from the input file. To really remove the entry, you would redirect the output of grep to a temporary file, and then change the name of the temporary file back to the name of the original file as shown here:


grep -v 'Suan Chin' datafile > temp

mv temp datafile


Remember that you must use a temporary file when redirecting the output from datafile. If you redirect from datafile to datafile, the shell will "clobber" the datafile. (See "Redirection" on page 25.)

Example 4.25.

% grep –l 'SE'  *

datafile

datebook


EXPLANATION

The –l option causes grep to print out only the filenames where the pattern is found instead of the line of text.

Example 4.26.

% grep –c 'west' datafile

3


EXPLANATION

The –c option causes grep to print the number of lines where the pattern was found. This does not mean the number of occurrences of the pattern. For example, if west is found three times on a line, it only counts the line once.

Example 4.27.

% grep  –w 'north' datafile

north              NO       Margot Weber             4.5   .89     5     9


EXPLANATION

The –w option causes grep to find the pattern only if it is a word,[a] not part of a word. Only the line containing the word north is printed, not northwest, northeast, etc.

[a] A word is a sequence of alphanumeric characters starting at the beginning of a line or preceded by whitespace and ending in whitespace, punctuation, or a newline.

Example 4.28.

% echo $LOGNAME

lewis



% grep -i "$LOGNAME" datafile

southwest             SW        Lewis Dalsass           2.7   .8    2     18


EXPLANATION

The value of the shell ENV variable, LOGNAME, is printed. It contains the user's login name. If the variable is enclosed in double quotes, it will still be expanded by the shell, and in case there is more than one word assigned to the variable, whitespace is shielded from shell interpretation. If single quotes are used, variable substitution does not take place; that is, $LOGNAME is printed.

    Previous Section  < Day Day Up >  Next Section