GREP(1V) USER COMMANDS GREP(1V)
NAME
grep, egrep, fgrep - search a file for a string or regular
expression
SYNOPSIS
grep [ -bchilnsvw ] [ -e expression ] [ filename... ]
egrep [ -bchilnsv ] [ -e expression ] [ -f filename ]
[ expression ] [ filename... ]
fgrep [ -bchilnsvx ] [ -e string ] [ -f filename ]
[ string ] [ filename... ]
SYSTEM V SYNOPSIS
/usr/5bin/grep [ -bchilnsvw ] [ -e expression ]
[ filename... ]
AVAILABILITY
The System V version of this command is available with the
System V software installation option. Refer to Installing
SunOS 4.1 for information on how to install optional
software.
DESCRIPTION
Commands of the grep family search the input filenames (the
standard input default) for lines matching a pattern. Nor-
mally, each line found is copied to the standard output.
grep patterns are limited regular expressions in the style
of ed(1). egrep patterns are full regular expressions
including alternation. fgrep patterns are fixed strings -
no regular expression metacharacters are supported.
In general, egrep is the fastest of these programs.
Take care when using the characters `$', `*', [, `^', `|',
`(', `)', and `\' in the expression, as these characters are
also meaningful to the shell. It is safest to enclose the
entire expression argument in single quotes '...'.
When any of the grep utilities is applied to more than one
input file, the name of the file is displayed preceding each
line which matches the pattern. The filename is not
displayed when processing a single file, so if you actually
want the filename to appear, use /dev/null as a second file
in the list.
OPTIONS
-b Precede each line by the block number on which it was
found. This is sometimes useful in locating disk block
numbers by context.
-c Display a count of matching lines rather than
displaying the lines which match.
-h Do not display filenames.
-i Ignore the case of letters in making comparisons - that
is, upper and lower case are considered identical.
-l List only the names of files with matching lines (once)
separated by NEWLINE characters.
-n Precede each line by its relative line number in the
file.
-s Work silently, that is, display nothing except error
messages. This is useful for checking the error
status.
-v Invert the search to only display lines that do not
match.
-w Search for the expression as a word as if surrounded by
\< and \>. This applies to grep only.
-x Display only those lines which match exactly - that is,
only lines which match in their entirety. This applies
to fgrep only.
-e expression
Same as a simple expression argument, but useful when
the expression begins with a `-'.
-e string
For fgrep the argument is a literal character string.
-f filename
Take the regular expression (egrep) or a list of
strings separated by NEWLINE (fgrep) from filename.
SYSTEM V OPTIONS
The -s option to grep indicates that error messages for
nonexistent or unreadable files should be suppressed, not
that all messages except for error messages should be
suppressed.
REGULAR EXPRESSIONS
The following one-character regular expressions match a sin-
gle character:
c An ordinary character (not one of the special charac-
ters discussed below) is a one-character regular
expression that matches that character.
\c A backslash (\) followed by any special character is a
one-character regular expression that matches the spe-
cial character itself. The special characters are:
o+ `.', `*', `[', and `\' (period, asterisk,
left square bracket, and backslash, respec-
tively), which are always special, except
when they appear within square brackets ([]).
o+ `^' (caret or circumflex), which is special
at the beginning of an entire regular expres-
sion, or when it immediately follows the left
of a pair of square brackets ([]).
o+ $ (currency symbol), which is special at the
end of an entire regular expression.
A backslash followed by one of `<', `>', `(', `)', `{', or
`}', represents a special operator in the regular expres-
sion; see below.
. A `.' (period) is a one-character regular expression
that matches any character except NEWLINE.
[string]
A non-empty string of characters enclosed in square
brackets is a one-character regular expression that
matches any one character in that string. If, however,
the first character of the string is a `^' (a circum-
flex or caret), the one-character regular expression
matches any character except NEWLINE and the remaining
characters in the string. The `^' has this special
meaning only if it occurs first in the string. The `-'
(minus) may be used to indicate a range of consecutive
ASCII characters; for example, [0-9] is equivalent to
[0123456789]. The `-' loses this special meaning if it
occurs first (after an initial `^', if any) or last in
the string. The `]' (right square bracket) does not
terminate such a string when it is the first character
within it (after an initial `^', if any); that is,
[]a-f] matches either `]' (a right square bracket ) or
one of the letters a through f inclusive. The four
characters `.', `*', `[', and `\' stand for themselves
within such a string of characters.
The following rules may be used to construct regular expres-
sions:
* A one-character regular expression followed by `*' (an
asterisk) is a regular expression that matches zero or
more occurrences of the one-character regular expres-
sion. If there is any choice, the longest leftmost
string that permits a match is chosen.
\(and\)
A regular expression enclosed between the character
sequences \( and \) matches whatever the unadorned reg-
ular expression matches. This applies only to grep.
\n The expression \n matches the same string of characters
as was matched by an expression enclosed between \( and
\) earlier in the same regular expression. Here n is a
digit; the sub-expression specified is that beginning
with the nth occurrence of \( counting from the left.
For example, the expression ^\(.*\)\1$ matches a line
consisting of two repeated appearances of the same
string.
Concatenation
The concatenation of regular expressions is a regular
expression that matches the concatenation of the strings
matched by each component of the regular expression.
\< The sequence \< in a regular expression constrains the
one-character regular expression immediately following
it only to match something at the beginning of a
"word"; that is, either at the beginning of a line, or
just before a letter, digit, or underline and after a
character not one of these.
\> The sequence \> in a regular expression constrains the
one-character regular expression immediately following
it only to match something at the end of a "word"; that
is, either at the end of a line, or just before a char-
acter which is neither a letter, digit, nor underline.
\{m\}
\{m,\}
\{m,n\}
A regular expression followed by \{m\}, \{m,\}, or
\{m,n\} matches a range of occurrences of the regular
expression. The values of m and n must be non-negative
integers less than 256; \{m\} matches exactly m
occurrences; \{m,\} matches at least m occurrences;
\{m,n\} matches any number of occurrences between m and
n inclusive. Whenever a choice exists, the regular
expression matches as many occurrences as possible.
^ A circumflex or caret (^) at the beginning of an entire
regular expression constrains that regular expression
to match an initial segment of a line.
$ A currency symbol ($) at the end of an entire regular
expression constrains that regular expression to match
a final segment of a line.
The construction
example% ^entire regular expression $
constrains the entire regular expression to match the entire
line.
egrep accepts regular expressions of the same sort grep
does, except for \(, \), \n, \<, \>, \{, and \}, with the
addition of:
* A regular expression (not just a one-
character regular expression) followed by `*'
(an asterisk) is a regular expression that
matches zero or more occurrences of the one-
character regular expression. If there is
any choice, the longest leftmost string that
permits a match is chosen.
+ A regular expression followed by `+' (a plus
sign) is a regular expression that matches
one or more occurrences of the one-character
regular expression. If there is any choice,
the longest leftmost string that permits a
match is chosen.
? A regular expression followed by `?' (a ques-
tion mark) is a regular expression that
matches zero or one occurrences of the one-
character regular expression. If there is
any choice, the longest leftmost string that
permits a match is chosen.
| Alternation: two regular expressions
separated by `|' or NEWLINE match either a
match for the first or a match for the
second.
() A regular expression enclosed in parentheses
matches a match for the regular expression.
The order of precedence of operators at the same parenthesis
level is `[ ]' (character classes), then `*' `+' `?'
(closures),then concatenation, then `|' (alternation)and
NEWLINE.
EXAMPLES
Search a file for a fixed string using fgrep:
example% fgrep intro /usr/share/unixworld/man/man3/*.3*
Look for character classes using grep:
example% grep '[1-8]([CJMSNX])' /usr/share/unixworld/man/man1/*.1
Look for alternative patterns using egrep:
example% egrep '(Sally|Fred) (Smith|Jones|Parker)' telephone.list
To get the filename displayed when only processing a single
file, use /dev/null as the second file in the list:
example% grep 'Sally Parker' telephone.list /dev/null
FILES
/dev/null
SEE ALSO
awk(1), ed(1), ex(1), sh(1), vi(1), sed(1V)
BUGS
Lines are limited to 1024 characters by grep; longer lines
are truncated.
The combination of -l and -v options does not produce a list
of files in which a regular expression is not found. To get
such a list, use the Bourne shell construct:
for filename in *
do
if [ `grep "re" $filename | wc -l` -eq 0 ]
then
echo $filename
fi
done
or the C shell construct:
foreach filename (*)
if (`grep "re" $filename | wc -l` == 0) echo $filename
end
Ideally there should be only one grep.
DIAGNOSTICS
Exit status is 0 if any matches are found, 1 if none, 2 for
syntax errors or inaccessible files.
Sun Release 4.1 Last change: 30 November 1988
REPORTS
Analyize In-Line NAC strategies and products.
ANALYTICS Plan and design your enterprise blade server deployments
InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today