Previous Section  < Day Day Up >  Next Section

4.13. grep with Options (UNIX and GNU)

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 examples in this section use the following datafile, which 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.53.

% 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.54.

% 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.

Example 4.55.

% 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.56.

% 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.57.

% 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.

% 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.58.

% 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.59.

% 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.

4.13.1 GNU grep Options Examples

In addition to the options provided with UNIX grep, the GNU version provides options that further refine the output resulting from pattern searches.

The examples in this section use the following datafile, 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.60.

% grep -V

grep (GNU grep) 2.2



Copyright (C) 1988, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc. This is

free software; see the source for copying conditions. There is NO warranty; not even for

 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


EXPLANATION

With the –V option, grep's version and copyright information are listed. The version information should be included with any bug reports sent to the GNU Foundation.

Example 4.61.

1   % grep -2 Patricia datafile

    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



2   % grep -C Patricia datafile

    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


EXPLANATION

  1. After a line matching Patricia is found, grep displays that line and the two lines before and after it.

  2. The –C option is the same as –2.

% 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.62.

% grep -A 2 Patricia datafile

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


EXPLANATION

After a line matching Patricia is found, grep displays that line and the two lines after it.

Example 4.63.

% grep -B 2 Patricia datafile

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


EXPLANATION

After a line matching Patricia is found, grep displays that line and the two lines before (preceding) it.

Example 4.64.

% grep -b '[abc]' datafile

0:northwest           NW      Charles Main          3.0    .98    3    34

39:western            WE      Sharon Gray           5.3    .97    5    23

76:southwest          SW      Lewis Dalsass         2.7    .8     2    18

115:southern          SO      Suan Chin             5.1    .95    4    15

150:southeast         SE      Patricia Hemenway     4.0    .7     4    17

193:eastern           EA      TB Savage             4.4    .84    5    20

228:northeast         NE      AM Main Jr.           5.1    .94    3    13

266:north             NO      Margot Weber          4.5    .89    5     9

301:central           CT      Ann Stephens          5.7    .94    5    13


EXPLANATION

With the –b option, grep prints the byte offset from the input file before each line of output.

Instead of using the datafile for these next two examples, we'll use a file called negative to demonstrate the –e and –x options.

% cat negative

-40 is cold.

This is line 1.

This is line 2.5

-alF are options to the ls command


Example 4.65.

1   % grep -e '-alF' negative

    -alF are options to the ls command



2   % grep --regexp=-40 negative

    -40 is cold.


EXPLANATION

  1. With the –e option, grep treats all characters in the pattern equally, so that leading dashes are not mistaken for options.

  2. The alternate way to represent –e is – –regexp=pattern, where pattern is the regular expression; in this example the regular expression is –40.

Example 4.66.

% grep -x -e '-40 is cold.' negative

-40 is cold.


EXPLANATION

With the –x option, grep will not match a line unless the search pattern is identical to the entire line. The –e is used to allow a dash as the first character in the search string.

The remaining examples in this section use the following datafile.

% 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.67.

1   % cat repatterns

    western

    north



2   % grep -f repatterns datafile

    northwest      NW     Charles Main       3.0    .98    3    34

    western        WE     Sharon Gray        5.3    .97    5    23

    northeast      NE     AM Main Jr.        5.1    .94    3    13

    north          NO     Margot Weber       4.5    .89    5     9


EXPLANATION

  1. The file repatterns is displayed. It contains grep's search patterns that will be matched against lines in an input file. Western and north are the patterns grep will use in its search.

  2. With the –f option followed by a filename (in this example, repatterns), grep will get its search patterns from that file and match them against lines in datafile. Grep searched for and printed all lines containing patterns western and north.

Example 4.68.

1   % grep  '[0-9]' datafile db

    datafile:northwest NW       Charles Main          3.0   .98   3  34

    datafile:western WE         Sharon Gray           5.3   .97   5  23

    datafile:southwest SW       Lewis Dalsass         2.7   .8    2  18

    datafile:southern SO        Suan Chin             5.1   .95   4  15

    datafile:southeast SE       Patricia Hemenway     4.0   .7    4  17

    datafile:eastern EA         TB Savage             4.4   .84   5  20

    datafile:northeast NE       AM Main Jr.           5.1   .94   3  13

    datafile:north NO           Margot Weber          4.5   .89   5   9

    datafile:central CT         Ann Stephens          5.7   .94   5  13

    db:123



2   % grep -h '[0-9]' datafle db

    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



    123


EXPLANATION

  1. If more than one file is listed, grep prepends each line of its output with the filename. Filenames are datafile and db.

  2. With the –h option, grep suppresses the header information; i.e., does not print the filenames.

Example 4.69.

% grep -q Charles datafile    or

% grep --quiet Charles datafile

% echo $status

0


EXPLANATION

The quiet option suppresses any output from grep. It is used when the exit status is all that is needed. If the exit status is zero, grep found the pattern.

    Previous Section  < Day Day Up >  Next Section