egrep reads lines from one or more files or from standard input and compares these lines with the specified patterns. Unless told otherwise (by options), egrep copies every line that matches one of the patterns to standard output. egrep permits the use of extended regular expressions in the specified pattern (see section “Regular POSIX shell expressions”). If you specify more than one input file, the relevant file name will be displayed before each output line.
Syntax
Format 1: |
egrep[ -bchilnvy] -e patternlist[ file...] |
Format 2: |
egrep[ -bchilnvy] -f patternlist[ file...] |
Format 3: |
egrep[ -bchilnvy] patternlist[ file...] |
The formats are described together since the patterns which egrep uses to compare the input lines are specified either via patternlist or using the option -e patternlist or -f patternlist. You must specify one of these arguments. Two of them or all three together are not permitted. No option specified egrep outputs all lines that match at least one of the patterns specified in patternlist. If you specify more than one input file, each output line will be preceded by the name of the file in which the line was found.
(block) Each output line is preceded by the number of the block in which it was found. Each file is made up of 512-byte blocks which are numbered consecutively from 0. The -b option is sometimes useful in locating disk block numbers by context (see the offset argument for the od command, for example).
(count) egrep outputs only the number of lines found (i.e. the lines that egrep would have displayed without the -c option, see Example 4); the lines themselves are not displayed.
(hidden) When searching multiple files, egrep does not write the file name before each output line.
(ignore) egrep does not distinguish between uppercase and lowercase
(list) egrep simply outputs the names of files that contain at least one of the matching lines. (These are the lines that egrep would output if the -l option were omitted, see Example 5.) Each file name is printed just once. The lines themselves are not displayed.
(number lines) Each output line is preceded by its line number in the relevant input file. Line numbering starts at 1. If egrep is reading from standard input, the line number refers to the standard input. (v - vice versa) egrep outputs all the lines which correspond to none of the specified patterns. Together with option -c: Together with option -l:
(expression) You will need this option if the first expression in patternlist begins with a minus sign. When used in conjunction with the -e option, a patternlist of this type is not interpreted as an option itself but as a list of patterns which egrep is to use in searching for matching input lines.
(file) egrep reads the pattern list from the file named patternfile. Each line in patternfile is interpreted as an extended regular expression.
A list of extended regular expressions that egrep is to use in searching for matching input lines (see section “Regular POSIX shell expressions”). Individual regular expressions must be separated by the newline character. Any newline character within patternlist is interpreted like an OR separator (|) in an extended regular expression.Regular expressions of the type (r|s) can also be specified without the parentheses: r|s (see Example 1). If patternlist contains newline characters or other characters that have a special meaning for the shell, you must enclose the specified patternlist in single quotes: ’patternlist’. If the first expression in patternlist begins with a minus sign, you must specify patternlist along with the -e option or terminate the option list with -- to prevent the patternlist from being interpreted as an option itself.
Name of the file that egrep is to scan. You may name any number of files. file not specified: |
grep, fgrep and egrep
The grep, fgrep and egrep commands perform similar functions and are largely identical in terms of usage. The following section lists the most important differences between these commands. grep processes simple regular expressions. Only one regular expression may be specified in each call. fgrep processes strings only. However, you may specify several strings in one call. The strings can either be entered directly in the command line, separated by newline characters, or passed to fgrep from within a file. fgrep is fast and compact and can search for a large number of strings. All specified strings are searched for in each individual line. egrep processes extended regular expressions. Among other things, these include all simple regular expressions with one exception: the \(...\) construct used in simple regular expressions does not have a special meaning in extended regular expression syntax and is hence not processed by egrep. Several regular expressions can be specified together, separated by newline characters. egrep interprets these newline characters as an OR operator (the vertical bar character; see Tables and directories, Regular POSIX shell expressions). The regular expressions can either be specified directly in the command line or passed to egrep via a file. |
Exit status
0 | Matching lines found |
1 | No matching lines found |
>1 | Syntax error or file cannot be opened. This exit status remains valid even if lines have been found in other input files. |
Locale
The following environment variables affect the execution of egrep: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example
The files customer1 and customer2 will be used as a basis for all following examples. Their contents are given below: customer1:080685 999.98 20 Units Item 038 Nicolson Ltd. 120387 1240.25 3 Units Item 023 Robinson Ltd. 180588 330.87 1 Units Item 332 Robinson Ltd. customer2: morrow lance, 86 sherwood street, london w1 robinson peter, 16 garden hill, london ec3 |
Example 1
Output lines that match a pattern (without an option and with option -i):
If you also wish to find lines containing the word robinson in lowercase you enter:
More complicated patterns can be set up with the help of regular expressions, e.g.: Display lines which contain the string Nicolson or Robinson:
Instead of the regular expression (Nicol|Robin)son you could also use the following regular expression:
In this case you can leave out the parentheses;
The OR operator (|) in the last expression Nicolson|Robinson could also be replaced by a newline character (see Example 2). |
Example 2
Using several patterns (without an option and with option -f):
Alternatively, you could write both patterns into a file called names (each pattern in a separate line) and then call egrep as follows:
The same result is obtained when the newline character that separates the patterns ^1 and 1$ is replaced by the OR operator:
|
Example 3
Output lines that match none of the specified patterns (option -v):
The above call thus yields all lines that neither begin nor end with 1. The same result can also be obtained with the following call (see Example 2):
|
Example 4
Display the number of lines found (option -c): To begin with, the number of lines that start with 1 are to be output for each input file.
The number of lines that do not begin with 1 are now to be displayed.
|
Example 5
Display file names only (option -l): The names of files containing lines that begin with 1 are to be output first.
The names of files containing lines that do not start with 1 are displayed next.
|
Example 6
Display found lines with relevant line numbers (option -n):
|
See also
ed, fgrep, grep, sed |