Index of Section 1 Manual Pages
| Interix / SUA | zgrep.1 | Interix / SUA |
GREP(1) System General Commands Manual GREP(1)
NAME
grep, egrep, fgrep, zgrep, zegrep, zfgrep - file pattern searcher
SYNOPSIS
grep [-abcEFGHhIiLlnoPqRrSsUvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--context[=num]]
[--line-buffered] [pattern] [file ...]
DESCRIPTION
The grep utility searches any given input files, selecting lines that
match one or more patterns. By default, a pattern matches an input line
if the regular expression (RE) in the pattern matches the input line
without its trailing newline. An empty expression matches every line.
Each input line that matches at least one of the patterns is written to
the standard output.
grep is used for simple patterns and basic regular expressions (BREs);
egrep can handle extended regular expressions (EREs). See re_format(7)
for more information on regular expressions. fgrep is quicker than both
grep and egrep, but can only handle fixed patterns (i.e. it does not
interpret regular expressions). Patterns may consist of one or more
lines, allowing any of the pattern lines to match a portion of the input.
zgrep, zegrep, and zfgrep act like grep, egrep, and fgrep, respectively,
but accept input files compressed with the compress(1) or gzip(1) com-
pression utilities.
The following options are available:
-A num, --after-context=num
Print num lines of trailing context after each match. See also
the -B and -C options.
-a, --text
Treat all files as ASCII text. Normally grep will simply print
``Binary file ... matches'' if files contain binary characters.
Use of this option forces grep to output lines matching the spec-
ified pattern.
-B num, --before-context=num
Print num lines of leading context before each match. See also
the -A and -C options.
-b The offset in bytes of a matched pattern is displayed in front of
the respective matched line.
-C[num], --context=num
Print num lines of leading and trailing context surrounding each
match. The default is 2 and is equivalent to -A 2 -B 2. Note:
no whitespace may be given between the option and its argument.
-c, --count
Only a count of selected lines is written to standard output.
-E, ---extended-regexp
Interpret pattern as an extended regular expression (i.e. force
grep to behave as egrep).
-e pattern, --regexp=pattern
Specify a pattern used during the search of the input: an input
line is selected if it matches any of the specified patterns.
This option is most useful when multiple -e options are used to
specify multiple patterns, or when a pattern begins with a dash
(`-').
-F, --fixed-strings
Interpret pattern as a set of fixed strings (i.e. force grep to
behave as fgrep).
-f file, --file=file
Read one or more newline separated patterns from file. Newlines
are not considered part of a pattern.
-G, --basic-regexp
Interpret pattern as a basic regular expression (i.e. force grep
to behave as traditional grep).
-H If -R is specified, follow symbolic links only if they were
explicitly listed on the command line. The default is not to
follow symbolic links.
-h, --no-filename
Never print filename headers (i.e. filenames) with output lines.
-I Ignore binary files.
-i, --ignore-case
Perform case insensitive matching. By default, grep is case sen-
sitive.
-L, ---files-without-match
Only the names of files not containing selected lines are written
to standard output. Pathnames are listed once per file searched.
If the standard input is searched, the string ``(standard
input)'' is written.
-l, ---files-with-matches
Only the names of files containing selected lines are written to
standard output. grep will only search a file until a match has
been found, making searches potentially less expensive. Path-
names are listed once per file searched. If the standard input
is searched, the string ``(standard input)'' is written.
-n, --line-number
Each output line is preceded by its relative line number in the
file, starting at line 1. The line number counter is reset for
each file processed. This option is ignored if -c, -L, -l, or -q
is specified.
-o Always print filename headers with output lines.
-P If -R is specified, no symbolic links are followed. This is the
default.
-q, --quiet, --silent
Quiet mode: suppress normal output. grep will only search a file
until a match has been found, making searches potentially less
expensive.
-R, --recursive
Recursively search subdirectories listed. To include files in the
current directory specify you may specify the current directory
with a ".". The following options can be validly used only with
recursion.
--include=pattern
When searching recursively include only files matching
pattern.
--index=pattern
Same as --include=pattern.
--exclude=pattern
When searching recursively exclude files matching pat-
tern.
--type When searching recursively include files of the follow-
ing types, in a comma separated list, only.
ace Display files that have additional ACE's
beyond POSIX.
all Display all file types.
blk Display block special files.
char Display character special files.
dir Display directories.
fifo Display FIFO files.
hard Display hard linked files.
r Display readable files.
reg Display regular files.
set Display setuid, setgid and setvtx files.
sock Display socket files.
sym Display symbolic linked files.
unknown Display files of an unknown type (not
FIFO, regular, block special, socket,
symbolic, character special or direc-
tory).
w Display writable files.
x Display executable files.
-r Same as -R.
-S If -R is specified, all symbolic links are followed. The default
is not to follow symbolic links.
-s, --no-messages
Silent mode. Nonexistent and unreadable files are ignored (i.e.
their error messages are suppressed).
-U, --binary
Search binary files, but do not attempt to print them.
-v, --invert-match
Selected lines are those not matching any of the specified pat-
terns.
-w, --word-regexp
The expression is searched for as a word (as if surrounded by
`[[:<:]]' and `[[:>:]]'; see re_format(7)).
-x, ---line-regexp
Only input lines selected against an entire fixed string or regu-
lar expression are considered to be matching lines.
-Z Force grep to behave as zgrep.
--binary-files=value
Controls searching and printing of binary files. Options are
binary, the default: search binary files but do not print them;
without-match: do not search binary files; and text: treat all
files as text.
--context[=num]
Print num lines of leading and trailing context. The default is
2.
--line-buffered
Force output to be line buffered. By default, output is line
buffered when standard output is a terminal and block buffered
otherwise.
If no file arguments are specified, the standard input is used.
RETURN VALUES
The grep utility exits with one of the following values:
0 One or more lines were selected.
1 No lines were selected.
>1 An error occurred.
EXAMPLES
To find all occurrences of the word `patricia' in a file:
$ grep 'patricia' myfile
To find all occurrences of the pattern `.Pp' at the beginning of a line:
$ grep '^\.Pp' myfile
The apostrophes ensure the entire expression is evaluated by grep instead
of by the user's shell. The caret `^' matches the null string at the
beginning of a line, and the `\' escapes the `.', which would otherwise
match any character.
To find all lines in a file which do not contain the words `foo' or
`bar':
$ grep -v -e 'foo' -e 'bar' myfile
A simple example of an extended regular expression:
$ egrep '19|20|25' calendar
Peruses the file `calendar' looking for either 19, 20, or 25.
A simple example of the recursive option is:
$ grep -R hero.
This search for the pattern 'hero' in all files in the current directory
(.) and all files recursively found in all sub-directories.
SEE ALSO
ed(1), ex(1), gzip(1), sed(1), re_format(7)
STANDARDS
The grep utility is compliant with the p1003.1-2004 specification.
The flags [-AaBbCGHhILoPRSUVwZ] are extensions to that specification.
All long options are provided for compatibility with GNU versions of this
utility.
HISTORY
The grep command first appeared in Version 6 AT&T UNIX.
Interix April, 21, 2006 Interix