GREP(1) User Commands GREP(1)

NAME


grep, egrep, fgrep - search a file for a pattern

SYNOPSIS


grep [-E|-F] [-bchHilLnorRsqvwx] [-A num] [-B num] [-C num|-num]
[--label=name] [-e pattern_list]... [-f pattern_file]...
[pattern_list] [file]...

DESCRIPTION


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.

OPTIONS


The following options are supported:

-A num Prints num input lines of context after each matching line. If
there are multiple matching lines, their context lines are
separated by a `--' delimiter line.

-b Precedes each line by the block number on which it was found.
This can be useful in locating block numbers by context (first
block is 0).

-B num Prints num input lines of context before each matching line.
If there are multiple matching lines, their context lines are
separated by a `--' delimiter line.

-c Prints only a count of the lines that contain the pattern.
Overrides -l and -L.

-C num, -num
Prints num input lines of context before and number input lines
of context after each matching line. If there are multiple
matching lines, their context lines are separated by a `--'
delimiter line.

-e pattern_list
Specifies one or more patterns to be used during the search for
input. Patterns in pattern_list must be separated by a NEWLINE
character. A null pattern can be specified by two adjacent
newline characters in pattern_list. Unless the -E or -F option
is also specified, each pattern is treated as a BRE, as
described in regex(7).

-E Matches using extended regular expressions. Treats each
pattern specified as an ERE, as described in regex(7). If any
entire ERE pattern matches an input line, the line is matched.
A null ERE matches every line.

-f pattern_file
Reads one or more patterns from the file named by the path name
pattern_file. Patterns in pattern_file are terminated by a
NEWLINE character. A null pattern can be specified by an empty
line in pattern_file. Unless the -E or -F option is also
specified, each pattern is treated as a BRE, as described in
regex(7).

-F Matches using fixed strings. Treats each pattern specified as
a string instead of a regular expression. If an input line
contains any of the patterns as a contiguous sequence of bytes,
the line is matched. A null string matches every line.

-h Prevents the name of the file containing the matching line from
being prepended to that line. Used when searching multiple
files.

-H Precedes each line by the name of the file containing the
matching line.

-i Ignores upper/lower case distinction during comparisons.

--label=name
When the name of the matching file is printed (-H), instead of
printing the string `(standard input)' the string name is
printed instead. See Example 5.

-l Prints only the names of files with matching lines, separated
by NEWLINE characters. Does not repeat the names of files when
the pattern is found more than once. If both -l and -L are
specified, only the last specified takes effect. Overrides -H.

-L The opposite of the -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 Precedes each line by its line number in the file (first line
is 1).

-o Prints only the matching part of a line. If a pattern appears
more than once in a line, it will be matched and printed
multiple times.

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 Quiet. Does not write anything to the standard output,
regardless of matching lines. Exits with zero status if an
input line is selected. Overrides -c, -l, and -L.

-r Read all files under each directory, recursively. Follow
symbolic links on the command line, but skip symlinks that are
encountered recursively. If file is a device, FIFO, or socket,
skip it.

-R Read all files under each directory, recursively, following all
symbolic links.

-s Suppresses error messages about nonexistent or unreadable
files.

-v Prints all lines except those that contain the pattern.

-w Searches for the expression as a word as if surrounded by `\<'
and `\>'.

-x Considers only input lines that use all characters in the line
to match an entire fixed string or regular expression to be
matching lines.

OPERANDS


The following operands are supported:

file A path name of a file to be searched for the patterns. If no
file operands are specified, the standard input is used.

pattern_list
Specifies one or more patterns to be used during the search for
input. This operand is treated as if it were specified as -e
pattern_list. Should not be specified if either -e or -f is
specified.

USAGE


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).

Large File Behavior


See largefile(7) for the description of the behavior of grep when
encountering files greater than or equal to 2 Gbyte (2^31 bytes).

EXIT STATUS


The following exit values are returned:

0 One or more matches were found.

1 No matches were found.

2 Syntax errors or inaccessible files (even if matches were
found).

EXAMPLES


Example 1 Finding All Uses of a Word
To find all uses of the word `Posix' (in any case) in the file
text.mm, and write with line numbers:

$ grep -i -n posix text.mm

Example 2 Finding All Empty Lines
To find all empty lines in the standard input:

$ grep ^$

or

$ grep -v .

Example 3 Finding Lines Containing Strings
All of the following commands print all lines containing
strings `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'

Example 4 Finding Lines with Matching Strings
Both of the following commands print all lines matching exactly
`abc' or `def':

$ grep -E '^abc$
^def$'
$ grep -F -x 'abc
def'

Example 5 Using --label
When piping standard input into 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

ENVIRONMENT VARIABLES


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.

CODE SET INDEPENDENCE


Enabled

INTERFACE STABILITY


Committed

SEE ALSO


sed(1), sh(1), attributes(7), environ(7), largefile(7), regex(7),
standards(7)

STANDARDS


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.

NOTES


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.

HISTORY


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.

illumos August 13, 2020 illumos

tribblix@gmail.com :: GitHub :: Privacy