Name

grep - Find a pattern in a file

Syntax

grep [ options ] pattern file

Description

Grep, abbreviation of Globally find Regular Expressions and Print, searches one or more files for lines matching a pattern. Normally, each line found is displayed. The following options are recognized:

-v

All lines but those matching are printed.

-c

Only a count of matching lines is printed.

-l

The names of files with matching lines are listed (once) separated by newlines.

-n

Each line is preceded by its relative line number in the file.

-h

Suppress display of filename headers.

-y

The case of letters is ignored in making comparisons (that is, upper and lower case are considered identical).

Example:

grep -y endif login

This displays all lines in the file login containing the word endif, ignoring alphabetic case.

Note

To search for a pattern consisting of words and spaces, the entire pattern must be enclosed in quotes:

grep "dream into action" clip

If the last argument in the command line starts with ‘>’, the output from grep is directed into the filename which follows:

grep -v mozart music.db >temp

This writes all lines in “music.db” not containing (-v) the pattern “mozart”, into the file “temp”.

Lines are limited to 255 characters; longer lines are truncated.

See Also

cat(C), tail(C), wc(C)