GREP(1) | User Commands | GREP(1) |
grep
, egrep
,
fgrep
— search a file for a
pattern
grep |
[-E |-F ]
[-bchHilLnorRsqvwx ] [-A
num] [-B
num] [-C
num|- num]
[--label =name]
[-e pattern_list]...
[-f pattern_file]...
[pattern_list] [file]... |
The grep
utility searches text files for a
pattern and prints all lines that contain that pattern. If no files are
specified, grep
assumes standard input. Normally,
each line found is copied to standard output. The file name is printed
before each line found if there is more than one input file.
grep
handles patterns as basic regular
expressions (BREs); egrep
(same as
grep
-E
) handles patterns as
extended regular expressions (EREs); fgrep
(same as
grep
-F
) handles patterns as
fixed strings.
The following options are supported:
-A
num--
’ delimiter
line.-b
-B
num--
’ delimiter
line.-c
-l
and -L
.-C
num,
-
num--
’ delimiter
line.-e
pattern_list-E
or -F
option is also
specified, each pattern is treated as a BRE, as described in
regex(7).-E
-f
pattern_file-E
or -F
option is also specified, each pattern is
treated as a BRE, as described in
regex(7).-F
-h
-H
-i
--label
=name-H
), instead of printing the string
‘(standard input)
’ the string
name is printed instead. See
Example 5.-l
-l
and
-L
are specified, only the last specified takes
effect. Overrides -H
.-L
-l
flag. Prints only the names
of files without matching lines. If both -l
and
-L
are specified, only the last specified takes
effect. Overrides -H
.-n
-o
The -o
option is overridden when any
of the -l
, -L
, or
-c
options are specified. When the
-o
option is specified, all context options are
ignored. The -o
and -v
options are not supported together at this time.
-q
-c
, -l
, and
-L
.-r
-R
-s
-v
-w
\<
’ and
‘\>
’.-x
The following operands are supported:
-e
pattern_list. Should not
be specified if either -e
or
-f
is specified.Be careful using the characters
‘$
’,
‘*
’,
‘[
’,
‘^
’,
‘|
’,
‘(
’,
‘)
’, and
‘\
’ in the
pattern_list because they are also meaningful to the
shell. It is safest to enclose the entire pattern_list
in single quotes: '...'
.
The -e
pattern
option has the same effect as the pattern operand, but
is useful when pattern begins with the hyphen
delimiter. It is also useful when it is more convenient to provide multiple
patterns as separate arguments.
Multiple -e
and -f
options are accepted and grep
uses all of the
patterns it is given while matching input text lines. Notice that the order
of evaluation is not specified. If an implementation finds a null string as
a pattern, it is allowed to use that pattern first, matching every line, and
effectively ignore any other patterns.
The -q
option provides a means of easily
determining whether or not a pattern (or string) exists in a group of files.
When searching several files, it provides a performance improvement (because
it can quit as soon as it finds the first match) and requires less care by
the user in choosing the set of files to supply as arguments (because it
exits zero if it finds a match even if grep
detected
an access or read error on earlier file operands).
See largefile(7) for the
description of the behavior of grep
when
encountering files greater than or equal to 2 Gbyte (2^31 bytes).
The following exit values are returned:
Posix
’
(in any case) in the file text.mm, and write with
line numbers:
$ grep -i -n posix text.mm
$ grep ^$
or
$ grep -v .
abc
’ or
‘def
’ or both:
$ grep 'abc def' $ grep -e 'abc def' $ grep -e 'abc' -e 'def' $ grep -E 'abc|def' $ grep -E -e 'abc|def' $ grep -E -e 'abc' -e 'def' $ grep -E 'abc def' $ grep -E -e 'abc def' $ grep -F -e 'abc' -e 'def' $ grep -F 'abc def' $ grep -F -e 'abc def'
abc
’ or
‘def
’:
$ grep -E '^abc$ ^def$' $ grep -F -x 'abc def'
--label
grep
, as part of a
pipeline, occasionally it can be useful override the file name
‘(standard input)
’ with something
from the pipeline. This would output each matching line instead with the
name of the input file.
$ for f in *.gz; do > gzcat $f | grep -H --label=$f foo > done
See environ(7) for
descriptions of the following environment variables that affect the
execution of grep
: LANG
,
LC_ALL
, LC_COLLATE
,
LC_CTYPE
, LC_MESSAGES
, and
NLSPATH
.
sed(1), sh(1), attributes(7), environ(7), largefile(7), regex(7), standards(7)
The grep
utility is compliant with the
IEEE Std 1003.1-2008 (“POSIX.1”)
specification with the exception of -s
option being
the same as -q
in current implementation for
historic reasons. The flags [-AbBChHrRw
] are
extensions to that specification.
The results are unspecified if input files contain lines longer
than LINE_MAX
bytes or contain binary data.
LINE_MAX
is defined in
<limits.h>
.
Portable applications should use grep
-E
and grep
-F
instead of egrep
and
fgrep
, respectively.
The grep
command first appeared in
Version 6 AT&T UNIX.
In the past /usr/bin/grep, /usr/bin/egrep, and /usr/bin/fgrep were separate implementations, and were not standard conforming, with standard conforming ones installed as /usr/xpg4/bin/grep, /usr/xpg4/bin/egrep, and /usr/xpg4/bin/fgrep, respectively. Now all non-conforming implementations are removed, and the ones previously found in /usr/xpg4/bin are installed in /usr/bin.
August 13, 2020 | OmniOS |