grep(C)
grep, egrep, fgrep --
search files for a pattern
Syntax
grep [ -E | -F ] [ -c |
-l | -q ] [ -bhinsvxy ] [ -e
expression ] [ -f expfile ]
[ expression ] [ file ... ]
egrep [ -c | -l | -q]
[ -bhinsvxy ] [ -e expression ]
[ -f expfile ] [ expression ]
[ file ... ]
fgrep [ -c | -l | -q ]
[ -bhinsvxy ] [ -e expression ]
[ -f expfile ] [ expression ]
[ file ... ]
Description
Commands of the grep family search the input files (or
standard input if no file argument is specified) for lines
matching a pattern. Normally, each matching line is copied to the
standard output. If more than one file is being searched, the name
of the file in which each match occurs is also written to the
standard output along with the matching line (unless the
-h option is used, see below).
The three grep commands differ significantly in use:
grep-
Searches for fixed strings or basic regular expressions
(patterns). grep regular expressions are described in
regexp(M);
see also
Chapter 12, ``Regular expressions''
in the SCO OpenServer Operating System User's Guide
for an introduction.
egrep-
Searches for extended regular expressions (including expression
groups, and alternatives). Note that the X/Open CAE Specification, Commands and Utilities, Issue 4, Version 2, 1994. marks this
command as obsolescent: use the grep -E option
instead.
fgrep-
Searches for fixed strings. fgrep does not recognize
regular expressions, but can find literal text rapidly. Note that
the X/Open CAE Specification, Commands and Utilities, Issue 4, Version 2, 1994. marks this command as obsolescent: use
the grep -F option instead.
grep and egrep also accept 3-digit octal character
specifications in pattern strings of the form:
\0nnn
The octal form is replaced with the corresponding ASCII character
(for example, ``\0101'' corresponds to ``A''). See
ascii(M)
for a list of characters and their octal equivalents.
The following options are recognized:
-E-
Each pattern is treated as an extended regular expression. A null
extended regular expression matches all lines. (The equivalent of
egrep.)
-F-
Each pattern is treated as a string instead of a regular
expression. A null string matches all lines. (The equivalent of
fgrep.)
-b-
Each line is preceded by the block number on which it was
found. This is sometimes useful in locating disk block numbers by
context.
-c-
Only a count of matching lines is displayed.
-e expression-
Specify patterns to be used during the search. The patterns in the
list must be separated by a newline character. Multiple -e
and -f options are accepted. Unless the -E or
-F options are specified, grep treats the
patterns as basic regular expressions.
-f expfile-
The regular expression for grep, or extended
expression for egrep, or strings list for
fgrep is taken from the expfile. Patterns in the
file are terminated by a newline character. Unless the -E
or -F options are specified, grep treats the
patterns as basic regular expressions.
-h-
Prevents the name of the file containing the matching line from
being printed before that line. Used when searching multiple files.
-i-
Turns on matching of letters of either case in the input so that
case is insignificant. Conversion between uppercase and lowercase
letters is dependent on the locale setting.
-l-
Only the names of files with matching lines are displayed, separated
by newlines.
-n-
Each line is preceded by its relative line number in the file.
-q-
Quiet. Nothing is written to the standard output. grep
exits with status 0 if an input line is selected. This provides a
quick and easy method of testing if a pattern or string exists in a
group of files.
-s-
Suppresses error messages produced for nonexistent or unreadable
files. Other error messages are not suppressed.
-v-
All lines but those matching are displayed.
-x-
Displays only exact matches of an entire line.
The following option is not a standard UNIX system option, and is
maintained for backwards compatibility with XENIX:
-y-
Turns on matching of letters of either case in the input so that
case is insignificant. Conversion between uppercase and lowercase
letters is dependent on the locale setting.
In all cases (except with -h) the filename is output if
there is more than one input file. Care should be taken when using
the characters $ [ ^
| ( ) and \ in
expression, because they are also meaningful to the shell.
It is safest to enclose the entire expression or
strings argument in single quotation marks. For example:
grep '[Ss]omeone' text.file
This command would find all lines containing the word ``someone'' in
the file text.file, whether the initial ``s'' is
uppercase or lowercase.
Multiple strings can be specified without using a separate pattern
file by using the quoting conventions of the shell to imbed newlines
in the string argument. For example, if you were using the
Bourne shell (see
sh(C))
you might enter the following on the command line:
fgrep ´Someone
someone´ text.file
See the
csh(C)
manual page for ways to imbed newlines in a string when using
csh.
grep -E (and egrep) accept regular expressions
as in
ed(C),
with the following additions:
-
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 0 or 1
occurrences of the regular expression.
-
Two regular expressions separated by a vertical bar (|) or by a
newline match strings that are matched by either regular expression.
-
A regular expression may be enclosed in parentheses, (), for
grouping. For example:
egrep ´([Ss]ome|[Aa]ny)one´ text.file
or
grep -E ´([Ss]ome|[Aa]ny)one´ text.file
This example displays all lines in text.file containing
the words ``someone'' or ``anyone'', whether or not they are spelled
with initial capital letters. Without the parentheses, this example
would display all lines containing the words ``some'' or ``anyone''
(because the vertical bar (|) operator is of lower precedence than
concatenation, see below).
The order of precedence of operators is [ ], then ? +, then
concatenation, then backslash (\) with newline or vertical bar (|).
Exit values
Exit status is 0 if any matches are found, 1 if no matches are found,
and 2 for syntax errors or inaccessible files.
Examples
To search the file /etc/passwd for all lines containing
the name ``henry'':
grep -F henry /etc/passwd
To search /etc/passwd for lines containing the name
``henry'' in upper or lower case (or capitalized):
grep -F -i henry /etc/passwd
To search mytext for lines containing any mention of
Henry, in any case and, optionally, with any suffix (for example,
``Henry's'', ``Henrys'''):
grep -i "henry[´s]*" mytext
To search mytext for lines containing ``Henry'' or
``Henrietta'':
grep -E '[Hh]enr(y|ietta)' mytext
(Note the shell quoting conventions in the above examples.)
Limitations
Lines are limited to {LINE_MAX} characters.
Open UNIX 8 compatibility notes
When running ACP on Open UNIX 8 and UnixWare 7 systems,
set OSRCMDS=on to use
the SCO OpenServer version of the <grep> commands.
This provides the expected behaviors
for SCO OpenServer applications.
The SCO OpenServer version of this command
is also provided on Open UNIX 8 systems under the OSP feature
See the
Running SCO OpenServer Applications
topic in the Open UNIX 8 documentation set.
See also
col(C),
coltbl(M),
ed(C),
locale(M),
regexp(M),
sed(C),
sh(C)
Chapter 12, ``Regular expressions''
in the SCO OpenServer Operating System User's Guide
Standards conformance
egrep, fgrep and grep are conformant
with:
ISO/IEC DIS 99452:1992, Information technology Portable Operating System Interface (POSIX) Part 2: Shell and Utilities (IEEE Std 1003.21992);
AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992;
X/Open CAE Specification, Commands and Utilities, Issue 4, Version 2, 1994.: note that egrep and fgrep are
marked as obsolescent.
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003