grep - Find a pattern in a file


grep [ options ] pattern file


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:


All lines but those matching are printed.


Only a count of matching lines is printed.


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


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


Suppress display of filename headers.


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


grep -y endif login

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


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)