GREP(1) | General Commands Manual | GREP(1) |
grep
, egrep
,
fgrep
, zgrep
,
zegrep
, zfgrep
—
file pattern searcher
grep |
[-abcEFGHhIiLlnoqRsUVvwxZ ]
[-A num]
[-B num]
[-C [num]]
[-e pattern]
[-f file]
[--binary-files =value]
[--context [=num]]
[--line-buffered ] [pattern]
[file ...] |
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. If no file arguments are
specified, the standard input is used.
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) compression
utilities.
The following options are available:
-A
num-B
and
-C
options.-a
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.-B
num-A
and
-C
options.-b
-C
[num]-A
2
-B
2. Note: no whitespace
may be given between the option and its argument.-c
-E
grep
to behave as
egrep
).-e
pattern-e
options are used to
specify multiple patterns, or when a pattern begins with a dash
(‘-’).-F
grep
to behave as
fgrep
).-f
file-G
grep
to behave as traditional
grep
).-H
-h
-I
-i
grep
is case sensitive.-L
-l
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.-n
-c
,
-L
, -l
, or
-q
is specified.-o
-q
grep
will only
search a file until a match has been found, making searches potentially
less expensive.-R
-s
-U
-V
-v
-w
-x
-Z
grep
to behave as
zgrep
.--binary-files
=value--context
[=num]--line-buffered
The grep
utility exits with one of the
following values:
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.
The grep
utility is compliant with the
IEEE Std 1003.1-2008 (“POSIX.1”)
specification.
The flags [-AaBbCGHhILoRUVwZ
] are
extensions to that specification, and the behaviour of the
-f
flag when used with an empty pattern file is left
undefined.
All long options are provided for compatibility with GNU versions of this utility.
Historic versions of the grep
utility also
supported the flags [-ruy
]. This implementation
supports those options; however, their use is strongly discouraged.
The grep
command first appeared in
Version 4 AT&T UNIX.
January 13, 2015 | OpenBSD-5.9 |