egrep(1) egrep(1)
NAME
egrep - search for extended patterns (extended grep)
SYNOPSIS
egrep [option ...] [patternlist] [file ...]
DESCRIPTION
egrep reads lines from one or more files or from standard input and
compares these lines with the specified patterns. Unless told other-
wise (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 expressions(5)].
If you specify more than one input file, the relevant file name will
be displayed before each output line.
The egrep patterns to be used when comparing input lines may be speci-
fied via patternlist, the -e patternlist option, or the -f patternfile
option. At least one of these three arguments must be specified, mul-
tiple specifications are allowed. Multiple specifications of these
arguments are ORed.
OPTIONS
The options -c, -l, and -q are mutually exclusive.
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.
-b (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 con-
secutively from 0.
The -b option is sometimes useful in locating disk block numbers
by context (see the 24offset argument for the od(1) command, for
example).
-c (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.
Page 1 Reliant UNIX 5.44 Printed 11/98
egrep(1) egrep(1)
-e patternlist
(expression) Synonymous with patternlist. The -e option must
always be specified 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. Multiple specification of -e options are
allowed.
-f patternfile
(file) egrep reads the pattern list from the file named pattern-
file. Each line in patternfile is interpreted as an extended reg-
ular expression. Multiple specifications of -f options are
allowed.
-h (hidden) When searching multiple files, egrep does not write the
file name before each output line.
-i
-y (ignore) egrep does not distinguish between uppercase and lower-
case.
-l (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.
-n (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 stan-
dard input.
-q (quiet) egrep does not produce any output, but returns only the
corresponding exit status.
-v (vice versa) egrep outputs all lines that do not match any of the
specified patterns.
In conjunction with option -c: egrep prints only the number of
lines that do not match.
In conjunction with option -l: egrep only outputs the names of
files containing such lines.
In conjunction with option -q: egrep returns only the correspond-
ing exit status.
Page 2 Reliant UNIX 5.44 Printed 11/98
egrep(1) egrep(1)
-x (exact) egrep only outputs the lines containing one of the speci-
fied patterns and no other characters.
In conjunction with option -c: egrep prints only the number of
such lines.
In conjunction with -l: egrep only outputs the names of files
containing such lines.
In conjunction with option -q: egrep returns only the correspond-
ing exit status.
-- End of the list of options. Must be specified if one of the argu-
ments begins with -.
patternlist
A list of extended regular expressions that egrep is to use in
searching for matching input lines [see expressions(5)]. Indivi-
dual regular expressions must be separated by the newline charac-
ter. 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'.
file Name of the file that egrep is to scan. You may name any number
of files.
file not specified: egrep reads its input from standard input.
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 if the options -E or -F are
not specified.
Page 3 Reliant UNIX 5.44 Printed 11/98
egrep(1) egrep(1)
fgrep only processes character strings. fgrep can search for a large
number of character strings very efficiently: fgrep searches each
individual line for all of the character strings. fgrep behaves in a
similar way to grep -F.
egrep processes extended regular expressions. These include simple
regular expressions, with the following exception: The simple regular
expressions \(regexpression\) and \{...\} have no special meaning for
extended regular expressions and are therefore not processed by egrep.
egrep behaves in a similar way to grep -E.
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 LCMESSAGES environment variable governs the language in which
message texts are displayed.
The LCCOLLATE environment variable governs the collating sequence.
LCCTYPE governs character classes and character conversion (shifting,
option -i/-y).
If LCMESSAGES, LCCOLLATE or LCCTYPE is undefined or is defined as
the null string, it defaults to the value of LANG. If LANG is likewise
undefined or null, the system acts as if it were not international-
ized.
If any of the locale variables has an invalid value, the system acts
as if none of the variables were set.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
Page 4 Reliant UNIX 5.44 Printed 11/98
egrep(1) egrep(1)
EXAMPLES
The files customer1 and customer2 will be used as a basis for all fol-
lowing examples. Their contents are given below:
customer1:
080685 999.98 20 Units Item 038 Nicholson 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):
$ egrep Robinson customer1 customer2
customer1:120387 1240.25 3 Units Item 023 Robinson Ltd.
customer1:180588 330.87 1 Units Item 332 Robinson Ltd.
If you also wish to find lines containing the word robinson in lower-
case you enter:
$ egrep -i robinson customer1 customer2
customer1:120387 1240.25 3 Units Item 023 Robinson Ltd.
customer1:180588 330.87 1 Units Item 332 Robinson Ltd.
customer2:robinson peter, 16 garden hill, london ec3
More complicated patterns can be set up with the help of regular
expressions, e.g.: Display lines which contain the string Nicholson or
Robinson:
$ egrep '(Nichol|Robin)son' customer1 customer2
customer1:080685 999.98 20 Units Item 038 Nicholson Ltd.
customer1:120387 1240.25 3 Units Item 023 Robinson Ltd.
customer1:180588 330.87 1 Units Item 332 Robinson Ltd.
Instead of the regular expression (Nichol|Robin)son you could also use
the following regular expression:
(Nicholson|Robinson)
In this case you can leave out the parentheses;
Nicholson|Robinson
The OR operator (|) in the last expression Nicholson|Robinson could
also be replaced by a newline character (see Example 2).
Page 5 Reliant UNIX 5.44 Printed 11/98
egrep(1) egrep(1)
Example 2
Using several patterns (without an option and with option -f):
$ egrep '^1
> 1$' customer1 customer2
customer1:120387 1240.25 3 Units Item 023 Robinson Ltd.
customer1:180588 330.87 1 Units Item 332 Robinson Ltd.
customer2:morrow lance, 86 sherwood street, london w1
Alternatively, you could write both patterns into a file called names
(each pattern in a separate line) and then call egrep as follows:
$ egrep -f names customer1 customer2
The same result is obtained when the newline character that separates
the patterns ^1 and 1$ is replaced by the OR operator:
$ egrep '^1|1$' customer1 customer2
Example 3
Output lines that match none of the specified patterns (option -v):
$ egrep -v '^1
> 1$' customer1 customer2
customer1:080685 999.98 20 Units Item 038 Nicholson Ltd.
customer2:robinson peter, 16 garden hill, london ec3
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 1):
$ egrep -v '^1|1$' customer1 customer2
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.
$ egrep -c '^1' customer1 customer2
customer1:2
customer2:0
The number of lines that do not begin with 1 are now to be displayed.
$ egrep -c -v '^1' customer1 customer2
customer1:1
customer2:2
Page 6 Reliant UNIX 5.44 Printed 11/98
egrep(1) egrep(1)
Example 5
Display file names only (option -l):
The names of files containing lines that begin with 1 are to be output
first.
$ egrep -l '^1' customer1 customer2
customer1
The names of files containing lines that do not start with 1 are
displayed next.
$ egrep -l -v '^1' customer1 customer2
customer1
customer2
Example 6
Display found lines with relevant line numbers (option -n):
$ egrep -n -i robinson customer1 customer2
customer1:2:120387 1240.25 3 Units Item 023 Robinson Ltd.
customer1:3:180588 330.87 1 Units Item 332 Robinson Ltd.
customer2:2:robinson peter, 16 garden hill, london ec3
SEE ALSO
ed(1), fgrep(1), grep(1), sed(1), sh(1), expressions(5).
Page 7 Reliant UNIX 5.44 Printed 11/98