[Previous] [Contents] [Next]

grep

Search for string patterns (POSIX)

Syntax:

grep [-E|-F] [-c|-f|-q] [-hinsvx] 
     [-e expression|-f expression_file]...  [file...]
grep [-E|-F] [-c|-f|-q] [-hinsvx] 
     expression [file...]

Historical UNIX versions:

egrep [-chilnqsvx] [-e expression|-f expression_file]...
      [file...]

egrep [-chilnqsvx] expression [file...]


fgrep [-chilnqsvx] [-e expression|-f expression_file]...
      [file...]

fgrep [-chilnqsvx] expression [file...]

Options:

-c
Write only a count of selected lines to standard output.
-e expression
A regular expression, of type determined by the -E, -F options. This form is used when more than one expression needs to be specified on the command line. The -e option can be repeated.
-E
Use extended regular expression (ERE) syntax.
-f expression_file
File containing a set of regular expressions, each separated by a newline. The type of the expressions is determined by the -E and -F options. This form is used when more than one expression needs to be specified. The -f option can be repeated.
-F
Treat expression as a fixed string instead of a regular expression. (Search for a fixed string or fixed strings.)
-h
(QNX extension) Don't prefix matched lines with a filename. This option applies only when grep is invoked with more than one file to search.
-i
Ignore uppercase and lowercase distinctions during comparisons.
-l
("el") Write only the names of files containing selected lines to standard output.
-n
Before each output line, display the line's line number in the file.
-q
Be quiet; don't write anything to the standard output, regardless of matching lines.
-s
Suppress the error messages ordinarily written for non-existent or unreadable files. Other error messages aren't suppressed.
-v
Select only those lines that don't match the specified patterns.
-x
Consider as matching lines only input lines selected against an entire fixed string or regular expression.
expression
A regular expression, whose type is determined by the -E and -F options. This form is used when only one expression is specified on the command line. Any names specified after this option are treated as input files.
file
The text file to be input. The default is standard input.

Description:

The grep utility searches input for lines matching the expression(s) given. When an input line matches any of the expressions, it is said to be "selected." By default, selected lines are written to standard output.

Numerous options allow variations upon the output format. For example, to reverse the meaning of the output, the -v option could be used.

There are three types of expressions understood by grep: Basic Regular Expressions, Extended Regular Expressions, and Fixed Regular Expressions. If you don't specify -E or -F, the expression(s) are taken to be Basic Regular Expressions.

Basic and Extended Regular expressions are similar to arithmetic expressions in that larger expressions are formed by combining smaller expressions and operators according to some precedence rule.

Regular expressions have an "invisible" operator, i.e. concatenation. The concatenation of two expressions means match the one on the left, then the one on the right.

The smallest expression is a single character.

Basic regular expressions

The following table summarizes the Basic Regular Expressions, and the precedence of the operators:

Expression Meaning
\( expression \) subexpression. Match the pattern expression. Used for backreferences (see below), and precedence
\N back-reference. Match the exact string that the Nth subexpression did
. (dot) match any single character
[charset] match any member of the set charset (see below)
c match any non-special character
\c Match literal c. The character may not be (, ), {, }, or any digit from 1 through 9. The \ is usually used to escape *, $, ^, ., [ and ]. \\ matches a literal "\". \ has no special meaning inside a bracket expression.
limited_expression* match any number of repetitions of limited_expression including zero.
limited_expression\{M\} match exactly M repetitions of limited_expression
limited_expression\{,N\} match zero to N repetitions of limited_expression
limited_expression\{M,N\} match M to N repetitions of limited_expression
expr0expr1 (concatenation) match expr0 then expr1
^expression match expression only at beginning of line
expression$ match expression only at end of line

A limited_expression is restricted to a back-reference, a subexpression, or a BRE matching a single character.

A charset is formed by concatenation of the following operators:

Expression Meaning
c any character c
c-d any character in the range from c to d
[:alpha:] any alphabetic character
[:upper:] any uppercase character
[:lower:] any lowercase character
[:digit:] any numeric character
[:alnum:] any numeric or alphabetic character
[:xdigit:] any character used to represent a hexadecimal number
[:space:] any character that is a whitespace
[:print:] any printable character
[:punct:] any character that is punctuation
[:graph:] any character with a graphic representation
[:cntrl:] any character used for control

If the charset begins with the caret (^), the set is inverted. For example:

    [^[:alpha:]]

means match any non-alphabetic character. (This can also be expressed by [^a-zA-Z].)

Extended Regular Expressions

The Extended Regular Expressions are an enriched set of regular expression operators. In particular, the Extended Regular Expressions support an operator for alternation, thus allowing a match of one expression or another. It is also important to note that the parenthesis syntax is different from Basic Regular Expressions, and the semantics are subtly different. There are no back-references in Extended Regular Expressions.

The following list summarizes the Extended Regular Expressions:

Expression Meaning
(expression) match expression; useful for altering precedence
. (dot) match any single character
c match any non-special character c
\c Match literal c. Normally used to escape ERE special characters.
[charset] match any element of charset
limited_expression* match any number of repetitions of limited_expression, including zero
limited_expression+ match 1 to any number of repetitions of limited_expression
limited_expression? limited_expression is optional (match 0 or 1 repetition)
limited_expression\{M\} match exactly M repetitions of limited_expression
limited_expression\{,N\} match zero to N repetitions of limited_expression
limited_expression\{M,N\} match M to N repetitions of limited_expression
expr0expr1 (concatenation) match expr0 then expr1
expr0|expr1 (alternation) match expr0 or expr1 (not both)
^expression match expression only at the beginning of a line
expression$ match expression only at the end of a line

For extended regular expressions a limited_expression is restricted to an expression matching a single character or an expression enclosed in parentheses.

Fixed Regular Expressions

Fixed Regular Expressions consist of a set of strings of characters. They do not permit the operators of Extended or Basic Regular Expressions. The algorithm used is extremely efficient for locating one of a set of strings within another string. Thus, if you don't need the various operators of Basic or Extended Regular Expressions, the Fixed Expressions are a better choice.

Examples:

Display lines in Phone.List containing telephone numbers:

grep '[[:digit:]]\{3\}-[[:digit:]]\{4\}' Phone.List

Display all the people logged in who are listed in the MyFriends file:

who | grep -F -f MyFriends

Display all occurrences of the words "steve" and "barney" in the Phone.List file:

grep -F -e steve -e barney Phone.List

Files:

The grep utility will read lines from the file(s) named on the command line including the standard input for each command-line item which is -, or from the standard input if no files are named on the command line.

Lines matching the search criteria will be written to the standard output. When multiple input files have been specified on the command line, each line in the output will be preceded by the name of the source file and a colon (:). If the input file was standard input specified by a dash (-) on the command line, the line will be prefixed by "stdin: ".

If an error occurs a diagnostic message is written to the standard error.

Exit status:

0
Lines were found matching the expression provided.
>0
An error occurred or no matching lines were found.

See also:

awk, lex, sed


[Previous] [Contents] [Next]