grep(1) grep(1)
NAME
grep - global regular expression print
SYNOPSIS
grep [option ...] [patternlist] [file ...]
DESCRIPTION
grep 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), grep copies every line that matches one of the pat-
terns to standard output.
grep permits the use of simple 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 pattern with which grep is to compare the individual lines are
specified either using patternlist or via the options -e patternlist
or -f patternfile. At least one of these three arguments must be
specified, multiple specifications are allowed. Multiple specifica-
tions are ORed.
OPTIONS
The options -E and -F are mutually exclusive, as are the options -c,
-l, and -q.
No option specified:
grep 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.
-E (E - extended) You can specify extended regular expressions as
patterns. grep -E behaves in a similar way to egrep.
-F (F - fixed) All patterns are interpreted as character strings.
grep -F behaves in a similar way to fgrep.
-b (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 offset argument for the od(1) command, for
example).
Page 1 Reliant UNIX 5.44 Printed 11/98
grep(1) grep(1)
-c (c - count) grep outputs only the number of lines found (i.e. the
lines that grep would have displayed without the -c option, see
Example 3); the lines themselves are not displayed.
-e patternlist
(e - expression) Synonymous with patternlist. The -e option must
always be specified if the first expression in patternlist begins
with a dash (-). When combined with -e, such a pattern list is
not interpreted as an option, but as a list of patterns with
which grep is to match the individual lines. Multiple specifica-
tions of the -e option are allowed.
-f patternfile
(f - file) grep reads the pattern list from the patternfile file.
Each line in patternfile is interpreted as a simple regular
expression. Multiple specifications of the -f option are allowed.
-h (h - hidden) When searching multiple files, grep does not write
the file name before each output line.
-i (i - ignore) grep does not distinguish between uppercase and
lowercase.
-l (l - list) grep 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 4.) Each
file name is printed just once. The lines themselves are not
displayed.
-n (n - number lines) Each output line is preceded by its line
number in the relevant input file. Line numbering starts at 1. If
grep is reading from standard input, the line number refers to
the standard input.
-q (q - quiet) grep does not produce any output, but returns only
the corresponding exit status.
-r (r - recursive) Names that refer to directories are processed
recursively; in other words, all the files and subdirectories in
that directory are scanned as well.
-s (s - silent) Error messages produced for non-existent files or
files which the user is not authorized to read are suppressed.
-v (v - vice versa) grep outputs all lines that do not match the
specified pattern.
In conjunction with option -c: grep prints only the number of
lines that do not match.
In conjunction with option -l: grep only outputs the names of
files containing such lines.
Page 2 Reliant UNIX 5.44 Printed 11/98
grep(1) grep(1)
In conjunction with option -q: grep returns only the correspond-
ing exit status.
-x (x - exact) grep does not output lines containing one of the
specified patterns and no other characters.
In conjunction with option -c: grep prints only the number of
such lines.
In conjunction with option -l: grep only outputs the names of
files containing such lines.
In conjunction with option -q: grep returns only the correspond-
ing exit status.
-y grep does not distinguish between uppercase and lowercase.
-- End of the list of options. Must be specified if the first argu-
ment begins with -.
patternlist
List of simple regular expressions that grep is to use in search-
ing for matching input lines [see expressions(5)]. Individual
regular expressions must be separated by the newline character. A
newline character within patternlist functions like an OR separa-
tor.
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 grep is to scan. You may name any number of
files.
file not specified: grep 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.
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.
Page 3 Reliant UNIX 5.44 Printed 11/98
grep(1) grep(1)
egrep processes simple regular expressions. These include simple regu-
lar 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.
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed.
The LCCOLLATE environment variable governs the collating sequence.
LCTYPE 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.
EXIT STATUS
0 Lines found
1 No line found
>1 Syntax error or "Cannot open file". This exit status remains valid
even if lines have been found in other input files.
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 Johnson Ltd.
120387 1240.25 3 Units Item 023 Skinner Ltd.
180588 330.87 1 Units Item 332 Skinner Ltd.
customer2:
skinner robert, 16 garden hill, london ec3
Page 4 Reliant UNIX 5.44 Printed 11/98
grep(1) grep(1)
Example 1
Output lines that match a specified pattern (without an option and
with option -i):
$ grep Skinner customer1 customer2
customer1:120387 1240.25 3 Units Item 023 Skinner Ltd.
customer1:180588 330.87 1 Units Item 332 Skinner Ltd.
If you also wish to display lines containing the word skinner in
lowercase you enter:
$ grep -i skinner customer1 customer2
customer1:120387 1240.25 3 Units Item 023 Skinner Ltd.
customer1:180588 330.87 1 Units Item 332 Skinner Ltd.
customer2:skinner robert, 16 garden hill, london ec3
More complicated patterns can be set up with the help of regular
expressions, e.g.:
Display all 1987 entries from the file customer1; these are lines that
contain the number 87 in columns 5 and 6:
$ grep '^....87' customer1
120387 1240.25 3 Units Item 023 Skinner Ltd.
Example 2
Output lines that do not match the specified pattern (option -v):
$ grep -v '^1' customer1 customer2
customer1:080685 999.98 20 Units Item 038 Johnson Ltd.
customer2:morrow lance, 86 sherwood street, london w1
customer2:skinner robert, 16 garden hill, london ec3
Example 3
Display the number of found lines (option -c):
First display the number of lines that start with 1 for each input
file.
$ grep -c '^1' customer1 customer2
customer1:2
customer2:0
The number of lines that do not start with 1 are to be displayed next.
$ grep -c -v '^1' customer1 customer2
customer1:1
customer2:2
Page 5 Reliant UNIX 5.44 Printed 11/98
grep(1) grep(1)
Example 4
Display file names only (option -l):
The names of files containing lines that begin with a 1 are to be out-
put first.
$ grep -l '^1' customer1 customer2
customer1
The names of files containing lines that do not start with 1 are
displayed next.
$ grep -l -v '^1' customer1 customer2
customer1
customer2
Example 5
Display found lines with their line numbers (option -n):
$ grep -n -i skinner customer1 customer2
customer1:2:120387 1240.25 3 Units Item 023 Skinner Ltd.
customer1:3:180588 330.87 1 Units Item 332 Skinner Ltd.
customer2:2:skinner robert, 16 garden hill, london ec3
SEE ALSO
ed(1), egrep(1), fgrep(1), sed(1), sh(1), stdio(3S), expressions(5).
Programmer's Guide, Internationalization - Localization.
Page 6 Reliant UNIX 5.44 Printed 11/98