grep(1)
_________________________________________________________________
grep, egrep, fgrep Command
search a file for a pattern
_________________________________________________________________
SYNTAX
grep [ options ] expression [ files ]
egrep [ options ] [ expression ] [ files ]
fgrep [ options ] [ string ] [ files ]
DESCRIPTION
Commands of the grep family search the input files (standard
input default) for lines matching a pattern. Normally, each line
found is copied to the standard output. Grep patterns are
limited regular expressions in the style of ed(1); it uses a
compact non-deterministic algorithm. Egrep patterns are full
regular expressions; it uses a fast deterministic algorithm that
sometimes needs exponential space. Fgrep patterns are fixed
strings; it is fast and compact. Options are:
-v All lines but those matching are printed.
-x (Exact) only lines matched in their entirety are printed
(fgrep only).
-c Only a count of matching lines is printed.
-l Only the names of files with matching lines are listed
(once), separated by new-lines.
-n Each line is preceded by its relative line number in the
file.
-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.
-s The error messages produced for nonexistent or unreadable
files are suppressed (grep only).
-e expression
Same as a simple expression argument, but useful when the
expression begins with a - (does not work with grep).
-f file
The regular expression (egrep) or strings list (fgrep) is
taken from the file. For fgrep, each line of the file is
taken to contain a separate string.
-i Ignore upper/lowercase distinction during comparisons.
In all cases, the file name is output if there is more than one
input file. Take care when using the characters $, *, [, ^, |,
(, ), and \ in expression, because they are also meaningful to
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
grep(1)
the shell. It is safest to enclose the entire expression
argument in single quotes '...'.
Fgrep searches for lines that contain the string or (if the -f
option is given) any of the strings.
Egrep accepts regular expressions as in ed(1), except for \( and
\), with the addition of:
1. A regular expression followed by + matches one or more
occurrences of the regular expression.
2. A regular expression followed by ? matches 0 or 1
occurrences of the regular expression.
3. Two regular expressions separated by | or by a new-line
match strings that are matched by either.
4. A regular expression may be enclosed in parentheses () for
grouping.
The order of precedence of operators is [], then *?+, then
concatenation, then | and new-line.
_________________________________________________________________
EXAMPLES
$ grep root /etc/passwd
Prints the lines in the file "/etc/passwd" that contain the login
name "root".
$ who | grep "xyz"
Prints the name, terminal number, and time that the user with
login name "xyz" logged in if "xyz" is logged in. If "xyz" is
not logged in, this command line prints nothing.
$ grep rsh /etc/passwd|cut -d: -f5
Searches the "/etc/passwd" file for users who run a restricted
shell, rsh(1). Then cut(1) prints the fifth field of every line
that grep identifies. The fifth field contains the users'
names.
$ egrep fs2 /etc/passwd
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)
grep(1)
Searches through the file "/etc/passwd" and prints all lines
containing the pattern "fs2" on the standard output.
$ egrep -l -e -ooutfile src/*
Searches through all the files in the subdirectory "src" for all
lines containing the regular expression "-ooutfile". Prints the
names of the files containing the pattern.
$ egrep 'int|long' prog.c
Searches through the file "prog.c" in the current working
directory for all lines containing the pattern "int" or the
pattern "long". The "|" character stands for logical "or".
Prints all of the lines that contain "int" or "long" on the
standard output.
$ ps -af | fgrep -x -f expfile
Searches through the list of active processes for lines that
entirely match the lines in "expfile".
$ find . -exec | fgrep -l attachment {} \;
Prints the names of all files under the current working directory
that contain the string "attachment".
_________________________________________________________________
SEE ALSO
ed(1), sed(1), sh(1).
DIAGNOSTICS
Exit status is 0 if any matches are found, 1 if none, 2 for
syntax errors or inaccessible files (even if matches were found).
CAVEATS
Ideally there should be only one grep, but we do not know a
single algorithm that spans a wide enough range of space-time
tradeoffs.
DG/UX 4.00 Page 3
Licensed material--property of copyright holder(s)
grep(1)
Lines are limited to BUFSIZ characters; longer lines are
truncated. (BUFSIZ is defined in /usr/include/stdio.h.)
If a line has embedded nulls, grep will match only up to the
first null; if it matches, it will print the entire line.
DG/UX 4.00 Page 4
Licensed material--property of copyright holder(s)