egrep Command

Purpose

Searches a file for a pattern.

Syntax

egrep [ -h ] [ -i ] [ -p[ Separator ] ] [ -s ] [ -u ] [ -v ] [ -w ] [ -x ] [ -y ] [ [ -b ] [ -n ] | [ -c | -l | -q ] ] { { -ePattern | -fStringFile } ... | Pattern } [ File ... ]

Description

The egrep command searches an input file (standard input by default) for lines matching a pattern specified by the Pattern parameter. These patterns are full regular expressions as in the ed command (except for the \ (backslash) and \\ (double backslash)). The following rules also apply to the egrep command:

  • A regular expression followed by a + (plus sign) matches one or more occurrences of the regular expression.
  • A regular expression followed by a ? (question mark) matches zero or one occurrence of the regular expression.
  • Multiple regular expressions separated by a | (vertical bar) or by a new-line character match strings that are matched by any of the regular expressions.
  • A regular expression may be enclosed in ( ) (parentheses) for grouping.

The new-line character will not be matched by the regular expressions.

The order of precedence for operators is [, ], *, ?, +, concatenation, | and the new-line character.

Note: The egrep command is the same as the grep command with the -E flag, except that error and usage messages are different and the -s flag functions differently.

The egrep command displays the file containing the matched line if you specify more than one File parameter. Characters with special meaning to the shell ($, *, [, |, ^, (, ), \ ) must be in quotation marks when they appear in the Pattern parameter. When the Pattern parameter is not a simple string, you usually must enclose the entire pattern in single quotation marks. In an expression such as [a-z], the minus means through according to the current collating sequence. A collating sequence may define equivalence classes for use in character ranges. It uses a fast, deterministic algorithm that sometimes needs exponential space.

Notes:
  1. Lines are limited to 2048 bytes.
  2. Paragraphs (under the -p flag) are currently limited to a length of 5000 characters.
  3. Do not run the grep command on a special file because it produces unpredictable results.
  4. Input lines should not contain the NULL character.
  5. Input files should end with the newline character.
  6. Although some flags can be specified simultaneously, some flags override others. For example, if you specify -l and -n together, only file names are written to standard output.

Flags

Item Description
-b Precedes each line by the block number on which it was found. Use this flag to help find disk block numbers by context. The -b flag cannot be used with input from stdin or pipes.
-c Displays only a count of matching lines.
-e Pattern Specifies a Pattern. This works like a simple Pattern but is useful when the Pattern begins with a - (minus sign).
-f StringFile Specifies a file that contains strings.
-h Suppresses file names when multiple files are being processed.
-i Ignores the case of letters when making comparisons.
-l Lists just the names of files (once) with matching lines. Each file name is separated by a new-line character. If standard input is searched, a path name of "(StandardInput)" is returned.
-n Precedes each line with its relative line number in the file. 
-p[Separator] Displays the entire paragraph containing matched lines. Paragraphs are delimited by paragraph separators, as specified by the Separator parameter, which are patterns in the same form as the search pattern. Lines containing the paragraph separators are used only as separators; they are never included in the output. The default paragraph separator is a blank line.
-q Suppresses all output to standard output, regardless of matching lines. Exits with a 0 status if an input line is selected.
-s Displays only error messages. This is useful for checking status.
-u Causes output to be unbuffered.
-v Displays all lines except those that match the specified pattern.
-w Does a word search.
-x Displays lines that match the specified pattern exactly with no additional characters.
-y Ignores the case of letters when making comparisons.

Exit Status

This command returns the following exit values:

Item Description
0 A match was found.
1 No match was found.
>1 A syntax error was found or a file was inaccessible (even if matches were found).

Examples

To use an extended pattern that contains some of the pattern-matching characters +, ?, |, (, and ), enter:

  egrep "\(([A-z]+|[0-9]+)\)" my.txt

This displays lines that contain letters in parentheses or digits in parentheses, but not parenthesized letter-digit combinations. It matches (y) and (783902), but not (alpha19c).

Note: When using the egrep command, \ ( (backslash followed by open parenthesis) or \ ( (backslash followed by close parenthesis) match parentheses in the text, but ( (open parenthesis) and ) (closed parenthesis) are special characters that group parts of the pattern. The reverse is true when using the grep command.

Files

Item Description
/usr/bin/egrep Contains the hard link to the egrep command.
/bin/egrep Specifies the symbolic link to the egrep command.