GREP(1) | General Commands Manual | GREP(1) |
grep
, egrep
,
fgrep
, rgrep
,
bzgrep
, bzegrep
,
bzfgrep
, zgrep
,
zegrep
, zfgrep
—
file pattern searcher
grep |
[-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz ]
[-A num]
[-B num]
[-C num]
[-e pattern]
[-f file]
[--binary-files= value]
[--color [= when]]
[--colour [= when]]
[--context= num]
[--label ]
[--line-buffered ] [--null ]
[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.
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. bzgrep
, bzegrep
,
and bzfgrep
act like grep
,
egrep
, and fgrep
,
respectively, but accept input files compressed with the
bzip2(1) compression utility.
The following options are available:
-A
num,
--after-context=
num-B
and
-C
options.-a
,
--text
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,
--before-context=
num-A
and
-C
options.-b
,
--byte-offset
-C
num,
--context=
num-A
and
-B
options.-c
,
--count
--colour=
[when],
--color=
[when]GREP_COLOR
environment variable. The possible
values of when are
“never
”,
“always
” and
“auto
”.-D
action,
--devices=
actionread
”, which means, that they are
read as if they were normal files. If the action is
set to “skip
”, devices are silently
skipped.-d
action,
--directories=
actionread
” 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 -R
and
-r
option.-E
,
--extended-regexp
grep
to behave as
egrep
).-e
pattern,
--regexp=
pattern-e
options are used to specify multiple patterns,
or when a pattern begins with a dash
(‘-’).--exclude
pattern--exclude
and --include
patterns are processed in the order given. If a name matches multiple
patterns, the latest matching rule wins. If no
--include
pattern is specified, all files are
searched that are not excluded. Patterns are matched to the full path
specified, not only to the filename component.--exclude-dir
pattern-R
is specified, it excludes directories
matching the given filename pattern from the search.
Note that --exclude-dir
and
--include-dir
patterns are processed in the order
given. If a name matches multiple patterns, the latest matching rule wins.
If no --include-dir
pattern is specified, all
directories are searched that are not excluded.-F
,
--fixed-strings
grep
to behave as
fgrep
).-f
file,
--file=
file-G
,
--basic-regexp
grep
to behave as traditional
grep
).-H
-h
,
--no-filename
--help
-I
--binary-files=
without-match
”
option.-i
,
--ignore-case
grep
is case sensitive.--include
pattern--include
and --exclude
patterns are processed in the order given. If a name matches multiple
patterns, the latest matching rule wins. Patterns are matched to the full
path specified, not only to the filename component.--include-dir
pattern-R
is specified, only directories matching the
given filename pattern are searched. Note that
--include-dir
and
--exclude-dir
patterns are processed in the order
given. If a name matches multiple patterns, the latest matching rule
wins.-J
,
--bz2decompress
-L
,
--files-without-match
--label
is specified.-l
,
--files-with-matches
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
is specified.--label
-H
, -L
, and
-l
.--mmap
-M
,
--lzma
-m
num,
--max-count=
num-n
,
--line-number
-c
,
-L
, -l
, or
-q
is specified.--null
-O
-R
is specified, follow symbolic links only if
they were explicitly listed on the command line. The default is not to
follow symbolic links.-o
,
--only-matching
-p
-R
is specified, no symbolic links are
followed. This is the default.-q
,
--quiet
, --silent
grep
will only
search a file until a match has been found, making searches potentially
less expensive.-R
,
-r
, --recursive
grep
to behave as
rgrep
).-S
-R
is specified, all symbolic links are
followed. The default is not to follow symbolic links.-s
,
--no-messages
-U
,
--binary
-u
-V
,
--version
-v
,
--invert-match
-w
,
--word-regexp
-x
is also specified.-x
,
--line-regexp
-y
-i
. Obsoleted.-z
,
--null-data
-X
,
--xz
-Z
,
--decompress
grep
to behave as
zgrep
.--binary-files=
valuebinary
(default)without-match
text
--line-buffered
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 -f
and file arguments.
GREP_OPTIONS
The grep
utility exits with one of the
following values:
$ grep 'patricia'
myfile
$ grep -w 'patricia'
myfile
$ grep -c FOO myfile
$ grep -c -i FOO
myfile
.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.
$ grep -v -e 'foo' -e 'bar'
myfile
$ egrep '19|20|25'
calendar
$ grep -H -R FIXME
--include="*.h" /usr/src/sys/arm/
$ grep -l -R FIXME
--include="*.h" /usr/src/sys/arm/
$ grep -b --colour -n foo
myfile
$ echo -e
'Free\nBSD\nAll.*reserved' | grep -E -f - myfile
$ pciconf -lv | grep -B3 -A1 -E
'class.*=.*storage'
$ grep -q foo myfile &&
echo File matches
bzip2(1), compress(1), ed(1), ex(1), gzip(1), sed(1), xz(1), zgrep(1), re_format(7)
The grep
utility is compliant with the
IEEE Std 1003.1-2008 (“POSIX.1”)
specification.
The flags [-AaBbCDdGHhILmopRSUVw
] 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 6 AT&T UNIX.
The grep
utility does not normalize
Unicode input, so a pattern containing composed characters will not match
decomposed input, and vice versa.
March 22, 2021 | Mac OS X 12 |