man-pages/man1/grep.1.html
2021-03-31 01:06:50 +01:00

1835 lines
37 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GREP</TITLE>
</HEAD><BODY>
<H1>GREP</H1>
Section: User Commands (1)<BR>Updated: 2019-12-29<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
grep, egrep, fgrep, rgrep - print lines that match patterns
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>grep</B>
[<I>OPTION</I>...]
<I>PATTERNS</I>
[<I>FILE</I>...]
<BR>
<B>grep</B>
[<I>OPTION</I>...]
<B>-e</B>
<I>PATTERNS</I>
...
[<I>FILE</I>...]
<BR>
<B>grep</B>
[<I>OPTION</I>...]
<B>-f</B>
<I>PATTERN_FILE</I>
...
[<I>FILE</I>...]
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>grep</B>
searches for
<I>PATTERNS</I>
in each
<I>FILE</I>.
<I>PATTERNS</I>
is one or more patterns separated by newline characters, and
<B>grep</B>
prints each line that matches a pattern.
Typically
<I>PATTERNS</I>
should be quoted when
<B>grep</B>
is used in a shell command.
<P>
A
<I>FILE</I>
of
``<B>-</B>''
stands for standard input.
If no
<I>FILE</I>
is given, recursive searches examine the working directory,
and nonrecursive searches read standard input.
<P>
In addition, the variant programs
<B>egrep</B>,
<B>fgrep</B>
and
<B>rgrep</B>
are the same as
<B>grep&nbsp;-E</B>,
<B>grep&nbsp;-F</B>,
and
<B>grep&nbsp;-r</B>,
respectively.
These variants are deprecated, but are provided for backward compatibility.
<A NAME="lbAE">&nbsp;</A>
<H2>OPTIONS</H2>
<A NAME="lbAF">&nbsp;</A>
<H3>Generic Program Information</H3>
<DL COMPACT>
<DT id="1"><B>--help</B>
<DD>
Output a usage message and exit.
<DT id="2"><B>-V</B>, <B>--version</B>
<DD>
Output the version number of
<B>grep</B>
and exit.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H3>Pattern Syntax</H3>
<DL COMPACT>
<DT id="3"><B>-E</B>, <B>--extended-regexp</B>
<DD>
Interpret
<I>PATTERNS</I>
as extended regular expressions (EREs, see below).
<DT id="4"><B>-F</B>, <B>--fixed-strings</B>
<DD>
Interpret
<I>PATTERNS</I>
as fixed strings, not regular expressions.
<DT id="5"><B>-G</B>, <B>--basic-regexp</B>
<DD>
Interpret
<I>PATTERNS</I>
as basic regular expressions (BREs, see below).
This is the default.
<DT id="6"><B>-P</B>, <B>--perl-regexp</B>
<DD>
Interpret PATTERNS as Perl-compatible regular expressions (PCREs).
This option is experimental when combined with the
<B>-z</B>
(<B>--null-data</B>)
option, and
<B>grep -P</B>
may warn of unimplemented features.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H3>Matching Control</H3>
<DL COMPACT>
<DT id="7"><B>-e</B><I> PATTERNS</I><B></B>, --regexp=<I>PATTERNS</I>
<DD>
Use
<I>PATTERNS</I>
as the patterns.
If this option is used multiple times or is combined with the
<B>-f</B>
(<B>--file</B>)
option, search for all patterns given.
This option can be used to protect a pattern beginning with ``-''.
<DT id="8"><B>-f</B><I> FILE</I><B></B>, --file=<I>FILE</I>
<DD>
Obtain patterns from
<I>FILE</I>,
one per line.
If this option is used multiple times or is combined with the
<B>-e</B>
(<B>--regexp</B>)
option, search for all patterns given.
The empty file contains zero patterns, and therefore matches nothing.
<DT id="9"><B>-i</B>, <B>--ignore-case</B>
<DD>
Ignore case distinctions in patterns and input data,
so that characters that differ only in case
match each other.
<DT id="10"><B>--no-ignore-case</B>
<DD>
Do not ignore case distinctions in patterns and input data.
This is the default.
This option is useful for passing to shell scripts that already use
<B>-i</B>,
to cancel its effects because the two options override each other.
<DT id="11"><B>-v</B>, <B>--invert-match</B>
<DD>
Invert the sense of matching, to select non-matching lines.
<DT id="12"><B>-w</B>, <B>--word-regexp</B>
<DD>
Select only those lines containing matches that form whole words.
The test is that the matching substring must either be at the
beginning of the line, or preceded by a non-word constituent
character.
Similarly, it must be either at the end of the line
or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.
This option has no effect if
<B>-x</B>
is also specified.
<DT id="13"><B>-x</B>, <B>--line-regexp</B>
<DD>
Select only those matches that exactly match the whole line.
For a regular expression pattern, this is like parenthesizing the
pattern and then surrounding it with
<B>^</B>
and
<B>$</B>.
<DT id="14"><B>-y</B>
<DD>
Obsolete synonym for
<B>-i</B>.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H3>General Output Control</H3>
<DL COMPACT>
<DT id="15"><B>-c</B>, <B>--count</B>
<DD>
Suppress normal output; instead print a count of
matching lines for each input file.
With the
<B>-v</B>, <B>--invert-match</B>
option (see below), count non-matching lines.
<DT id="16"><B>--color</B>[<B>=</B><I>WHEN</I>], <B>--colour</B>[<B>=</B><I>WHEN</I>]
<DD>
Surround the matched (non-empty) strings, matching lines, context lines,
file names, line numbers, byte offsets, and separators (for fields and
groups of context lines) with escape sequences to display them in color
on the terminal.
The colors are defined by the environment variable
<B>GREP_COLORS</B>.
The deprecated environment variable
<B>GREP_COLOR</B>
is still supported, but its setting does not have priority.
<I>WHEN</I>
is
<B>never</B>, <B>always</B>, or <B>auto</B>.
<DT id="17"><B>-L</B>, <B>--files-without-match</B>
<DD>
Suppress normal output; instead print the name
of each input file from which no output would
normally have been printed.
The scanning will stop on the first match.
<DT id="18"><B>-l</B>, <B>--files-with-matches</B>
<DD>
Suppress normal output; instead print
the name of each input file from which output
would normally have been printed.
The scanning will stop on the first match.
<DT id="19"><B>-m</B><I> NUM</I><B></B>, --max-count=<I>NUM</I>
<DD>
Stop reading a file after
<I>NUM</I>
matching lines.
If the input is standard input from a regular file,
and
<I>NUM</I>
matching lines are output,
<B>grep</B>
ensures that the standard input is positioned to just after the last
matching line before exiting, regardless of the presence of trailing
context lines.
This enables a calling process to resume a search.
When
<B>grep</B>
stops after
<I>NUM</I>
matching lines, it outputs any trailing context lines.
When the
<B>-c</B>
or
<B>--count</B>
option is also used,
<B>grep</B>
does not output a count greater than
<I>NUM</I>.
When the
<B>-v</B>
or
<B>--invert-match</B>
option is also used,
<B>grep</B>
stops after outputting
<I>NUM</I>
non-matching lines.
<DT id="20"><B>-o</B>, <B>--only-matching</B>
<DD>
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
<DT id="21"><B>-q</B>, <B>--quiet</B>, <B>--silent</B>
<DD>
Quiet; do not write anything to standard output.
Exit immediately with zero status if any match is found,
even if an error was detected.
Also see the
<B>-s</B>
or
<B>--no-messages</B>
option.
<DT id="22"><B>-s</B>, <B>--no-messages</B>
<DD>
Suppress error messages about nonexistent or unreadable files.
</DL>
<A NAME="lbAJ">&nbsp;</A>
<H3>Output Line Prefix Control</H3>
<DL COMPACT>
<DT id="23"><B>-b</B>, <B>--byte-offset</B>
<DD>
Print the 0-based byte offset within the input file
before each line of output.
If
<B>-o</B>
(<B>--only-matching</B>)
is specified,
print the offset of the matching part itself.
<DT id="24"><B>-H</B>, <B>--with-filename</B>
<DD>
Print the file name for each match.
This is the default when there is more than one file to search.
<DT id="25"><B>-h</B>, <B>--no-filename</B>
<DD>
Suppress the prefixing of file names on output.
This is the default when there is only one file
(or only standard input) to search.
<DT id="26"><B>--label=</B><I>LABEL</I>
<DD>
Display input actually coming from standard input as input coming from file
<I>LABEL</I>.
This can be useful for commands that transform a file's contents
before searching,
e.g.,
<B>gzip -cd foo.gz | grep --label=foo -H 'some pattern'</B>.
See also the
<B>-H</B>
option.
<DT id="27"><B>-n</B>, <B>--line-number</B>
<DD>
Prefix each line of output with the 1-based line number
within its input file.
<DT id="28"><B>-T</B>, <B>--initial-tab</B>
<DD>
Make sure that the first character of actual line content lies on a
tab stop, so that the alignment of tabs looks normal.
This is useful with options that prefix their output to the actual content:
<B>-H</B>,<B>-n</B>,
and
<B>-b</B>.
In order to improve the probability that lines
from a single file will all start at the same column,
this also causes the line number and byte offset (if present)
to be printed in a minimum size field width.
<DT id="29"><B>-u</B>, <B>--unix-byte-offsets</B>
<DD>
Report Unix-style byte offsets.
This switch causes
<B>grep</B>
to report byte offsets as if the file were a Unix-style text file,
i.e., with CR characters stripped off.
This will produce results identical to running
<B>grep</B>
on a Unix machine.
This option has no effect unless
<B>-b</B>
option is also used;
it has no effect on platforms other than MS-DOS and MS-Windows.
<DT id="30"><B>-Z</B>, <B>--null</B>
<DD>
Output a zero byte (the ASCII
<B>NUL</B>
character) instead of the character that normally follows a file name.
For example,
<B>grep -lZ</B>
outputs a zero byte after each file name instead of the usual newline.
This option makes the output unambiguous, even in the presence of file
names containing unusual characters like newlines.
This option can be used with commands like
<B>find -print0</B>,
<B>perl -0</B>,
<B>sort -z</B>,
and
<B>xargs -0</B>
to process arbitrary file names,
even those that contain newline characters.
</DL>
<A NAME="lbAK">&nbsp;</A>
<H3>Context Line Control</H3>
<DL COMPACT>
<DT id="31"><B>-A</B><I> NUM</I><B></B>, --after-context=<I>NUM</I>
<DD>
Print
<I>NUM</I>
lines of trailing context after matching lines.
Places a line containing a group separator
(<B>--</B>)
between contiguous groups of matches.
With the
<B>-o</B>
or
<B>--only-matching</B>
option, this has no effect and a warning is given.
<DT id="32"><B>-B</B><I> NUM</I><B></B>, --before-context=<I>NUM</I>
<DD>
Print
<I>NUM</I>
lines of leading context before matching lines.
Places a line containing a group separator
(<B>--</B>)
between contiguous groups of matches.
With the
<B>-o</B>
or
<B>--only-matching</B>
option, this has no effect and a warning is given.
<DT id="33"><B>-C</B><I> NUM</I><B></B>, -<I>NUM</I><B></B>, --context=<I>NUM</I>
<DD>
Print
<I>NUM</I>
lines of output context.
Places a line containing a group separator
(<B>--</B>)
between contiguous groups of matches.
With the
<B>-o</B>
or
<B>--only-matching</B>
option, this has no effect and a warning is given.
</DL>
<A NAME="lbAL">&nbsp;</A>
<H3>File and Directory Selection</H3>
<DL COMPACT>
<DT id="34"><B>-a</B>, <B>--text</B>
<DD>
Process a binary file as if it were text; this is equivalent to the
<B>--binary-files=text</B>
option.
<DT id="35"><B>--binary-files=</B><I>TYPE</I>
<DD>
If a file's data or metadata
indicate that the file contains binary data,
assume that the file is of type
<I>TYPE</I>.
Non-text bytes indicate binary data; these are either output bytes that are
improperly encoded for the current locale, or null input bytes when the
<B>-z</B>
option is not given.
<DT id="36"><DD>
By default,
<I>TYPE</I>
is
<B>binary</B>,
and
<B>grep</B>
suppresses output after null input binary data is discovered,
and suppresses output lines that contain improperly encoded data.
When some output is suppressed,
<B>grep</B>
follows any output
with a one-line message saying that a binary file matches.
<DT id="37"><DD>
If
<I>TYPE</I>
is
<B>without-match</B>,
when
<B>grep</B>
discovers null input binary data it assumes that the rest of the file
does not match; this is equivalent to the
<B>-I</B>
option.
<DT id="38"><DD>
If
<I>TYPE</I>
is
<B>text</B>,
<B>grep</B>
processes a binary file as if it were text; this is equivalent to the
<B>-a</B>
option.
<DT id="39"><DD>
When
<I>type</I>
is
<B>binary</B>,
<B>grep</B>
may treat non-text bytes as line terminators even without the
<B>-z</B>
option. This means choosing
<B>binary</B>
versus
<B>text</B>
can affect whether a pattern matches a file. For
example, when
<I>type</I>
is
<B>binary</B>
the pattern
<B>q$ might</B>
match
<B>q</B>
immediately followed by a null byte, even though this
is not matched when
<I>type</I>
is
<B>text</B>.
Conversely, when
<I>type</I>
is
<B>binary</B>
the pattern
<B>.</B>
(period) might not match a null byte.
<DT id="40"><DD>
<I>Warning:</I>
The
<B>-a</B>
option might output binary garbage,
which can have nasty side effects if the output is a terminal and if the
terminal driver interprets some of it as commands.
On the other hand, when reading files whose text encodings are
unknown, it can be helpful to use
<B>-a</B>
or to set
<B>LC_ALL='C'</B>
in the environment, in order to find more matches even if the matches
are unsafe for direct display.
<DT id="41"><B>-D</B><I> ACTION</I><B></B>, --devices=<I>ACTION</I>
<DD>
If an input file is a device, FIFO or socket, use
<I>ACTION</I>
to process it.
By default,
<I>ACTION</I>
is
<B>read</B>,
which means that devices are read just as if they were ordinary files.
If
<I>ACTION</I>
is
<B>skip</B>,
devices are silently skipped.
<DT id="42"><B>-d</B><I> ACTION</I><B></B>, --directories=<I>ACTION</I>
<DD>
If an input file is a directory, use
<I>ACTION</I>
to process it.
By default,
<I>ACTION</I>
is
<B>read</B>,
i.e., read directories just as if they were ordinary files.
If
<I>ACTION</I>
is
<B>skip</B>,
silently skip directories.
If
<I>ACTION</I>
is
<B>recurse</B>,
read all files under each directory, recursively,
following symbolic links only if they are on the command line.
This is equivalent to the
<B>-r</B>
option.
<DT id="43"><B>--exclude=</B><I>GLOB</I>
<DD>
Skip any command-line file with a name suffix that matches the pattern
<I>GLOB</I>,
using wildcard matching; a name suffix is either the whole
name, or a trailing part that starts with a non-slash character
immediately after a slash
(<B>/</B>)
in the name.
When searching recursively, skip any subfile whose base name matches
<I>GLOB</I>;
the base name is the part after the last slash.
A pattern can use
<B>*</B>,
<B>?</B>,
and
<B>[</B>...<B>]</B>
as wildcards, and
<B>\</B>
to quote a wildcard or backslash character literally.
<DT id="44"><B>--exclude-from=</B><I>FILE</I>
<DD>
Skip files whose base name matches any of the file-name globs read from
<I>FILE</I>
(using wildcard matching as described under
<B>--exclude</B>).
<DT id="45"><B>--exclude-dir=</B><I>GLOB</I>
<DD>
Skip any command-line directory with a name suffix that matches the
pattern
<I>GLOB</I>.
When searching recursively, skip any subdirectory
whose base name matches
<I>GLOB</I>.
Ignore any redundant trailing slashes in
<I>GLOB</I>.
<DT id="46"><B>-I</B>
<DD>
Process a binary file as if it did not contain matching data; this is
equivalent to the
<B>--binary-files=without-match</B>
option.
<DT id="47"><B>--include=</B><I>GLOB</I>
<DD>
Search only files whose base name matches
<I>GLOB</I>
(using wildcard matching as described under
<B>--exclude</B>).
<DT id="48"><B>-r</B>, <B>--recursive</B>
<DD>
Read all files under each directory, recursively,
following symbolic links only if they are on the command line.
Note that if no file operand is given, grep searches the working directory.
This is equivalent to the
<B>-d recurse</B>
option.
<DT id="49"><B>-R</B>, <B>--dereference-recursive</B>
<DD>
Read all files under each directory, recursively.
Follow all symbolic links, unlike
<B>-r</B>.
</DL>
<A NAME="lbAM">&nbsp;</A>
<H3>Other Options</H3>
<DL COMPACT>
<DT id="50"><B>--line-buffered</B>
<DD>
Use line buffering on output.
This can cause a performance penalty.
<DT id="51"><B>-U</B>, <B>--binary</B>
<DD>
Treat the file(s) as binary.
By default, under MS-DOS and MS-Windows,
<B>grep</B>
guesses whether a file is text or binary as described for the
<B>--binary-files</B>
option.
If
<B>grep</B>
decides the file is a text file, it strips the CR characters from the
original file contents (to make regular expressions with
<B>^</B>
and
<B>$</B>
work correctly).
Specifying
<B>-U</B>
overrules this guesswork, causing all files to be read and passed to the
matching mechanism verbatim; if the file is a text file with CR/LF
pairs at the end of each line, this will cause some regular
expressions to fail.
This option has no effect on platforms
other than MS-DOS and MS-Windows.
<DT id="52"><B>-z</B>, <B>--null-data</B>
<DD>
Treat input and output data as sequences of lines, each terminated by
a zero byte (the ASCII NUL character) instead of a newline.
Like the
<B>-Z</B>
or
<B>--null</B>
option, this option can be used with commands like
<B>sort -z</B>
to process arbitrary file names.
</DL>
<A NAME="lbAN">&nbsp;</A>
<H2>REGULAR EXPRESSIONS</H2>
A regular expression is a pattern that describes a set of strings.
Regular expressions are constructed analogously to arithmetic
expressions, by using various operators to combine smaller expressions.
<P>
<B>grep</B>
understands three different versions of regular expression syntax:
``basic'' (BRE), ``extended'' (ERE) and ``perl'' (PCRE).
In GNU
<B>grep</B>
there is no difference in available functionality between basic and
extended syntaxes.
In other implementations, basic regular expressions are less powerful.
The following description applies to extended regular expressions;
differences for basic regular expressions are summarized afterwards.
Perl-compatible regular expressions give additional functionality, and are
documented in <A HREF="/cgi-bin/man/man2html?3+pcresyntax">pcresyntax</A>(3) and <A HREF="/cgi-bin/man/man2html?3+pcrepattern">pcrepattern</A>(3), but work only if
PCRE is available in the system.
<P>
The fundamental building blocks are the regular expressions
that match a single character.
Most characters, including all letters and digits,
are regular expressions that match themselves.
Any meta-character with special meaning
may be quoted by preceding it with a backslash.
<P>
The period
<B>.</B>
matches any single character.
It is unspecified whether it matches an encoding error.
<A NAME="lbAO">&nbsp;</A>
<H3>Character Classes and Bracket Expressions</H3>
A
<I>bracket expression</I>
is a list of characters enclosed by
<B>[</B>
and
<B>]</B>.
It matches any single
character in that list.
If the first character of the list
is the caret
<B>^</B>
then it matches any character
<I>not</I>
in the list; it is unspecified whether it matches an encoding error.
For example, the regular expression
<B>[0123456789]</B>
matches any single digit.
<P>
Within a bracket expression, a
<I>range expression</I>
consists of two characters separated by a hyphen.
It matches any single character that sorts between the two characters,
inclusive, using the locale's collating sequence and character set.
For example, in the default C locale,
<B>[a-d]</B>
is equivalent to
<B>[abcd]</B>.
Many locales sort characters in dictionary order, and in these locales
<B>[a-d]</B>
is typically not equivalent to
<B>[abcd]</B>;
it might be equivalent to
<B>[aBbCcDd]</B>,
for example.
To obtain the traditional interpretation of bracket expressions,
you can use the C locale by setting the
<B>LC_ALL</B>
environment variable to the value
<B>C</B>.
<P>
Finally, certain named classes of characters are predefined within
bracket expressions, as follows.
Their names are self explanatory, and they are
<B>[:alnum:]</B>,
<B>[:alpha:]</B>,
<B>[:blank:]</B>,
<B>[:cntrl:]</B>,
<B>[:digit:]</B>,
<B>[:graph:]</B>,
<B>[:lower:]</B>,
<B>[:print:]</B>,
<B>[:punct:]</B>,
<B>[:space:]</B>,
<B>[:upper:]</B>,
and
<B>[:xdigit:]</B>.
For example,
<B>[[:alnum:]]</B>
means the character class of numbers and
letters in the current locale.
In the C locale and ASCII
character set encoding, this is the same as
<B>[0-9A-Za-z]</B>.
(Note that the brackets in these class names are part of the symbolic
names, and must be included in addition to the brackets delimiting
the bracket expression.)
Most meta-characters lose their special meaning inside bracket expressions.
To include a literal
<B>]</B>
place it first in the list.
Similarly, to include a literal
<B>^</B>
place it anywhere but first.
Finally, to include a literal
<B>-</B>
place it last.
<A NAME="lbAP">&nbsp;</A>
<H3>Anchoring</H3>
The caret
<B>^</B>
and the dollar sign
<B>$</B>
are meta-characters that respectively match the empty string at the
beginning and end of a line.
<A NAME="lbAQ">&nbsp;</A>
<H3>The Backslash Character and Special Expressions</H3>
The symbols
<B>\&lt;</B>
and
<B>\&gt;</B>
respectively match the empty string at the beginning and end of a word.
The symbol
<B>\b</B>
matches the empty string at the edge of a word,
and
<B>\B</B>
matches the empty string provided it's
<I>not</I>
at the edge of a word.
The symbol
<B>\w</B>
is a synonym for
<B>[_[:alnum:]]</B>
and
<B>\W</B>
is a synonym for
<B>[^_[:alnum:]]</B>.
<A NAME="lbAR">&nbsp;</A>
<H3>Repetition</H3>
A regular expression may be followed by one of several repetition operators:
<DL COMPACT>
<DT id="53"><B>?</B>
<DD>
The preceding item is optional and matched at most once.
<DT id="54"><B>*</B>
<DD>
The preceding item will be matched zero or more times.
<DT id="55"><B>+</B>
<DD>
The preceding item will be matched one or more times.
<DT id="56"><B>{</B><I>n</I><B>}</B>
<DD>
The preceding item is matched exactly
<I>n</I>
times.
<DT id="57"><B>{</B><I>n</I><B>,}</B>
<DD>
The preceding item is matched
<I>n</I>
or more times.
<DT id="58"><B>{,</B><I>m</I><B>}</B>
<DD>
The preceding item is matched at most
<I>m</I>
times.
This is a GNU extension.
<DT id="59"><B>{</B><I>n</I><B>,</B><I>m</I><B>}</B>
<DD>
The preceding item is matched at least
<I>n</I>
times, but not more than
<I>m</I>
times.
</DL>
<A NAME="lbAS">&nbsp;</A>
<H3>Concatenation</H3>
Two regular expressions may be concatenated; the resulting
regular expression matches any string formed by concatenating
two substrings that respectively match the concatenated
expressions.
<A NAME="lbAT">&nbsp;</A>
<H3>Alternation</H3>
Two regular expressions may be joined by the infix operator
<B>|</B>;
the resulting regular expression matches any string matching
either alternate expression.
<A NAME="lbAU">&nbsp;</A>
<H3>Precedence</H3>
Repetition takes precedence over concatenation, which in turn
takes precedence over alternation.
A whole expression may be enclosed in parentheses
to override these precedence rules and form a subexpression.
<A NAME="lbAV">&nbsp;</A>
<H3>Back-references and Subexpressions</H3>
The back-reference
<B>\</B><I>n</I>, where
<I>n</I>
is a single digit, matches the substring
previously matched by the
<I>n</I>th
parenthesized subexpression of the regular expression.
<A NAME="lbAW">&nbsp;</A>
<H3>Basic vs Extended Regular Expressions</H3>
In basic regular expressions the meta-characters
<B>?</B>,
<B>+</B>,
<B>{</B>,
<B>|</B>,
<B>(</B>,
and
<B>)</B>
lose their special meaning; instead use the backslashed
versions
<B>\?</B>,
<B>\+</B>,
<B>\{</B>,
<B>\|</B>,
<B>\(</B>,
and
<B>\)</B>.
<A NAME="lbAX">&nbsp;</A>
<H2>EXIT STATUS</H2>
Normally the exit status is 0 if a line is selected, 1 if no lines
were selected, and 2 if an error occurred. However, if the
<B>-q</B>
or
<B>--quiet</B>
or
<B>--silent</B>
is used and a line is selected, the exit status is 0 even if an error
occurred.
<A NAME="lbAY">&nbsp;</A>
<H2>ENVIRONMENT</H2>
The behavior of
<B>grep</B>
is affected by the following environment variables.
<P>
The locale for category
<B>LC_</B><I>foo</I>
is specified by examining the three environment variables
<B>LC_ALL</B>,
<B>LC_</B><I>foo</I>,
<B>LANG</B>,
in that order.
The first of these variables that is set specifies the locale.
For example, if
<B>LC_ALL</B>
is not set, but
<B>LC_MESSAGES</B>
is set to
<B>pt_BR</B>,
then the Brazilian Portuguese locale is used for the
<B>LC_MESSAGES</B>
category.
The C locale is used if none of these environment variables are set,
if the locale catalog is not installed, or if
<B>grep</B>
was not compiled with national language support (NLS).
The shell command
<B>locale -a</B>
lists locales that are currently available.
<DL COMPACT>
<DT id="60"><B>GREP_OPTIONS</B>
<DD>
This variable specifies default options
to be placed in front of any explicit options.
As this causes problems when writing portable scripts,
this feature will be removed in a future release of
<B>grep</B>,
and
<B>grep</B>
warns if it is used.
Please use an alias or script instead.
<DT id="61"><B>GREP_COLOR</B>
<DD>
This variable specifies the color used to highlight matched (non-empty) text.
It is deprecated in favor of
<B>GREP_COLORS</B>,
but still supported.
The
<B>mt</B>,
<B>ms</B>,
and
<B>mc</B>
capabilities of
<B>GREP_COLORS</B>
have priority over it.
It can only specify the color used to highlight
the matching non-empty text in any matching line
(a selected line when the
<B>-v</B>
command-line option is omitted,
or a context line when
<B>-v</B>
is specified).
The default is
<B>01;31</B>,
which means a bold red foreground text on the terminal's default background.
<DT id="62"><B>GREP_COLORS</B>
<DD>
Specifies the colors and other attributes
used to highlight various parts of the output.
Its value is a colon-separated list of capabilities
that defaults to
<B>ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36</B>
with the
<B>rv</B>
and
<B>ne</B>
boolean capabilities omitted (i.e., false).
Supported capabilities are as follows.
<DL COMPACT><DT id="63"><DD>
<DL COMPACT>
<DT id="64"><B>sl=</B>
<DD>
SGR substring for whole selected lines
(i.e.,
matching lines when the
<B>-v</B>
command-line option is omitted,
or non-matching lines when
<B>-v</B>
is specified).
If however the boolean
<B>rv</B>
capability
and the
<B>-v</B>
command-line option are both specified,
it applies to context matching lines instead.
The default is empty (i.e., the terminal's default color pair).
<DT id="65"><B>cx=</B>
<DD>
SGR substring for whole context lines
(i.e.,
non-matching lines when the
<B>-v</B>
command-line option is omitted,
or matching lines when
<B>-v</B>
is specified).
If however the boolean
<B>rv</B>
capability
and the
<B>-v</B>
command-line option are both specified,
it applies to selected non-matching lines instead.
The default is empty (i.e., the terminal's default color pair).
<DT id="66"><B>rv</B>
<DD>
Boolean value that reverses (swaps) the meanings of
the
<B>sl=</B>
and
<B>cx=</B>
capabilities
when the
<B>-v</B>
command-line option is specified.
The default is false (i.e., the capability is omitted).
<DT id="67"><B>mt=01;31</B>
<DD>
SGR substring for matching non-empty text in any matching line
(i.e.,
a selected line when the
<B>-v</B>
command-line option is omitted,
or a context line when
<B>-v</B>
is specified).
Setting this is equivalent to setting both
<B>ms=</B>
and
<B>mc=</B>
at once to the same value.
The default is a bold red text foreground over the current line background.
<DT id="68"><B>ms=01;31</B>
<DD>
SGR substring for matching non-empty text in a selected line.
(This is only used when the
<B>-v</B>
command-line option is omitted.)
The effect of the
<B>sl=</B>
(or
<B>cx=</B>
if
<B>rv</B>)
capability remains active when this kicks in.
The default is a bold red text foreground over the current line background.
<DT id="69"><B>mc=01;31</B>
<DD>
SGR substring for matching non-empty text in a context line.
(This is only used when the
<B>-v</B>
command-line option is specified.)
The effect of the
<B>cx=</B>
(or
<B>sl=</B>
if
<B>rv</B>)
capability remains active when this kicks in.
The default is a bold red text foreground over the current line background.
<DT id="70"><B>fn=35</B>
<DD>
SGR substring for file names prefixing any content line.
The default is a magenta text foreground over the terminal's default background.
<DT id="71"><B>ln=32</B>
<DD>
SGR substring for line numbers prefixing any content line.
The default is a green text foreground over the terminal's default background.
<DT id="72"><B>bn=32</B>
<DD>
SGR substring for byte offsets prefixing any content line.
The default is a green text foreground over the terminal's default background.
<DT id="73"><B>se=36</B>
<DD>
SGR substring for separators that are inserted
between selected line fields
(<B>:</B>),
between context line fields,
(<B>-</B>),
and between groups of adjacent lines when nonzero context is specified
(<B>--</B>).
The default is a cyan text foreground over the terminal's default background.
<DT id="74"><B>ne</B>
<DD>
Boolean value that prevents clearing to the end of line
using Erase in Line (EL) to Right
(<B>\33[K</B>)
each time a colorized item ends.
This is needed on terminals on which EL is not supported.
It is otherwise useful on terminals
for which the
<B>back_color_erase</B>
(<B>bce</B>)
boolean terminfo capability does not apply,
when the chosen highlight colors do not affect the background,
or when EL is too slow or causes too much flicker.
The default is false (i.e., the capability is omitted).
</DL>
<P>
Note that boolean capabilities have no
<B>=</B>...
part.
They are omitted (i.e., false) by default and become true when specified.
<P>
See the Select Graphic Rendition (SGR) section
in the documentation of the text terminal that is used
for permitted values and their meaning as character attributes.
These substring values are integers in decimal representation
and can be concatenated with semicolons.
<B>grep</B>
takes care of assembling the result
into a complete SGR sequence
(<B>\33[</B>...<B>m</B>).
Common values to concatenate include
<B>1</B>
for bold,
<B>4</B>
for underline,
<B>5</B>
for blink,
<B>7</B>
for inverse,
<B>39</B>
for default foreground color,
<B>30</B>
to
<B>37</B>
for foreground colors,
<B>90</B>
to
<B>97</B>
for 16-color mode foreground colors,
<B>38;5;0</B>
to
<B>38;5;255</B>
for 88-color and 256-color modes foreground colors,
<B>49</B>
for default background color,
<B>40</B>
to
<B>47</B>
for background colors,
<B>100</B>
to
<B>107</B>
for 16-color mode background colors, and
<B>48;5;0</B>
to
<B>48;5;255</B>
for 88-color and 256-color modes background colors.
</DL>
<DT id="75"><B>LC_ALL</B>, <B>LC_COLLATE</B>, <B>LANG</B><DD>
These variables specify the locale for the
<B>LC_COLLATE</B>
category,
which determines the collating sequence
used to interpret range expressions like
<B>[a-z]</B>.
<DT id="76"><B>LC_ALL</B>, <B>LC_CTYPE</B>, <B>LANG</B><DD>
These variables specify the locale for the
<B>LC_CTYPE</B>
category,
which determines the type of characters,
e.g., which characters are whitespace.
This category also determines the character encoding, that is, whether
text is encoded in UTF-8, ASCII, or some other encoding. In the C or
POSIX locale, all characters are encoded as a single byte and every
byte is a valid character.
<DT id="77"><B>LC_ALL</B>, <B>LC_MESSAGES</B>, <B>LANG</B><DD>
These variables specify the locale for the
<B>LC_MESSAGES</B>
category,
which determines the language that
<B>grep</B>
uses for messages.
The default C locale uses American English messages.
<DT id="78"><B>POSIXLY_CORRECT</B>
<DD>
If set,
<B>grep</B>
behaves as POSIX requires; otherwise,
<B>grep</B>
behaves more like other GNU programs.
POSIX requires that options that follow file names must be
treated as file names; by default, such options are permuted to the
front of the operand list and are treated as options.
Also, POSIX requires that unrecognized options be diagnosed as
``illegal'', but since they are not really against the law the default
is to diagnose them as ``invalid''.
<B>POSIXLY_CORRECT</B>
also disables <B>_</B><I>N</I><B>_GNU_nonoption_argv_flags_</B>,
described below.
<DT id="79"><B>_</B><I>N</I><B>_GNU_nonoption_argv_flags_</B><DD>
(Here
<I>N</I>
is
<B>grep</B>'s
numeric process ID.) If the
<I>i</I>th
character of this environment variable's value is
<B>1</B>,
do not consider the
<I>i</I>th
operand of
<B>grep</B>
to be an option, even if it appears to be one.
A shell can put this variable in the environment for each command it runs,
specifying which operands are the results of file name wildcard
expansion and therefore should not be treated as options.
This behavior is available only with the GNU C library, and only
when
<B>POSIXLY_CORRECT</B>
is not set.
</DL>
<A NAME="lbAZ">&nbsp;</A>
<H2>NOTES</H2>
This man page is maintained only fitfully;
the full documentation is often more up-to-date.
<A NAME="lbBA">&nbsp;</A>
<H2>COPYRIGHT</H2>
Copyright 1998-2000, 2002, 2005-2020 Free Software Foundation, Inc.
<P>
This is free software;
see the source for copying conditions.
There is NO warranty;
not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
<A NAME="lbBB">&nbsp;</A>
<H2>BUGS</H2>
<A NAME="lbBC">&nbsp;</A>
<H3>Reporting Bugs</H3>
Email bug reports to
the bug-reporting address
An
email archive
and a
bug tracker
are available.
<A NAME="lbBD">&nbsp;</A>
<H3>Known Bugs</H3>
Large repetition counts in the
<B>{</B><I>n</I><B>,</B><I>m</I><B>}</B>
construct may cause
<B>grep</B>
to use lots of memory.
In addition,
certain other obscure regular expressions require exponential time
and space, and may cause
<B>grep</B>
to run out of memory.
<P>
Back-references are very slow, and may require exponential time.
<A NAME="lbBE">&nbsp;</A>
<H2>EXAMPLE</H2>
The following example outputs the location and contents of any line
containing ``f'' and ending in ``.c'',
within all files in the current directory whose names
contain ``g'' and end in ``.h''.
The
<B>-n</B>
option outputs line numbers, the
<B>--</B>
argument treats expansions of ``*g*.h'' starting with ``-''
as file names not options,
and the empty file /dev/null causes file names to be output
even if only one file name happens to be of the form ``*g*.h''.
<P>
$ <B>grep</B> -n -- 'f.*\.c$' *g*.h /dev/null
argmatch.h:1:/* definitions and prototypes for argmatch.c
<P>
The only line that matches is line 1 of argmatch.h.
Note that the regular expression syntax used in the pattern differs
from the globbing syntax that the shell uses to match file names.
<A NAME="lbBF">&nbsp;</A>
<H2>SEE ALSO</H2>
<A NAME="lbBG">&nbsp;</A>
<H3>Regular Manual Pages</H3>
<B><A HREF="/cgi-bin/man/man2html?1+awk">awk</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+cmp">cmp</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+diff">diff</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+find">find</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+perl">perl</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+sed">sed</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+sort">sort</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+xargs">xargs</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+pcre">pcre</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pcresyntax">pcresyntax</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pcrepattern">pcrepattern</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+terminfo">terminfo</A></B>(5),
<B><A HREF="/cgi-bin/man/man2html?7+glob">glob</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+regex">regex</A></B>(7).
<A NAME="lbBH">&nbsp;</A>
<H3>Full Documentation</H3>
A
complete manual
is available.
If the
<B>info</B>
and
<B>grep</B>
programs are properly installed at your site, the command
<DL COMPACT>
<DT id="80"><DD>
<B>info grep</B>
</DL>
<P>
should give you access to the complete manual.
<BR>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="81"><A HREF="#lbAB">NAME</A><DD>
<DT id="82"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="83"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="84"><A HREF="#lbAE">OPTIONS</A><DD>
<DL>
<DT id="85"><A HREF="#lbAF">Generic Program Information</A><DD>
<DT id="86"><A HREF="#lbAG">Pattern Syntax</A><DD>
<DT id="87"><A HREF="#lbAH">Matching Control</A><DD>
<DT id="88"><A HREF="#lbAI">General Output Control</A><DD>
<DT id="89"><A HREF="#lbAJ">Output Line Prefix Control</A><DD>
<DT id="90"><A HREF="#lbAK">Context Line Control</A><DD>
<DT id="91"><A HREF="#lbAL">File and Directory Selection</A><DD>
<DT id="92"><A HREF="#lbAM">Other Options</A><DD>
</DL>
<DT id="93"><A HREF="#lbAN">REGULAR EXPRESSIONS</A><DD>
<DL>
<DT id="94"><A HREF="#lbAO">Character Classes and Bracket Expressions</A><DD>
<DT id="95"><A HREF="#lbAP">Anchoring</A><DD>
<DT id="96"><A HREF="#lbAQ">The Backslash Character and Special Expressions</A><DD>
<DT id="97"><A HREF="#lbAR">Repetition</A><DD>
<DT id="98"><A HREF="#lbAS">Concatenation</A><DD>
<DT id="99"><A HREF="#lbAT">Alternation</A><DD>
<DT id="100"><A HREF="#lbAU">Precedence</A><DD>
<DT id="101"><A HREF="#lbAV">Back-references and Subexpressions</A><DD>
<DT id="102"><A HREF="#lbAW">Basic vs Extended Regular Expressions</A><DD>
</DL>
<DT id="103"><A HREF="#lbAX">EXIT STATUS</A><DD>
<DT id="104"><A HREF="#lbAY">ENVIRONMENT</A><DD>
<DT id="105"><A HREF="#lbAZ">NOTES</A><DD>
<DT id="106"><A HREF="#lbBA">COPYRIGHT</A><DD>
<DT id="107"><A HREF="#lbBB">BUGS</A><DD>
<DL>
<DT id="108"><A HREF="#lbBC">Reporting Bugs</A><DD>
<DT id="109"><A HREF="#lbBD">Known Bugs</A><DD>
</DL>
<DT id="110"><A HREF="#lbBE">EXAMPLE</A><DD>
<DT id="111"><A HREF="#lbBF">SEE ALSO</A><DD>
<DL>
<DT id="112"><A HREF="#lbBG">Regular Manual Pages</A><DD>
<DT id="113"><A HREF="#lbBH">Full Documentation</A><DD>
</DL>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:15 GMT, March 31, 2021
</BODY>
</HTML>