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
Regular expressions also can include special characters that let you perform wildcard searches. For example, the regular
describes a pattern such as
. The brackets (
) are an example of
, also called regular expression syntax.
Dont 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.
regular expression: A regular expression describes a pattern using literal characters and optional metacharacters known as regular
pattern matching: Pattern matching is the task of using regular expressions to search for text.
string: A string is a sequence of characters.
metacharacter: A metacharacter is a character with special meaning in regular expressions and is not treated literally. Examples include
the * and . metacharacters.
In the next lesson, you will learn how quotes affect the shells interpretation of regular expressions.