You should already be familiar with the
command, which has this general form:
% grep pattern file
searches one or more files for the specified
and displays all lines containing that pattern.
argument is a regular expression
. Using regular expressions to perform searches often is called pattern matching 
In the simplest cases, a regular expression is a literal string
or sequence of characters. For example,
is a regular expression that describes the literal characters
, strung together.
It describes a pattern found in words like
Regular expressions also can include special characters that let you perform wildcard searches. For example, the regular expression
describes a pattern such as
The brackets (
) are an example of metacharacters
, also called regular expression syntax.
Do not confuse regular expressions with the wildcard patterns used to match file names. File-matching wildcards are used by the shell to match the names
of files. By contrast, regular expressions are used by programs to search the contents
of files. Some common
programs that use regular expression syntax are
. Beginners often are confused because some special characters, such as
are used both as regular expression metacharacters and as file name wildcards.
Sometimes, the user wants to search some string of characters in a file, such as to search the record of some student whose family name is Gould in the freshman file of the last section. The greg command can be used to do this.
The name "grep" comes from the g/re/p command of the ed (a Unix line editor). The g/re/p means "globally search for a regular expression and print all lines containing it."
The regular expressions are a set of UNIX rules that can be used to specify one or more items in a singly character string. The regular expressions work like wildcards (in fact, wildcards belong to the regular expressions), that is,
they can save typing in many characters by using representation rules. But the level of support for regular expressions is not the same for different tools. The grep command searches a file or files for lines that have a certain pattern. The syntax is:
$ grep [options] character-string file(s)