[Next] [Previous] [Up] [Top] [Contents]

7.2 Text Processing Commands

7.2.1 grep

This section provides an introduction to the use of regular expressions and grep.

The grep utility is used to search for generalized regular expressions occurring in Unix files. Regular expressions, such as those shown above, are best specified in apostrophes (or single quotes) when specified in the grep utility. The egrep utility provides searching capability using an extended set of meta-characters. The syntax of the grep utility, some of the available options, and a few examples are shown below.

Syntax

grep [options] regexp [file[s]]

Common Options

-i ignore case

-c report only a count of the number of lines containing matches, not the matches themselves

-v invert the search, displaying only lines that do not match

-n display the line number along with the line on which a match was found

-s work silently, reporting only the final status:

0, for match(es) found

1, for no matches

2, for errors

-l list filenames, but not lines, in which matches were found

Examples

Consider the following file:

{unix prompt 5} cat num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

4 12 twelve

5 11 eleven

6 10 ten

7 9 nine

8 8 eight

9 7 seven

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

Here are some grep examples using this file. In the first we'll search for the number 15:

{unix prompt 6} grep '15' num.list

1 15 fifteen

15 1 one

Now we'll use the "-c" option to count the number of lines matching the search criterion:

{unix prompt 7} grep -c '15' num.list

2

Here we'll be a little more general in our search, selecting for all lines containing the character 1 followed by either of 1, 2 or 5:

{unix prompt 8} grep '1[125]' num.list

1 15 fifteen

4 12 twelve

5 11 eleven

11 5 five

12 4 four

15 1 one

Now we'll search for all lines that begin with a space:

{unix prompt 9} grep '^ ' num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

4 12 twelve

5 11 eleven

6 10 ten

7 9 nine

8 8 eight

9 7 seven

Or all lines that don't begin with a space:

{unix prompt 10} grep '^[^ ]' num.list

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

The latter could also be done by using the -v option with the original search string, e.g.:

{unix prompt 11} grep -v '^ ' num.list

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

Here we search for all lines that begin with the characters 1 through 9:

{unix prompt 12} grep '^[1-9]' num.list

10 6 six

11 5 five

12 4 four

13 3 three

14 2 two

15 1 one

This example will search for any instances of t followed by zero or more occurrences of e:

{unix prompt 13} grep 'te*' num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

4 12 twelve

6 10 ten

8 8 eight

13 3 three

14 2 two

This example will search for any instances of t followed by one or more occurrences of e:

{unix prompt 14} grep 'tee*' num.list

1 15 fifteen

2 14 fourteen

3 13 thirteen

6 10 ten

We can also take our input from a program, rather than a file. Here we report on any lines output by the who program that begin with the letter l.

{unix prompt 15} who | grep '^l'

lcondron ttyp0 Dec 1 02:41 (lcondron-pc.acs.)


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]