Main index | Section 1 | Options |
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.
The following options are available:
| |
Print
num
lines of trailing context after each match.
See also the
| |
| |
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 specified pattern. | |
| |
Print
num
lines of leading context before each match.
See also the
| |
| |
The offset in bytes of a matched pattern is displayed in front of the respective matched line. | |
| |
Print
num
lines of leading and trailing context surrounding each match.
See also the
| |
| |
Only a count of selected lines is written to standard output. | |
| |
Mark up the matching text with the expression stored in the GREP_COLOR environment variable. The possible values of when are " never", " always" and " auto". | |
| |
Specify the demanded action for devices, FIFOs and sockets. The default action is " read", which means, that they are read as if they were normal files. If the action is set to " skip", devices are silently skipped. | |
| |
Specify the demanded
action
for directories.
It is
" read"
by default, which means that the directories
are read in the same manner as normal files.
Other possible values are
" skip"
to silently ignore the directories, and
" recurse"
to read them recursively, which has the same effect as the
| |
| |
Interpret pattern as an extended regular expression (i.e., force grep to behave as egrep). | |
| |
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
| |
| |
If specified, it excludes files matching the given
filename
pattern
from the search.
Note that
| |
| |
If
| |
| |
Interpret pattern as a set of fixed strings (i.e., force grep to behave as fgrep). | |
| |
Read one or more newline separated patterns from file. Empty pattern lines match every input line. Newlines are not considered part of a pattern. If file is empty, nothing is matched. | |
| |
Interpret pattern as a basic regular expression (i.e., force grep to behave as traditional grep). | |
| |
Always print filename headers with output lines. | |
| |
Never print filename headers (i.e., filenames) with output lines. | |
| |
Print a brief help message. | |
| |
Ignore binary files.
This option is equivalent to the
" | |
| |
Perform case insensitive matching. By default, grep is case sensitive. | |
| |
If specified, only files matching the given filename
pattern
are searched.
Note that
| |
| |
If
| |
| |
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 unless a
| |
| |
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.
Pathnames are listed once per file searched.
If the standard input is searched, the string
"(standard input)"
is written unless a
| |
| |
Label to use in place of
"(standard input)"
for a file name where a file name would normally be printed.
This option applies to
| |
| |
Use mmap(2) instead of read(2) to read input, which can result in better performance under some circumstances but can cause undefined behaviour. | |
| |
Stop reading the file after num matches. | |
| |
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
| |
| |
Prints a zero-byte after the file name. | |
| |
If
| |
| |
Prints only the matching part of the lines. | |
| |
If
| |
| |
Quiet mode: suppress normal output. grep will only search a file until a match has been found, making searches potentially less expensive. | |
| |
Recursively search subdirectories listed. (i.e., force grep to behave as rgrep). | |
| |
If
| |
| |
Silent mode. Nonexistent and unreadable files are ignored (i.e., their error messages are suppressed). | |
| |
Search binary files, but do not attempt to print them. | |
| |
This option has no effect and is provided only for compatibility with GNU grep. | |
| |
Display version information and exit. | |
| |
Selected lines are those not matching any of the specified patterns. | |
| |
The expression is searched for as a word (as if surrounded by
'[[:<:]]'
and
'[[:>:]]';
see
re_format(7)).
This option has no effect if
| |
| |
Only input lines selected against an entire fixed string or regular expression are considered to be matching lines. | |
| |
Equivalent to
| |
| |
Treat input and output data as sequences of lines terminated by a zero-byte instead of a newline. | |
| |
Controls searching and printing of binary files. Options are: | |
binary (default) | Search binary files but do not print them. |
without-match | Do not search binary files. |
text | Treat all files as text. |
| 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.
Additionally,
" -"
may be used in place of a file name, anywhere that a file name is accepted, to
read from standard input.
This includes both
0 | One or more lines were selected. |
1 | No lines were selected. |
>1 | An error occurred. |
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 ‘amp;.’, 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. | |
The flags
[
All long options are provided for compatibility with GNU versions of this utility.
Historic versions of the
grep
utility also supported the flags
[
GREP (1) | April 17, 2021 |
Main index | Section 1 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | The last good thing written in C was Franz Schubert's Symphony #9. | ” |
— Erwin Dietrich |