|
Packit |
709fb3 |
\input texinfo @c -*-texinfo-*-
|
|
Packit |
709fb3 |
@c %**start of header
|
|
Packit |
709fb3 |
@setfilename grep.info
|
|
Packit |
709fb3 |
@include version.texi
|
|
Packit |
709fb3 |
@settitle GNU Grep @value{VERSION}
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@c Combine indices.
|
|
Packit |
709fb3 |
@syncodeindex ky cp
|
|
Packit |
709fb3 |
@syncodeindex pg cp
|
|
Packit |
709fb3 |
@syncodeindex tp cp
|
|
Packit |
709fb3 |
@defcodeindex op
|
|
Packit |
709fb3 |
@syncodeindex op cp
|
|
Packit |
709fb3 |
@syncodeindex vr cp
|
|
Packit |
709fb3 |
@c %**end of header
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@documentencoding UTF-8
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@copying
|
|
Packit |
709fb3 |
This manual is for @command{grep}, a pattern matching engine.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Copyright @copyright{} 1999-2002, 2005, 2008-2017 Free Software Foundation,
|
|
Packit |
709fb3 |
Inc.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@quotation
|
|
Packit |
709fb3 |
Permission is granted to copy, distribute and/or modify this document
|
|
Packit |
709fb3 |
under the terms of the GNU Free Documentation License, Version 1.3 or
|
|
Packit |
709fb3 |
any later version published by the Free Software Foundation; with no
|
|
Packit |
709fb3 |
Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
|
|
Packit |
709fb3 |
Texts. A copy of the license is included in the section entitled
|
|
Packit |
709fb3 |
``GNU Free Documentation License''.
|
|
Packit |
709fb3 |
@end quotation
|
|
Packit |
709fb3 |
@end copying
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@dircategory Text creation and manipulation
|
|
Packit |
709fb3 |
@direntry
|
|
Packit |
709fb3 |
* grep: (grep). Print lines matching a pattern.
|
|
Packit |
709fb3 |
@end direntry
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@titlepage
|
|
Packit |
709fb3 |
@title GNU Grep: Print lines matching a pattern
|
|
Packit |
709fb3 |
@subtitle version @value{VERSION}, @value{UPDATED}
|
|
Packit |
709fb3 |
@author Alain Magloire et al.
|
|
Packit |
709fb3 |
@page
|
|
Packit |
709fb3 |
@vskip 0pt plus 1filll
|
|
Packit |
709fb3 |
@insertcopying
|
|
Packit |
709fb3 |
@end titlepage
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@contents
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@ifnottex
|
|
Packit |
709fb3 |
@node Top
|
|
Packit |
709fb3 |
@top grep
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@command{grep} prints lines that contain a match for a pattern.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This manual is for version @value{VERSION} of GNU Grep.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@insertcopying
|
|
Packit |
709fb3 |
@end ifnottex
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@menu
|
|
Packit |
709fb3 |
* Introduction:: Introduction.
|
|
Packit |
709fb3 |
* Invoking:: Command-line options, environment, exit status.
|
|
Packit |
709fb3 |
* Regular Expressions:: Regular Expressions.
|
|
Packit |
709fb3 |
* Usage:: Examples.
|
|
Packit |
709fb3 |
* Performance:: Performance tuning.
|
|
Packit |
709fb3 |
* Reporting Bugs:: Reporting Bugs.
|
|
Packit |
709fb3 |
* Copying:: License terms for this manual.
|
|
Packit |
709fb3 |
* Index:: Combined index.
|
|
Packit |
709fb3 |
@end menu
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Introduction
|
|
Packit |
709fb3 |
@chapter Introduction
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex searching for a pattern
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@command{grep} searches input files
|
|
Packit |
709fb3 |
for lines containing a match to a given pattern list.
|
|
Packit |
709fb3 |
When it finds a match in a line,
|
|
Packit |
709fb3 |
it copies the line to standard output (by default),
|
|
Packit |
709fb3 |
or produces whatever other sort of output you have requested with options.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Though @command{grep} expects to do the matching on text,
|
|
Packit |
709fb3 |
it has no limits on input line length other than available memory,
|
|
Packit |
709fb3 |
and it can match arbitrary characters within a line.
|
|
Packit |
709fb3 |
If the final byte of an input file is not a newline,
|
|
Packit |
709fb3 |
@command{grep} silently supplies one.
|
|
Packit |
709fb3 |
Since newline is also a separator for the list of patterns,
|
|
Packit |
709fb3 |
there is no way to match newline characters in a text.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Invoking
|
|
Packit |
709fb3 |
@chapter Invoking @command{grep}
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The general synopsis of the @command{grep} command line is
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep @var{options} @var{pattern} @var{input_file_names}
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
There can be zero or more @var{options}.
|
|
Packit |
709fb3 |
@var{pattern} will only be seen as such
|
|
Packit |
709fb3 |
(and not as an @var{input_file_name})
|
|
Packit |
709fb3 |
if it wasn't already specified within @var{options}
|
|
Packit |
709fb3 |
(by using the @samp{-e@ @var{pattern}}
|
|
Packit |
709fb3 |
or @samp{-f@ @var{file}} options).
|
|
Packit |
709fb3 |
There can be zero or more @var{input_file_names}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@menu
|
|
Packit |
709fb3 |
* Command-line Options:: Short and long names, grouped by category.
|
|
Packit |
709fb3 |
* Environment Variables:: POSIX, GNU generic, and GNU grep specific.
|
|
Packit |
709fb3 |
* Exit Status:: Exit status returned by @command{grep}.
|
|
Packit |
709fb3 |
* grep Programs:: @command{grep} programs.
|
|
Packit |
709fb3 |
@end menu
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Command-line Options
|
|
Packit |
709fb3 |
@section Command-line Options
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@command{grep} comes with a rich set of options:
|
|
Packit |
709fb3 |
some from POSIX and some being GNU extensions.
|
|
Packit |
709fb3 |
Long option names are always a GNU extension,
|
|
Packit |
709fb3 |
even for options that are from POSIX specifications.
|
|
Packit |
709fb3 |
Options that are specified by POSIX,
|
|
Packit |
709fb3 |
under their short names,
|
|
Packit |
709fb3 |
are explicitly marked as such
|
|
Packit |
709fb3 |
to facilitate POSIX-portable programming.
|
|
Packit |
709fb3 |
A few option names are provided
|
|
Packit |
709fb3 |
for compatibility with older or more exotic implementations.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@menu
|
|
Packit |
709fb3 |
* Generic Program Information::
|
|
Packit |
709fb3 |
* Matching Control::
|
|
Packit |
709fb3 |
* General Output Control::
|
|
Packit |
709fb3 |
* Output Line Prefix Control::
|
|
Packit |
709fb3 |
* Context Line Control::
|
|
Packit |
709fb3 |
* File and Directory Selection::
|
|
Packit |
709fb3 |
* Other Options::
|
|
Packit |
709fb3 |
@end menu
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Several additional options control
|
|
Packit |
709fb3 |
which variant of the @command{grep} matching engine is used.
|
|
Packit |
709fb3 |
@xref{grep Programs}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Generic Program Information
|
|
Packit |
709fb3 |
@subsection Generic Program Information
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --help
|
|
Packit |
709fb3 |
@opindex --help
|
|
Packit |
709fb3 |
@cindex usage summary, printing
|
|
Packit |
709fb3 |
Print a usage message briefly summarizing the command-line options
|
|
Packit |
709fb3 |
and the bug-reporting address, then exit.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -V
|
|
Packit |
709fb3 |
@itemx --version
|
|
Packit |
709fb3 |
@opindex -V
|
|
Packit |
709fb3 |
@opindex --version
|
|
Packit |
709fb3 |
@cindex version, printing
|
|
Packit |
709fb3 |
Print the version number of @command{grep} to the standard output stream.
|
|
Packit |
709fb3 |
This version number should be included in all bug reports.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Matching Control
|
|
Packit |
709fb3 |
@subsection Matching Control
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -e @var{pattern}
|
|
Packit |
709fb3 |
@itemx --regexp=@var{pattern}
|
|
Packit |
709fb3 |
@opindex -e
|
|
Packit |
709fb3 |
@opindex --regexp=@var{pattern}
|
|
Packit |
709fb3 |
@cindex pattern list
|
|
Packit |
709fb3 |
Use @var{pattern} as the pattern.
|
|
Packit |
709fb3 |
If this option is used multiple times or is combined with the
|
|
Packit |
709fb3 |
@option{-f} (@option{--file}) option, search for all patterns given.
|
|
Packit |
709fb3 |
(@option{-e} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -f @var{file}
|
|
Packit |
709fb3 |
@itemx --file=@var{file}
|
|
Packit |
709fb3 |
@opindex -f
|
|
Packit |
709fb3 |
@opindex --file
|
|
Packit |
709fb3 |
@cindex pattern from file
|
|
Packit |
709fb3 |
Obtain patterns from @var{file}, one per line.
|
|
Packit |
709fb3 |
If this option is used multiple times or is combined with the
|
|
Packit |
709fb3 |
@option{-e} (@option{--regexp}) option, search for all patterns given.
|
|
Packit |
709fb3 |
The empty file contains zero patterns, and therefore matches nothing.
|
|
Packit |
709fb3 |
(@option{-f} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -i
|
|
Packit |
709fb3 |
@itemx -y
|
|
Packit |
709fb3 |
@itemx --ignore-case
|
|
Packit |
709fb3 |
@opindex -i
|
|
Packit |
709fb3 |
@opindex -y
|
|
Packit |
709fb3 |
@opindex --ignore-case
|
|
Packit |
709fb3 |
@cindex case insensitive search
|
|
Packit |
709fb3 |
Ignore case distinctions, so that characters that differ only in case
|
|
Packit |
709fb3 |
match each other. Although this is straightforward when letters
|
|
Packit |
709fb3 |
differ in case only via lowercase-uppercase pairs, the behavior is
|
|
Packit |
709fb3 |
unspecified in other situations. For example, uppercase ``S'' has an
|
|
Packit |
709fb3 |
unusual lowercase counterpart ``Å¿'' (Unicode character U+017F, LATIN
|
|
Packit |
709fb3 |
SMALL LETTER LONG S) in many locales, and it is unspecified whether
|
|
Packit |
709fb3 |
this unusual character matches ``S'' or ``s'' even though uppercasing
|
|
Packit |
709fb3 |
it yields ``S''. Another example: the lowercase German letter ``ß''
|
|
Packit |
709fb3 |
(U+00DF, LATIN SMALL LETTER SHARP S) is normally capitalized as the
|
|
Packit |
709fb3 |
two-character string ``SS'' but it does not match ``SS'', and it might
|
|
Packit |
709fb3 |
not match the uppercase letter ``ẞ'' (U+1E9E, LATIN CAPITAL LETTER
|
|
Packit |
709fb3 |
SHARP S) even though lowercasing the latter yields the former.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@option{-y} is an obsolete synonym that is provided for compatibility.
|
|
Packit |
709fb3 |
(@option{-i} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -v
|
|
Packit |
709fb3 |
@itemx --invert-match
|
|
Packit |
709fb3 |
@opindex -v
|
|
Packit |
709fb3 |
@opindex --invert-match
|
|
Packit |
709fb3 |
@cindex invert matching
|
|
Packit |
709fb3 |
@cindex print non-matching lines
|
|
Packit |
709fb3 |
Invert the sense of matching, to select non-matching lines.
|
|
Packit |
709fb3 |
(@option{-v} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -w
|
|
Packit |
709fb3 |
@itemx --word-regexp
|
|
Packit |
709fb3 |
@opindex -w
|
|
Packit |
709fb3 |
@opindex --word-regexp
|
|
Packit |
709fb3 |
@cindex matching whole words
|
|
Packit |
709fb3 |
Select only those lines containing matches that form whole words.
|
|
Packit |
709fb3 |
The test is that the matching substring must either
|
|
Packit |
709fb3 |
be at the beginning of the line,
|
|
Packit |
709fb3 |
or preceded by a non-word constituent character.
|
|
Packit |
709fb3 |
Similarly,
|
|
Packit |
709fb3 |
it must be either at the end of the line
|
|
Packit |
709fb3 |
or followed by a non-word constituent character.
|
|
Packit |
709fb3 |
Word-constituent characters are letters, digits, and the underscore.
|
|
Packit |
709fb3 |
This option has no effect if @option{-x} is also specified.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -x
|
|
Packit |
709fb3 |
@itemx --line-regexp
|
|
Packit |
709fb3 |
@opindex -x
|
|
Packit |
709fb3 |
@opindex --line-regexp
|
|
Packit |
709fb3 |
@cindex match the whole line
|
|
Packit |
709fb3 |
Select only those matches that exactly match the whole line.
|
|
Packit |
709fb3 |
For a regular expression pattern, this is like parenthesizing the
|
|
Packit |
709fb3 |
pattern and then surrounding it with @samp{^} and @samp{$}.
|
|
Packit |
709fb3 |
(@option{-x} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node General Output Control
|
|
Packit |
709fb3 |
@subsection General Output Control
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -c
|
|
Packit |
709fb3 |
@itemx --count
|
|
Packit |
709fb3 |
@opindex -c
|
|
Packit |
709fb3 |
@opindex --count
|
|
Packit |
709fb3 |
@cindex counting lines
|
|
Packit |
709fb3 |
Suppress normal output;
|
|
Packit |
709fb3 |
instead print a count of matching lines for each input file.
|
|
Packit |
709fb3 |
With the @option{-v} (@option{--invert-match}) option,
|
|
Packit |
709fb3 |
count non-matching lines.
|
|
Packit |
709fb3 |
(@option{-c} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --color[=@var{WHEN}]
|
|
Packit |
709fb3 |
@itemx --colour[=@var{WHEN}]
|
|
Packit |
709fb3 |
@opindex --color
|
|
Packit |
709fb3 |
@opindex --colour
|
|
Packit |
709fb3 |
@cindex highlight, color, colour
|
|
Packit |
709fb3 |
Surround the matched (non-empty) strings, matching lines, context lines,
|
|
Packit |
709fb3 |
file names, line numbers, byte offsets, and separators (for fields and
|
|
Packit |
709fb3 |
groups of context lines) with escape sequences to display them in color
|
|
Packit |
709fb3 |
on the terminal.
|
|
Packit |
709fb3 |
The colors are defined by the environment variable @env{GREP_COLORS}
|
|
Packit |
709fb3 |
and default to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36}
|
|
Packit |
709fb3 |
for bold red matched text, magenta file names, green line numbers,
|
|
Packit |
709fb3 |
green byte offsets, cyan separators, and default terminal colors otherwise.
|
|
Packit |
709fb3 |
The deprecated environment variable @env{GREP_COLOR} is still supported,
|
|
Packit |
709fb3 |
but its setting does not have priority;
|
|
Packit |
709fb3 |
it defaults to @samp{01;31} (bold red)
|
|
Packit |
709fb3 |
which only covers the color for matched text.
|
|
Packit |
709fb3 |
@var{WHEN} is @samp{never}, @samp{always}, or @samp{auto}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -L
|
|
Packit |
709fb3 |
@itemx --files-without-match
|
|
Packit |
709fb3 |
@opindex -L
|
|
Packit |
709fb3 |
@opindex --files-without-match
|
|
Packit |
709fb3 |
@cindex files which don't match
|
|
Packit |
709fb3 |
Suppress normal output;
|
|
Packit |
709fb3 |
instead print the name of each input file from which
|
|
Packit |
709fb3 |
no output would normally have been printed.
|
|
Packit |
709fb3 |
The scanning of each file stops on the first match.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -l
|
|
Packit |
709fb3 |
@itemx --files-with-matches
|
|
Packit |
709fb3 |
@opindex -l
|
|
Packit |
709fb3 |
@opindex --files-with-matches
|
|
Packit |
709fb3 |
@cindex names of matching files
|
|
Packit |
709fb3 |
Suppress normal output;
|
|
Packit |
709fb3 |
instead print the name of each input file from which
|
|
Packit |
709fb3 |
output would normally have been printed.
|
|
Packit |
709fb3 |
The scanning of each file stops on the first match.
|
|
Packit |
709fb3 |
(@option{-l} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -m @var{num}
|
|
Packit |
709fb3 |
@itemx --max-count=@var{num}
|
|
Packit |
709fb3 |
@opindex -m
|
|
Packit |
709fb3 |
@opindex --max-count
|
|
Packit |
709fb3 |
@cindex max-count
|
|
Packit |
709fb3 |
Stop after the first @var{num} selected lines.
|
|
Packit |
709fb3 |
If the input is standard input from a regular file,
|
|
Packit |
709fb3 |
and @var{num} selected lines are output,
|
|
Packit |
709fb3 |
@command{grep} ensures that the standard input is positioned
|
|
Packit |
709fb3 |
just after the last selected line before exiting,
|
|
Packit |
709fb3 |
regardless of the presence of trailing context lines.
|
|
Packit |
709fb3 |
This enables a calling process to resume a search.
|
|
Packit |
709fb3 |
For example, the following shell script makes use of it:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
while grep -m 1 PATTERN
|
|
Packit |
709fb3 |
do
|
|
Packit |
709fb3 |
echo xxxx
|
|
Packit |
709fb3 |
done < FILE
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
But the following probably will not work because a pipe is not a regular
|
|
Packit |
709fb3 |
file:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
# This probably will not work.
|
|
Packit |
709fb3 |
cat FILE |
|
|
Packit |
709fb3 |
while grep -m 1 PATTERN
|
|
Packit |
709fb3 |
do
|
|
Packit |
709fb3 |
echo xxxx
|
|
Packit |
709fb3 |
done
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex context lines
|
|
Packit |
709fb3 |
When @command{grep} stops after @var{num} selected lines,
|
|
Packit |
709fb3 |
it outputs any trailing context lines.
|
|
Packit |
709fb3 |
When the @option{-c} or @option{--count} option is also used,
|
|
Packit |
709fb3 |
@command{grep} does not output a count greater than @var{num}.
|
|
Packit |
709fb3 |
When the @option{-v} or @option{--invert-match} option is also used,
|
|
Packit |
709fb3 |
@command{grep} stops after outputting @var{num} non-matching lines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -o
|
|
Packit |
709fb3 |
@itemx --only-matching
|
|
Packit |
709fb3 |
@opindex -o
|
|
Packit |
709fb3 |
@opindex --only-matching
|
|
Packit |
709fb3 |
@cindex only matching
|
|
Packit |
709fb3 |
Print only the matched (non-empty) parts of matching lines,
|
|
Packit |
709fb3 |
with each such part on a separate output line.
|
|
Packit |
709fb3 |
Output lines use the same delimiters as input, and delimiters are null
|
|
Packit |
709fb3 |
bytes if @option{-z} (@option{--null-data}) is also used (@pxref{Other
|
|
Packit |
709fb3 |
Options}).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -q
|
|
Packit |
709fb3 |
@itemx --quiet
|
|
Packit |
709fb3 |
@itemx --silent
|
|
Packit |
709fb3 |
@opindex -q
|
|
Packit |
709fb3 |
@opindex --quiet
|
|
Packit |
709fb3 |
@opindex --silent
|
|
Packit |
709fb3 |
@cindex quiet, silent
|
|
Packit |
709fb3 |
Quiet; do not write anything to standard output.
|
|
Packit |
709fb3 |
Exit immediately with zero status if any match is found,
|
|
Packit |
709fb3 |
even if an error was detected.
|
|
Packit |
709fb3 |
Also see the @option{-s} or @option{--no-messages} option.
|
|
Packit |
709fb3 |
(@option{-q} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -s
|
|
Packit |
709fb3 |
@itemx --no-messages
|
|
Packit |
709fb3 |
@opindex -s
|
|
Packit |
709fb3 |
@opindex --no-messages
|
|
Packit |
709fb3 |
@cindex suppress error messages
|
|
Packit |
709fb3 |
Suppress error messages about nonexistent or unreadable files.
|
|
Packit |
709fb3 |
Portability note:
|
|
Packit |
709fb3 |
unlike GNU @command{grep},
|
|
Packit |
709fb3 |
7th Edition Unix @command{grep} did not conform to POSIX,
|
|
Packit |
709fb3 |
because it lacked @option{-q}
|
|
Packit |
709fb3 |
and its @option{-s} option behaved like
|
|
Packit |
709fb3 |
GNU @command{grep}'s @option{-q} option.@footnote{Of course, 7th Edition
|
|
Packit |
709fb3 |
Unix predated POSIX by several years!}
|
|
Packit |
709fb3 |
USG-style @command{grep} also lacked @option{-q}
|
|
Packit |
709fb3 |
but its @option{-s} option behaved like GNU @command{grep}'s.
|
|
Packit |
709fb3 |
Portable shell scripts should avoid both
|
|
Packit |
709fb3 |
@option{-q} and @option{-s} and should redirect
|
|
Packit |
709fb3 |
standard and error output to @file{/dev/null} instead.
|
|
Packit |
709fb3 |
(@option{-s} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Output Line Prefix Control
|
|
Packit |
709fb3 |
@subsection Output Line Prefix Control
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
When several prefix fields are to be output,
|
|
Packit |
709fb3 |
the order is always file name, line number, and byte offset,
|
|
Packit |
709fb3 |
regardless of the order in which these options were specified.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -b
|
|
Packit |
709fb3 |
@itemx --byte-offset
|
|
Packit |
709fb3 |
@opindex -b
|
|
Packit |
709fb3 |
@opindex --byte-offset
|
|
Packit |
709fb3 |
@cindex byte offset
|
|
Packit |
709fb3 |
Print the 0-based byte offset within the input file
|
|
Packit |
709fb3 |
before each line of output.
|
|
Packit |
709fb3 |
If @option{-o} (@option{--only-matching}) is specified,
|
|
Packit |
709fb3 |
print the offset of the matching part itself.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -H
|
|
Packit |
709fb3 |
@itemx --with-filename
|
|
Packit |
709fb3 |
@opindex -H
|
|
Packit |
709fb3 |
@opindex --with-filename
|
|
Packit |
709fb3 |
@cindex with filename prefix
|
|
Packit |
709fb3 |
Print the file name for each match.
|
|
Packit |
709fb3 |
This is the default when there is more than one file to search.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -h
|
|
Packit |
709fb3 |
@itemx --no-filename
|
|
Packit |
709fb3 |
@opindex -h
|
|
Packit |
709fb3 |
@opindex --no-filename
|
|
Packit |
709fb3 |
@cindex no filename prefix
|
|
Packit |
709fb3 |
Suppress the prefixing of file names on output.
|
|
Packit |
709fb3 |
This is the default when there is only one file
|
|
Packit |
709fb3 |
(or only standard input) to search.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --label=@var{LABEL}
|
|
Packit |
709fb3 |
@opindex --label
|
|
Packit |
709fb3 |
@cindex changing name of standard input
|
|
Packit |
709fb3 |
Display input actually coming from standard input
|
|
Packit |
709fb3 |
as input coming from file @var{LABEL}. This is
|
|
Packit |
709fb3 |
especially useful when implementing tools like
|
|
Packit |
709fb3 |
@command{zgrep}; e.g.:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
gzip -cd foo.gz | grep --label=foo -H something
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -n
|
|
Packit |
709fb3 |
@itemx --line-number
|
|
Packit |
709fb3 |
@opindex -n
|
|
Packit |
709fb3 |
@opindex --line-number
|
|
Packit |
709fb3 |
@cindex line numbering
|
|
Packit |
709fb3 |
Prefix each line of output with the 1-based line number within its input file.
|
|
Packit |
709fb3 |
(@option{-n} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -T
|
|
Packit |
709fb3 |
@itemx --initial-tab
|
|
Packit |
709fb3 |
@opindex -T
|
|
Packit |
709fb3 |
@opindex --initial-tab
|
|
Packit |
709fb3 |
@cindex tab-aligned content lines
|
|
Packit |
709fb3 |
Make sure that the first character of actual line content lies on a tab stop,
|
|
Packit |
709fb3 |
so that the alignment of tabs looks normal.
|
|
Packit |
709fb3 |
This is useful with options that prefix their output to the actual content:
|
|
Packit |
709fb3 |
@option{-H}, @option{-n}, and @option{-b}.
|
|
Packit |
709fb3 |
This may also prepend spaces to output line numbers and byte offsets
|
|
Packit |
709fb3 |
so that lines from a single file all start at the same column.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -Z
|
|
Packit |
709fb3 |
@itemx --null
|
|
Packit |
709fb3 |
@opindex -Z
|
|
Packit |
709fb3 |
@opindex --null
|
|
Packit |
709fb3 |
@cindex zero-terminated file names
|
|
Packit |
709fb3 |
Output a zero byte (the ASCII NUL character)
|
|
Packit |
709fb3 |
instead of the character that normally follows a file name.
|
|
Packit |
709fb3 |
For example,
|
|
Packit |
709fb3 |
@samp{grep -lZ} outputs a zero byte after each file name
|
|
Packit |
709fb3 |
instead of the usual newline.
|
|
Packit |
709fb3 |
This option makes the output unambiguous,
|
|
Packit |
709fb3 |
even in the presence of file names containing unusual characters like newlines.
|
|
Packit |
709fb3 |
This option can be used with commands like
|
|
Packit |
709fb3 |
@samp{find -print0}, @samp{perl -0}, @samp{sort -z}, and @samp{xargs -0}
|
|
Packit |
709fb3 |
to process arbitrary file names,
|
|
Packit |
709fb3 |
even those that contain newline characters.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Context Line Control
|
|
Packit |
709fb3 |
@subsection Context Line Control
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex context lines
|
|
Packit |
709fb3 |
@dfn{Context lines} are non-matching lines that are near a matching line.
|
|
Packit |
709fb3 |
They are output only if one of the following options are used.
|
|
Packit |
709fb3 |
Regardless of how these options are set,
|
|
Packit |
709fb3 |
@command{grep} never outputs any given line more than once.
|
|
Packit |
709fb3 |
If the @option{-o} (@option{--only-matching}) option is specified,
|
|
Packit |
709fb3 |
these options have no effect and a warning is given upon their use.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -A @var{num}
|
|
Packit |
709fb3 |
@itemx --after-context=@var{num}
|
|
Packit |
709fb3 |
@opindex -A
|
|
Packit |
709fb3 |
@opindex --after-context
|
|
Packit |
709fb3 |
@cindex after context
|
|
Packit |
709fb3 |
@cindex context lines, after match
|
|
Packit |
709fb3 |
Print @var{num} lines of trailing context after matching lines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -B @var{num}
|
|
Packit |
709fb3 |
@itemx --before-context=@var{num}
|
|
Packit |
709fb3 |
@opindex -B
|
|
Packit |
709fb3 |
@opindex --before-context
|
|
Packit |
709fb3 |
@cindex before context
|
|
Packit |
709fb3 |
@cindex context lines, before match
|
|
Packit |
709fb3 |
Print @var{num} lines of leading context before matching lines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -C @var{num}
|
|
Packit |
709fb3 |
@itemx -@var{num}
|
|
Packit |
709fb3 |
@itemx --context=@var{num}
|
|
Packit |
709fb3 |
@opindex -C
|
|
Packit |
709fb3 |
@opindex --context
|
|
Packit |
709fb3 |
@opindex -@var{num}
|
|
Packit |
709fb3 |
@cindex context lines
|
|
Packit |
709fb3 |
Print @var{num} lines of leading and trailing output context.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --group-separator=@var{string}
|
|
Packit |
709fb3 |
@opindex --group-separator
|
|
Packit |
709fb3 |
@cindex group separator
|
|
Packit |
709fb3 |
When @option{-A}, @option{-B} or @option{-C} are in use,
|
|
Packit |
709fb3 |
print @var{string} instead of @option{--} between groups of lines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --no-group-separator
|
|
Packit |
709fb3 |
@opindex --group-separator
|
|
Packit |
709fb3 |
@cindex group separator
|
|
Packit |
709fb3 |
When @option{-A}, @option{-B} or @option{-C} are in use,
|
|
Packit |
709fb3 |
do not print a separator between groups of lines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Here are some points about how @command{grep} chooses
|
|
Packit |
709fb3 |
the separator to print between prefix fields and line content:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@itemize @bullet
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Matching lines normally use @samp{:} as a separator
|
|
Packit |
709fb3 |
between prefix fields and actual line content.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Context (i.e., non-matching) lines use @samp{-} instead.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
When context is not specified,
|
|
Packit |
709fb3 |
matching lines are simply output one right after another.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
When context is specified,
|
|
Packit |
709fb3 |
lines that are adjacent in the input form a group
|
|
Packit |
709fb3 |
and are output one right after another, while
|
|
Packit |
709fb3 |
by default a separator appears between non-adjacent groups.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
The default separator
|
|
Packit |
709fb3 |
is a @samp{--} line; its presence and appearance
|
|
Packit |
709fb3 |
can be changed with the options above.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Each group may contain
|
|
Packit |
709fb3 |
several matching lines when they are close enough to each other
|
|
Packit |
709fb3 |
that two adjacent groups connect and can merge into a single
|
|
Packit |
709fb3 |
contiguous one.
|
|
Packit |
709fb3 |
@end itemize
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node File and Directory Selection
|
|
Packit |
709fb3 |
@subsection File and Directory Selection
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -a
|
|
Packit |
709fb3 |
@itemx --text
|
|
Packit |
709fb3 |
@opindex -a
|
|
Packit |
709fb3 |
@opindex --text
|
|
Packit |
709fb3 |
@cindex suppress binary data
|
|
Packit |
709fb3 |
@cindex binary files
|
|
Packit |
709fb3 |
Process a binary file as if it were text;
|
|
Packit |
709fb3 |
this is equivalent to the @samp{--binary-files=text} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --binary-files=@var{type}
|
|
Packit |
709fb3 |
@opindex --binary-files
|
|
Packit |
709fb3 |
@cindex binary files
|
|
Packit |
709fb3 |
If a file's data or metadata
|
|
Packit |
709fb3 |
indicate that the file contains binary data,
|
|
Packit |
709fb3 |
assume that the file is of type @var{type}.
|
|
Packit |
709fb3 |
Non-text bytes indicate binary data; these are either output bytes that are
|
|
Packit |
709fb3 |
improperly encoded for the current locale (@pxref{Environment
|
|
Packit |
709fb3 |
Variables}), or null input bytes when the
|
|
Packit |
709fb3 |
@option{-z} (@option{--null-data}) option is not given (@pxref{Other
|
|
Packit |
709fb3 |
Options}).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
By default, @var{type} is @samp{binary}, and @command{grep}
|
|
Packit |
709fb3 |
suppresses output after null input binary data is discovered,
|
|
Packit |
709fb3 |
and suppresses output lines that contain improperly encoded data.
|
|
Packit |
709fb3 |
When some output is suppressed, @command{grep} follows any output
|
|
Packit |
709fb3 |
with a one-line message saying that a binary file matches.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
If @var{type} is @samp{without-match},
|
|
Packit |
709fb3 |
when @command{grep} discovers null input binary data
|
|
Packit |
709fb3 |
it assumes that the rest of the file does not match;
|
|
Packit |
709fb3 |
this is equivalent to the @option{-I} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
If @var{type} is @samp{text},
|
|
Packit |
709fb3 |
@command{grep} processes binary data as if it were text;
|
|
Packit |
709fb3 |
this is equivalent to the @option{-a} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
When @var{type} is @samp{binary}, @command{grep} may treat non-text
|
|
Packit |
709fb3 |
bytes as line terminators even without the @option{-z}
|
|
Packit |
709fb3 |
(@option{--null-data}) option. This means choosing @samp{binary}
|
|
Packit |
709fb3 |
versus @samp{text} can affect whether a pattern matches a file. For
|
|
Packit |
709fb3 |
example, when @var{type} is @samp{binary} the pattern @samp{q$} might
|
|
Packit |
709fb3 |
match @samp{q} immediately followed by a null byte, even though this
|
|
Packit |
709fb3 |
is not matched when @var{type} is @samp{text}. Conversely, when
|
|
Packit |
709fb3 |
@var{type} is @samp{binary} the pattern @samp{.} (period) might not
|
|
Packit |
709fb3 |
match a null byte.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@emph{Warning:} The @option{-a} (@option{--binary-files=text}) option
|
|
Packit |
709fb3 |
might output binary garbage, which can have nasty side effects if the
|
|
Packit |
709fb3 |
output is a terminal and if the terminal driver interprets some of it
|
|
Packit |
709fb3 |
as commands. On the other hand, when reading files whose text
|
|
Packit |
709fb3 |
encodings are unknown, it can be helpful to use @option{-a} or to set
|
|
Packit |
709fb3 |
@samp{LC_ALL='C'} in the environment, in order to find more matches
|
|
Packit |
709fb3 |
even if the matches are unsafe for direct display.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -D @var{action}
|
|
Packit |
709fb3 |
@itemx --devices=@var{action}
|
|
Packit |
709fb3 |
@opindex -D
|
|
Packit |
709fb3 |
@opindex --devices
|
|
Packit |
709fb3 |
@cindex device search
|
|
Packit |
709fb3 |
If an input file is a device, FIFO, or socket, use @var{action} to process it.
|
|
Packit |
709fb3 |
If @var{action} is @samp{read},
|
|
Packit |
709fb3 |
all devices are read just as if they were ordinary files.
|
|
Packit |
709fb3 |
If @var{action} is @samp{skip},
|
|
Packit |
709fb3 |
devices, FIFOs, and sockets are silently skipped.
|
|
Packit |
709fb3 |
By default, devices are read if they are on the command line or if the
|
|
Packit |
709fb3 |
@option{-R} (@option{--dereference-recursive}) option is used, and are
|
|
Packit |
709fb3 |
skipped if they are encountered recursively and the @option{-r}
|
|
Packit |
709fb3 |
(@option{--recursive}) option is used.
|
|
Packit |
709fb3 |
This option has no effect on a file that is read via standard input.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -d @var{action}
|
|
Packit |
709fb3 |
@itemx --directories=@var{action}
|
|
Packit |
709fb3 |
@opindex -d
|
|
Packit |
709fb3 |
@opindex --directories
|
|
Packit |
709fb3 |
@cindex directory search
|
|
Packit |
709fb3 |
@cindex symbolic links
|
|
Packit |
709fb3 |
If an input file is a directory, use @var{action} to process it.
|
|
Packit |
709fb3 |
By default, @var{action} is @samp{read},
|
|
Packit |
709fb3 |
which means that directories are read just as if they were ordinary files
|
|
Packit |
709fb3 |
(some operating systems and file systems disallow this,
|
|
Packit |
709fb3 |
and will cause @command{grep}
|
|
Packit |
709fb3 |
to print error messages for every directory or silently skip them).
|
|
Packit |
709fb3 |
If @var{action} is @samp{skip}, directories are silently skipped.
|
|
Packit |
709fb3 |
If @var{action} is @samp{recurse},
|
|
Packit |
709fb3 |
@command{grep} reads all files under each directory, recursively,
|
|
Packit |
709fb3 |
following command-line symbolic links and skipping other symlinks;
|
|
Packit |
709fb3 |
this is equivalent to the @option{-r} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --exclude=@var{glob}
|
|
Packit |
709fb3 |
@opindex --exclude
|
|
Packit |
709fb3 |
@cindex exclude files
|
|
Packit |
709fb3 |
@cindex searching directory trees
|
|
Packit |
709fb3 |
Skip any command-line file with a name suffix that matches the pattern
|
|
Packit |
709fb3 |
@var{glob}, using wildcard matching; a name suffix is either the whole
|
|
Packit |
709fb3 |
name, or any suffix starting after a @samp{/} and before a
|
|
Packit |
709fb3 |
non-@samp{/}. When searching recursively, skip any subfile whose base
|
|
Packit |
709fb3 |
name matches @var{glob}; the base name is the part after the last
|
|
Packit |
709fb3 |
@samp{/}. A pattern can use
|
|
Packit |
709fb3 |
@samp{*}, @samp{?}, and @samp{[}...@samp{]} as wildcards,
|
|
Packit |
709fb3 |
and @code{\} to quote a wildcard or backslash character literally.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --exclude-from=@var{file}
|
|
Packit |
709fb3 |
@opindex --exclude-from
|
|
Packit |
709fb3 |
@cindex exclude files
|
|
Packit |
709fb3 |
@cindex searching directory trees
|
|
Packit |
709fb3 |
Skip files whose name matches any of the patterns
|
|
Packit |
709fb3 |
read from @var{file} (using wildcard matching as described
|
|
Packit |
709fb3 |
under @option{--exclude}).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --exclude-dir=@var{glob}
|
|
Packit |
709fb3 |
@opindex --exclude-dir
|
|
Packit |
709fb3 |
@cindex exclude directories
|
|
Packit |
709fb3 |
Skip any command-line directory with a name suffix that matches the
|
|
Packit |
709fb3 |
pattern @var{glob}. When searching recursively, skip any subdirectory
|
|
Packit |
709fb3 |
whose base name matches @var{glob}. Ignore any redundant trailing
|
|
Packit |
709fb3 |
slashes in @var{glob}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -I
|
|
Packit |
709fb3 |
Process a binary file as if it did not contain matching data;
|
|
Packit |
709fb3 |
this is equivalent to the @samp{--binary-files=without-match} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --include=@var{glob}
|
|
Packit |
709fb3 |
@opindex --include
|
|
Packit |
709fb3 |
@cindex include files
|
|
Packit |
709fb3 |
@cindex searching directory trees
|
|
Packit |
709fb3 |
Search only files whose name matches @var{glob},
|
|
Packit |
709fb3 |
using wildcard matching as described under @option{--exclude}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -r
|
|
Packit |
709fb3 |
@itemx --recursive
|
|
Packit |
709fb3 |
@opindex -r
|
|
Packit |
709fb3 |
@opindex --recursive
|
|
Packit |
709fb3 |
@cindex recursive search
|
|
Packit |
709fb3 |
@cindex searching directory trees
|
|
Packit |
709fb3 |
@cindex symbolic links
|
|
Packit |
709fb3 |
For each directory operand,
|
|
Packit |
709fb3 |
read and process all files in that directory, recursively.
|
|
Packit |
709fb3 |
Follow symbolic links on the command line, but skip symlinks
|
|
Packit |
709fb3 |
that are encountered recursively.
|
|
Packit |
709fb3 |
Note that if no file operand is given, grep searches the working directory.
|
|
Packit |
709fb3 |
This is the same as the @samp{--directories=recurse} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -R
|
|
Packit |
709fb3 |
@itemx --dereference-recursive
|
|
Packit |
709fb3 |
@opindex -R
|
|
Packit |
709fb3 |
@opindex --dereference-recursive
|
|
Packit |
709fb3 |
@cindex recursive search
|
|
Packit |
709fb3 |
@cindex searching directory trees
|
|
Packit |
709fb3 |
@cindex symbolic links
|
|
Packit |
709fb3 |
For each directory operand, read and process all files in that
|
|
Packit |
709fb3 |
directory, recursively, following all symbolic links.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Other Options
|
|
Packit |
709fb3 |
@subsection Other Options
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item --line-buffered
|
|
Packit |
709fb3 |
@opindex --line-buffered
|
|
Packit |
709fb3 |
@cindex line buffering
|
|
Packit |
709fb3 |
Use line buffering on output.
|
|
Packit |
709fb3 |
This can cause a performance penalty.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -U
|
|
Packit |
709fb3 |
@itemx --binary
|
|
Packit |
709fb3 |
@opindex -U
|
|
Packit |
709fb3 |
@opindex --binary
|
|
Packit |
709fb3 |
@cindex MS-Windows binary I/O
|
|
Packit |
709fb3 |
@cindex binary I/O
|
|
Packit |
709fb3 |
On platforms that distinguish between text and binary I/O,
|
|
Packit |
709fb3 |
use the latter when reading and writing files other
|
|
Packit |
709fb3 |
than the user's terminal, so that all input bytes are read and written
|
|
Packit |
709fb3 |
as-is. This overrides the default behavior where @command{grep}
|
|
Packit |
709fb3 |
follows the operating system's advice whether to use text or binary
|
|
Packit |
709fb3 |
I/O@. On MS-Windows when @command{grep} uses text I/O it reads a
|
|
Packit |
709fb3 |
carriage return--newline pair as a newline and a Control-Z as
|
|
Packit |
709fb3 |
end-of-file, and it writes a newline as a carriage return--newline
|
|
Packit |
709fb3 |
pair.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
When using text I/O @option{--byte-offset} (@option{-b}) counts and
|
|
Packit |
709fb3 |
@option{--binary-files} heuristics apply to input data after text-I/O
|
|
Packit |
709fb3 |
processing. Also, the @option{--binary-files} heuristics need not agree
|
|
Packit |
709fb3 |
with the @option{--binary} option; that is, they may treat the data as
|
|
Packit |
709fb3 |
text even if @option{--binary} is given, or vice versa.
|
|
Packit |
709fb3 |
@xref{File and Directory Selection}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This option has no effect on GNU and other POSIX-compatible platforms,
|
|
Packit |
709fb3 |
which do not distinguish text from binary I/O.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -z
|
|
Packit |
709fb3 |
@itemx --null-data
|
|
Packit |
709fb3 |
@opindex -z
|
|
Packit |
709fb3 |
@opindex --null-data
|
|
Packit |
709fb3 |
@cindex zero-terminated lines
|
|
Packit |
709fb3 |
Treat input and output data as sequences of lines, each terminated by
|
|
Packit |
709fb3 |
a zero byte (the ASCII NUL character) instead of a newline.
|
|
Packit |
709fb3 |
Like the @option{-Z} or @option{--null} option,
|
|
Packit |
709fb3 |
this option can be used with commands like
|
|
Packit |
709fb3 |
@samp{sort -z} to process arbitrary file names.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Environment Variables
|
|
Packit |
709fb3 |
@section Environment Variables
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The behavior of @command{grep} is affected
|
|
Packit |
709fb3 |
by the following environment variables.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@vindex LANGUAGE @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LC_ALL @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LC_MESSAGES @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LANG @r{environment variable}
|
|
Packit |
709fb3 |
The locale for category @w{@code{LC_@var{foo}}}
|
|
Packit |
709fb3 |
is specified by examining the three environment variables
|
|
Packit |
709fb3 |
@env{LC_ALL}, @w{@env{LC_@var{foo}}}, and @env{LANG},
|
|
Packit |
709fb3 |
in that order.
|
|
Packit |
709fb3 |
The first of these variables that is set specifies the locale.
|
|
Packit |
709fb3 |
For example, if @env{LC_ALL} is not set,
|
|
Packit |
709fb3 |
but @env{LC_COLLATE} is set to @samp{pt_BR},
|
|
Packit |
709fb3 |
then the Brazilian Portuguese locale is used
|
|
Packit |
709fb3 |
for the @env{LC_COLLATE} category.
|
|
Packit |
709fb3 |
As a special case for @env{LC_MESSAGES} only, the environment variable
|
|
Packit |
709fb3 |
@env{LANGUAGE} can contain a colon-separated list of languages that
|
|
Packit |
709fb3 |
overrides the three environment variables that ordinarily specify
|
|
Packit |
709fb3 |
the @env{LC_MESSAGES} category.
|
|
Packit |
709fb3 |
The @samp{C} locale is used if none of these environment variables are set,
|
|
Packit |
709fb3 |
if the locale catalog is not installed,
|
|
Packit |
709fb3 |
or if @command{grep} was not compiled
|
|
Packit |
709fb3 |
with national language support (NLS).
|
|
Packit |
709fb3 |
The shell command @code{locale -a} lists locales that are currently available.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Many of the environment variables in the following list let you
|
|
Packit |
709fb3 |
control highlighting using
|
|
Packit |
709fb3 |
Select Graphic Rendition (SGR)
|
|
Packit |
709fb3 |
commands interpreted by the terminal or terminal emulator.
|
|
Packit |
709fb3 |
(See the
|
|
Packit |
709fb3 |
section
|
|
Packit |
709fb3 |
in the documentation of your text terminal
|
|
Packit |
709fb3 |
for permitted values and their meanings as character attributes.)
|
|
Packit |
709fb3 |
These substring values are integers in decimal representation
|
|
Packit |
709fb3 |
and can be concatenated with semicolons.
|
|
Packit |
709fb3 |
@command{grep} takes care of assembling the result
|
|
Packit |
709fb3 |
into a complete SGR sequence (@samp{\33[}...@samp{m}).
|
|
Packit |
709fb3 |
Common values to concatenate include
|
|
Packit |
709fb3 |
@samp{1} for bold,
|
|
Packit |
709fb3 |
@samp{4} for underline,
|
|
Packit |
709fb3 |
@samp{5} for blink,
|
|
Packit |
709fb3 |
@samp{7} for inverse,
|
|
Packit |
709fb3 |
@samp{39} for default foreground color,
|
|
Packit |
709fb3 |
@samp{30} to @samp{37} for foreground colors,
|
|
Packit |
709fb3 |
@samp{90} to @samp{97} for 16-color mode foreground colors,
|
|
Packit |
709fb3 |
@samp{38;5;0} to @samp{38;5;255}
|
|
Packit |
709fb3 |
for 88-color and 256-color modes foreground colors,
|
|
Packit |
709fb3 |
@samp{49} for default background color,
|
|
Packit |
709fb3 |
@samp{40} to @samp{47} for background colors,
|
|
Packit |
709fb3 |
@samp{100} to @samp{107} for 16-color mode background colors,
|
|
Packit |
709fb3 |
and @samp{48;5;0} to @samp{48;5;255}
|
|
Packit |
709fb3 |
for 88-color and 256-color modes background colors.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The two-letter names used in the @env{GREP_COLORS} environment variable
|
|
Packit |
709fb3 |
(and some of the others) refer to terminal ``capabilities,'' the ability
|
|
Packit |
709fb3 |
of a terminal to highlight text, or change its color, and so on.
|
|
Packit |
709fb3 |
These capabilities are stored in an online database and accessed by
|
|
Packit |
709fb3 |
the @code{terminfo} library.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex environment variables
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @env
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item GREP_OPTIONS
|
|
Packit |
709fb3 |
@vindex GREP_OPTIONS @r{environment variable}
|
|
Packit |
709fb3 |
@cindex default options environment variable
|
|
Packit |
709fb3 |
This variable specifies default options to be placed in front of any
|
|
Packit |
709fb3 |
explicit options.
|
|
Packit |
709fb3 |
As this causes problems when writing portable scripts, this feature
|
|
Packit |
709fb3 |
will be removed in a future release of @command{grep}, and @command{grep}
|
|
Packit |
709fb3 |
warns if it is used. Please use an alias or script instead.
|
|
Packit |
709fb3 |
For example, if @command{grep} is in the directory @samp{/usr/bin} you
|
|
Packit |
709fb3 |
can prepend @file{$HOME/bin} to your @env{PATH} and create an
|
|
Packit |
709fb3 |
executable script @file{$HOME/bin/grep} containing the following:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
#! /bin/sh
|
|
Packit |
709fb3 |
export PATH=/usr/bin
|
|
Packit |
709fb3 |
exec grep --color=auto --devices=skip "$@@"
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item GREP_COLOR
|
|
Packit |
709fb3 |
@vindex GREP_COLOR @r{environment variable}
|
|
Packit |
709fb3 |
@cindex highlight markers
|
|
Packit |
709fb3 |
This variable specifies the color used to highlight matched (non-empty) text.
|
|
Packit |
709fb3 |
It is deprecated in favor of @env{GREP_COLORS}, but still supported.
|
|
Packit |
709fb3 |
The @samp{mt}, @samp{ms}, and @samp{mc} capabilities of @env{GREP_COLORS}
|
|
Packit |
709fb3 |
have priority over it.
|
|
Packit |
709fb3 |
It can only specify the color used to highlight
|
|
Packit |
709fb3 |
the matching non-empty text in any matching line
|
|
Packit |
709fb3 |
(a selected line when the @option{-v} command-line option is omitted,
|
|
Packit |
709fb3 |
or a context line when @option{-v} is specified).
|
|
Packit |
709fb3 |
The default is @samp{01;31},
|
|
Packit |
709fb3 |
which means a bold red foreground text on the terminal's default background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item GREP_COLORS
|
|
Packit |
709fb3 |
@vindex GREP_COLORS @r{environment variable}
|
|
Packit |
709fb3 |
@cindex highlight markers
|
|
Packit |
709fb3 |
This variable specifies the colors and other attributes
|
|
Packit |
709fb3 |
used to highlight various parts of the output.
|
|
Packit |
709fb3 |
Its value is a colon-separated list of @code{terminfo} capabilities
|
|
Packit |
709fb3 |
that defaults to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36}
|
|
Packit |
709fb3 |
with the @samp{rv} and @samp{ne} boolean capabilities omitted (i.e., false).
|
|
Packit |
709fb3 |
Supported capabilities are as follows.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @code
|
|
Packit |
709fb3 |
@item sl=
|
|
Packit |
709fb3 |
@vindex sl GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for whole selected lines
|
|
Packit |
709fb3 |
(i.e.,
|
|
Packit |
709fb3 |
matching lines when the @option{-v} command-line option is omitted,
|
|
Packit |
709fb3 |
or non-matching lines when @option{-v} is specified).
|
|
Packit |
709fb3 |
If however the boolean @samp{rv} capability
|
|
Packit |
709fb3 |
and the @option{-v} command-line option are both specified,
|
|
Packit |
709fb3 |
it applies to context matching lines instead.
|
|
Packit |
709fb3 |
The default is empty (i.e., the terminal's default color pair).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item cx=
|
|
Packit |
709fb3 |
@vindex cx GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for whole context lines
|
|
Packit |
709fb3 |
(i.e.,
|
|
Packit |
709fb3 |
non-matching lines when the @option{-v} command-line option is omitted,
|
|
Packit |
709fb3 |
or matching lines when @option{-v} is specified).
|
|
Packit |
709fb3 |
If however the boolean @samp{rv} capability
|
|
Packit |
709fb3 |
and the @option{-v} command-line option are both specified,
|
|
Packit |
709fb3 |
it applies to selected non-matching lines instead.
|
|
Packit |
709fb3 |
The default is empty (i.e., the terminal's default color pair).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item rv
|
|
Packit |
709fb3 |
@vindex rv GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
Boolean value that reverses (swaps) the meanings of
|
|
Packit |
709fb3 |
the @samp{sl=} and @samp{cx=} capabilities
|
|
Packit |
709fb3 |
when the @option{-v} command-line option is specified.
|
|
Packit |
709fb3 |
The default is false (i.e., the capability is omitted).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item mt=01;31
|
|
Packit |
709fb3 |
@vindex mt GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for matching non-empty text in any matching line
|
|
Packit |
709fb3 |
(i.e.,
|
|
Packit |
709fb3 |
a selected line when the @option{-v} command-line option is omitted,
|
|
Packit |
709fb3 |
or a context line when @option{-v} is specified).
|
|
Packit |
709fb3 |
Setting this is equivalent to setting both @samp{ms=} and @samp{mc=}
|
|
Packit |
709fb3 |
at once to the same value.
|
|
Packit |
709fb3 |
The default is a bold red text foreground over the current line background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item ms=01;31
|
|
Packit |
709fb3 |
@vindex ms GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for matching non-empty text in a selected line.
|
|
Packit |
709fb3 |
(This is used only when the @option{-v} command-line option is omitted.)
|
|
Packit |
709fb3 |
The effect of the @samp{sl=} (or @samp{cx=} if @samp{rv}) capability
|
|
Packit |
709fb3 |
remains active when this takes effect.
|
|
Packit |
709fb3 |
The default is a bold red text foreground over the current line background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item mc=01;31
|
|
Packit |
709fb3 |
@vindex mc GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for matching non-empty text in a context line.
|
|
Packit |
709fb3 |
(This is used only when the @option{-v} command-line option is specified.)
|
|
Packit |
709fb3 |
The effect of the @samp{cx=} (or @samp{sl=} if @samp{rv}) capability
|
|
Packit |
709fb3 |
remains active when this takes effect.
|
|
Packit |
709fb3 |
The default is a bold red text foreground over the current line background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item fn=35
|
|
Packit |
709fb3 |
@vindex fn GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for file names prefixing any content line.
|
|
Packit |
709fb3 |
The default is a magenta text foreground over the terminal's default background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item ln=32
|
|
Packit |
709fb3 |
@vindex ln GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for line numbers prefixing any content line.
|
|
Packit |
709fb3 |
The default is a green text foreground over the terminal's default background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item bn=32
|
|
Packit |
709fb3 |
@vindex bn GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for byte offsets prefixing any content line.
|
|
Packit |
709fb3 |
The default is a green text foreground over the terminal's default background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item se=36
|
|
Packit |
709fb3 |
@vindex fn GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
SGR substring for separators that are inserted
|
|
Packit |
709fb3 |
between selected line fields (@samp{:}),
|
|
Packit |
709fb3 |
between context line fields (@samp{-}),
|
|
Packit |
709fb3 |
and between groups of adjacent lines
|
|
Packit |
709fb3 |
when nonzero context is specified (@samp{--}).
|
|
Packit |
709fb3 |
The default is a cyan text foreground over the terminal's default background.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item ne
|
|
Packit |
709fb3 |
@vindex ne GREP_COLORS @r{capability}
|
|
Packit |
709fb3 |
Boolean value that prevents clearing to the end of line
|
|
Packit |
709fb3 |
using Erase in Line (EL) to Right (@samp{\33[K})
|
|
Packit |
709fb3 |
each time a colorized item ends.
|
|
Packit |
709fb3 |
This is needed on terminals on which EL is not supported.
|
|
Packit |
709fb3 |
It is otherwise useful on terminals
|
|
Packit |
709fb3 |
for which the @code{back_color_erase}
|
|
Packit |
709fb3 |
(@code{bce}) boolean @code{terminfo} capability does not apply,
|
|
Packit |
709fb3 |
when the chosen highlight colors do not affect the background,
|
|
Packit |
709fb3 |
or when EL is too slow or causes too much flicker.
|
|
Packit |
709fb3 |
The default is false (i.e., the capability is omitted).
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Note that boolean capabilities have no @samp{=}... part.
|
|
Packit |
709fb3 |
They are omitted (i.e., false) by default and become true when specified.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item LC_ALL
|
|
Packit |
709fb3 |
@itemx LC_COLLATE
|
|
Packit |
709fb3 |
@itemx LANG
|
|
Packit |
709fb3 |
@vindex LC_ALL @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LC_COLLATE @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LANG @r{environment variable}
|
|
Packit |
709fb3 |
@cindex character type
|
|
Packit |
709fb3 |
@cindex national language support
|
|
Packit |
709fb3 |
@cindex NLS
|
|
Packit |
709fb3 |
These variables specify the locale for the @env{LC_COLLATE} category,
|
|
Packit |
709fb3 |
which might affect how range expressions like @samp{[a-z]} are
|
|
Packit |
709fb3 |
interpreted.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item LC_ALL
|
|
Packit |
709fb3 |
@itemx LC_CTYPE
|
|
Packit |
709fb3 |
@itemx LANG
|
|
Packit |
709fb3 |
@vindex LC_ALL @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LC_CTYPE @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LANG @r{environment variable}
|
|
Packit |
709fb3 |
These variables specify the locale for the @env{LC_CTYPE} category,
|
|
Packit |
709fb3 |
which determines the type of characters,
|
|
Packit |
709fb3 |
e.g., which characters are whitespace.
|
|
Packit |
709fb3 |
This category also determines the character encoding, that is, whether
|
|
Packit |
709fb3 |
text is encoded in UTF-8, ASCII, or some other encoding. In the
|
|
Packit |
709fb3 |
@samp{C} or @samp{POSIX} locale, all characters are encoded as a
|
|
Packit |
709fb3 |
single byte and every byte is a valid character.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item LANGUAGE
|
|
Packit |
709fb3 |
@itemx LC_ALL
|
|
Packit |
709fb3 |
@itemx LC_MESSAGES
|
|
Packit |
709fb3 |
@itemx LANG
|
|
Packit |
709fb3 |
@vindex LANGUAGE @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LC_ALL @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LC_MESSAGES @r{environment variable}
|
|
Packit |
709fb3 |
@vindex LANG @r{environment variable}
|
|
Packit |
709fb3 |
@cindex language of messages
|
|
Packit |
709fb3 |
@cindex message language
|
|
Packit |
709fb3 |
@cindex national language support
|
|
Packit |
709fb3 |
@cindex translation of message language
|
|
Packit |
709fb3 |
These variables specify the locale for the @env{LC_MESSAGES} category,
|
|
Packit |
709fb3 |
which determines the language that @command{grep} uses for messages.
|
|
Packit |
709fb3 |
The default @samp{C} locale uses American English messages.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item POSIXLY_CORRECT
|
|
Packit |
709fb3 |
@vindex POSIXLY_CORRECT @r{environment variable}
|
|
Packit |
709fb3 |
If set, @command{grep} behaves as POSIX requires; otherwise,
|
|
Packit |
709fb3 |
@command{grep} behaves more like other GNU programs.
|
|
Packit |
709fb3 |
POSIX
|
|
Packit |
709fb3 |
requires that options that
|
|
Packit |
709fb3 |
follow file names must be treated as file names;
|
|
Packit |
709fb3 |
by default,
|
|
Packit |
709fb3 |
such options are permuted to the front of the operand list
|
|
Packit |
709fb3 |
and are treated as options.
|
|
Packit |
709fb3 |
Also, @env{POSIXLY_CORRECT} disables special handling of an
|
|
Packit |
709fb3 |
invalid bracket expression. @xref{invalid-bracket-expr}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item _@var{N}_GNU_nonoption_argv_flags_
|
|
Packit |
709fb3 |
@vindex _@var{N}_GNU_nonoption_argv_flags_ @r{environment variable}
|
|
Packit |
709fb3 |
(Here @code{@var{N}} is @command{grep}'s numeric process ID.)
|
|
Packit |
709fb3 |
If the @var{i}th character of this environment variable's value is @samp{1},
|
|
Packit |
709fb3 |
do not consider the @var{i}th operand of @command{grep} to be an option,
|
|
Packit |
709fb3 |
even if it appears to be one.
|
|
Packit |
709fb3 |
A shell can put this variable in the environment for each command it runs,
|
|
Packit |
709fb3 |
specifying which operands are the results of file name wildcard expansion
|
|
Packit |
709fb3 |
and therefore should not be treated as options.
|
|
Packit |
709fb3 |
This behavior is available only with the GNU C library,
|
|
Packit |
709fb3 |
and only when @env{POSIXLY_CORRECT} is not set.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Exit Status
|
|
Packit |
709fb3 |
@section Exit Status
|
|
Packit |
709fb3 |
@cindex exit status
|
|
Packit |
709fb3 |
@cindex return status
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Normally the exit status is 0 if a line is selected, 1 if no lines
|
|
Packit |
709fb3 |
were selected, and 2 if an error occurred. However, if the
|
|
Packit |
709fb3 |
@option{-q} or @option{--quiet} or @option{--silent} option is used
|
|
Packit |
709fb3 |
and a line is selected, the exit status is 0 even if an error
|
|
Packit |
709fb3 |
occurred. Other @command{grep} implementations may exit with status
|
|
Packit |
709fb3 |
greater than 2 on error.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node grep Programs
|
|
Packit |
709fb3 |
@section @command{grep} Programs
|
|
Packit |
709fb3 |
@cindex @command{grep} programs
|
|
Packit |
709fb3 |
@cindex variants of @command{grep}
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@command{grep} searches the named input files
|
|
Packit |
709fb3 |
for lines containing a match to the given pattern.
|
|
Packit |
709fb3 |
By default, @command{grep} prints the matching lines.
|
|
Packit |
709fb3 |
A file named @file{-} stands for standard input.
|
|
Packit |
709fb3 |
If no input is specified, @command{grep} searches the working
|
|
Packit |
709fb3 |
directory @file{.} if given a command-line option specifying
|
|
Packit |
709fb3 |
recursion; otherwise, @command{grep} searches standard input.
|
|
Packit |
709fb3 |
There are four major variants of @command{grep},
|
|
Packit |
709fb3 |
controlled by the following options.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @option
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -G
|
|
Packit |
709fb3 |
@itemx --basic-regexp
|
|
Packit |
709fb3 |
@opindex -G
|
|
Packit |
709fb3 |
@opindex --basic-regexp
|
|
Packit |
709fb3 |
@cindex matching basic regular expressions
|
|
Packit |
709fb3 |
Interpret the pattern as a basic regular expression (BRE).
|
|
Packit |
709fb3 |
This is the default.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -E
|
|
Packit |
709fb3 |
@itemx --extended-regexp
|
|
Packit |
709fb3 |
@opindex -E
|
|
Packit |
709fb3 |
@opindex --extended-regexp
|
|
Packit |
709fb3 |
@cindex matching extended regular expressions
|
|
Packit |
709fb3 |
Interpret the pattern as an extended regular expression (ERE).
|
|
Packit |
709fb3 |
(@option{-E} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -F
|
|
Packit |
709fb3 |
@itemx --fixed-strings
|
|
Packit |
709fb3 |
@opindex -F
|
|
Packit |
709fb3 |
@opindex --fixed-strings
|
|
Packit |
709fb3 |
@cindex matching fixed strings
|
|
Packit |
709fb3 |
Interpret the pattern as a list of fixed strings (instead of regular
|
|
Packit |
709fb3 |
expressions), separated by newlines, any of which is to be matched.
|
|
Packit |
709fb3 |
(@option{-F} is specified by POSIX.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -P
|
|
Packit |
709fb3 |
@itemx --perl-regexp
|
|
Packit |
709fb3 |
@opindex -P
|
|
Packit |
709fb3 |
@opindex --perl-regexp
|
|
Packit |
709fb3 |
@cindex matching Perl-compatible regular expressions
|
|
Packit |
709fb3 |
Interpret the pattern as a Perl-compatible regular expression (PCRE).
|
|
Packit |
709fb3 |
This is experimental, particularly when combined with
|
|
Packit |
709fb3 |
the @option{-z} (@option{--null-data}) option, and
|
|
Packit |
709fb3 |
@samp{grep@ -P} may warn of unimplemented features.
|
|
Packit |
709fb3 |
@xref{Other Options}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
In addition,
|
|
Packit |
709fb3 |
two variant programs @command{egrep} and @command{fgrep} are available.
|
|
Packit |
709fb3 |
@command{egrep} is the same as @samp{grep@ -E}.
|
|
Packit |
709fb3 |
@command{fgrep} is the same as @samp{grep@ -F}.
|
|
Packit |
709fb3 |
Direct invocation as either
|
|
Packit |
709fb3 |
@command{egrep} or @command{fgrep} is deprecated,
|
|
Packit |
709fb3 |
but is provided to allow historical applications
|
|
Packit |
709fb3 |
that rely on them to run unmodified.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Regular Expressions
|
|
Packit |
709fb3 |
@chapter Regular Expressions
|
|
Packit |
709fb3 |
@cindex regular expressions
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
A @dfn{regular expression} is a pattern that describes a set of strings.
|
|
Packit |
709fb3 |
Regular expressions are constructed analogously to arithmetic expressions,
|
|
Packit |
709fb3 |
by using various operators to combine smaller expressions.
|
|
Packit |
709fb3 |
@command{grep} understands
|
|
Packit |
709fb3 |
three different versions of regular expression syntax:
|
|
Packit |
709fb3 |
basic (BRE), extended (ERE), and Perl-compatible (PCRE).
|
|
Packit |
709fb3 |
In GNU @command{grep},
|
|
Packit |
709fb3 |
there is no difference in available functionality between the basic and
|
|
Packit |
709fb3 |
extended syntaxes.
|
|
Packit |
709fb3 |
In other implementations, basic regular expressions are less powerful.
|
|
Packit |
709fb3 |
The following description applies to extended regular expressions;
|
|
Packit |
709fb3 |
differences for basic regular expressions are summarized afterwards.
|
|
Packit |
709fb3 |
Perl-compatible regular expressions give additional functionality, and
|
|
Packit |
709fb3 |
are documented in the @i{pcresyntax}(3) and @i{pcrepattern}(3) manual
|
|
Packit |
709fb3 |
pages, but work only if PCRE is available in the system.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@menu
|
|
Packit |
709fb3 |
* Fundamental Structure::
|
|
Packit |
709fb3 |
* Character Classes and Bracket Expressions::
|
|
Packit |
709fb3 |
* The Backslash Character and Special Expressions::
|
|
Packit |
709fb3 |
* Anchoring::
|
|
Packit |
709fb3 |
* Back-references and Subexpressions::
|
|
Packit |
709fb3 |
* Basic vs Extended::
|
|
Packit |
709fb3 |
@end menu
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Fundamental Structure
|
|
Packit |
709fb3 |
@section Fundamental Structure
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The fundamental building blocks are the regular expressions that match
|
|
Packit |
709fb3 |
a single character.
|
|
Packit |
709fb3 |
Most characters, including all letters and digits,
|
|
Packit |
709fb3 |
are regular expressions that match themselves.
|
|
Packit |
709fb3 |
Any meta-character
|
|
Packit |
709fb3 |
with special meaning may be quoted by preceding it with a backslash.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
A regular expression may be followed by one of several
|
|
Packit |
709fb3 |
repetition operators:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @samp
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item .
|
|
Packit |
709fb3 |
@opindex .
|
|
Packit |
709fb3 |
@cindex dot
|
|
Packit |
709fb3 |
@cindex period
|
|
Packit |
709fb3 |
The period @samp{.} matches any single character.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item ?
|
|
Packit |
709fb3 |
@opindex ?
|
|
Packit |
709fb3 |
@cindex question mark
|
|
Packit |
709fb3 |
@cindex match expression at most once
|
|
Packit |
709fb3 |
The preceding item is optional and will be matched at most once.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item *
|
|
Packit |
709fb3 |
@opindex *
|
|
Packit |
709fb3 |
@cindex asterisk
|
|
Packit |
709fb3 |
@cindex match expression zero or more times
|
|
Packit |
709fb3 |
The preceding item will be matched zero or more times.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item +
|
|
Packit |
709fb3 |
@opindex +
|
|
Packit |
709fb3 |
@cindex plus sign
|
|
Packit |
709fb3 |
@cindex match expression one or more times
|
|
Packit |
709fb3 |
The preceding item will be matched one or more times.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item @{@var{n}@}
|
|
Packit |
709fb3 |
@opindex @{@var{n}@}
|
|
Packit |
709fb3 |
@cindex braces, one argument
|
|
Packit |
709fb3 |
@cindex match expression @var{n} times
|
|
Packit |
709fb3 |
The preceding item is matched exactly @var{n} times.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item @{@var{n},@}
|
|
Packit |
709fb3 |
@opindex @{@var{n},@}
|
|
Packit |
709fb3 |
@cindex braces, second argument omitted
|
|
Packit |
709fb3 |
@cindex match expression @var{n} or more times
|
|
Packit |
709fb3 |
The preceding item is matched @var{n} or more times.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item @{,@var{m}@}
|
|
Packit |
709fb3 |
@opindex @{,@var{m}@}
|
|
Packit |
709fb3 |
@cindex braces, first argument omitted
|
|
Packit |
709fb3 |
@cindex match expression at most @var{m} times
|
|
Packit |
709fb3 |
The preceding item is matched at most @var{m} times.
|
|
Packit |
709fb3 |
This is a GNU extension.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item @{@var{n},@var{m}@}
|
|
Packit |
709fb3 |
@opindex @{@var{n},@var{m}@}
|
|
Packit |
709fb3 |
@cindex braces, two arguments
|
|
Packit |
709fb3 |
@cindex match expression from @var{n} to @var{m} times
|
|
Packit |
709fb3 |
The preceding item is matched at least @var{n} times, but not more than
|
|
Packit |
709fb3 |
@var{m} times.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The empty regular expression matches the empty string.
|
|
Packit |
709fb3 |
Two regular expressions may be concatenated;
|
|
Packit |
709fb3 |
the resulting regular expression
|
|
Packit |
709fb3 |
matches any string formed by concatenating two substrings
|
|
Packit |
709fb3 |
that respectively match the concatenated expressions.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Two regular expressions may be joined by the infix operator @samp{|};
|
|
Packit |
709fb3 |
the resulting regular expression
|
|
Packit |
709fb3 |
matches any string matching either alternate expression.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Repetition takes precedence over concatenation,
|
|
Packit |
709fb3 |
which in turn takes precedence over alternation.
|
|
Packit |
709fb3 |
A whole expression may be enclosed in parentheses
|
|
Packit |
709fb3 |
to override these precedence rules and form a subexpression.
|
|
Packit |
709fb3 |
An unmatched @samp{)} matches just itself.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Character Classes and Bracket Expressions
|
|
Packit |
709fb3 |
@section Character Classes and Bracket Expressions
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex bracket expression
|
|
Packit |
709fb3 |
@cindex character class
|
|
Packit |
709fb3 |
A @dfn{bracket expression} is a list of characters enclosed by @samp{[} and
|
|
Packit |
709fb3 |
@samp{]}.
|
|
Packit |
709fb3 |
It matches any single character in that list;
|
|
Packit |
709fb3 |
if the first character of the list is the caret @samp{^},
|
|
Packit |
709fb3 |
then it matches any character @strong{not} in the list.
|
|
Packit |
709fb3 |
For example, the regular expression
|
|
Packit |
709fb3 |
@samp{[0123456789]} matches any single digit.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex range expression
|
|
Packit |
709fb3 |
Within a bracket expression, a @dfn{range expression} consists of two
|
|
Packit |
709fb3 |
characters separated by a hyphen.
|
|
Packit |
709fb3 |
It matches any single character that
|
|
Packit |
709fb3 |
sorts between the two characters, inclusive.
|
|
Packit |
709fb3 |
In the default C locale, the sorting sequence is the native character
|
|
Packit |
709fb3 |
order; for example, @samp{[a-d]} is equivalent to @samp{[abcd]}.
|
|
Packit |
709fb3 |
In other locales, the sorting sequence is not specified, and
|
|
Packit |
709fb3 |
@samp{[a-d]} might be equivalent to @samp{[abcd]} or to
|
|
Packit |
709fb3 |
@samp{[aBbCcDd]}, or it might fail to match any character, or the set of
|
|
Packit |
709fb3 |
characters that it matches might even be erratic.
|
|
Packit |
709fb3 |
To obtain the traditional interpretation
|
|
Packit |
709fb3 |
of bracket expressions, you can use the @samp{C} locale by setting the
|
|
Packit |
709fb3 |
@env{LC_ALL} environment variable to the value @samp{C}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Finally, certain named classes of characters are predefined within
|
|
Packit |
709fb3 |
bracket expressions, as follows.
|
|
Packit |
709fb3 |
Their interpretation depends on the @env{LC_CTYPE} locale;
|
|
Packit |
709fb3 |
for example, @samp{[[:alnum:]]} means the character class of numbers and letters
|
|
Packit |
709fb3 |
in the current locale.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex classes of characters
|
|
Packit |
709fb3 |
@cindex character classes
|
|
Packit |
709fb3 |
@table @samp
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:alnum:]
|
|
Packit |
709fb3 |
@opindex alnum @r{character class}
|
|
Packit |
709fb3 |
@cindex alphanumeric characters
|
|
Packit |
709fb3 |
Alphanumeric characters:
|
|
Packit |
709fb3 |
@samp{[:alpha:]} and @samp{[:digit:]}; in the @samp{C} locale and ASCII
|
|
Packit |
709fb3 |
character encoding, this is the same as @samp{[0-9A-Za-z]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:alpha:]
|
|
Packit |
709fb3 |
@opindex alpha @r{character class}
|
|
Packit |
709fb3 |
@cindex alphabetic characters
|
|
Packit |
709fb3 |
Alphabetic characters:
|
|
Packit |
709fb3 |
@samp{[:lower:]} and @samp{[:upper:]}; in the @samp{C} locale and ASCII
|
|
Packit |
709fb3 |
character encoding, this is the same as @samp{[A-Za-z]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:blank:]
|
|
Packit |
709fb3 |
@opindex blank @r{character class}
|
|
Packit |
709fb3 |
@cindex blank characters
|
|
Packit |
709fb3 |
Blank characters:
|
|
Packit |
709fb3 |
space and tab.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:cntrl:]
|
|
Packit |
709fb3 |
@opindex cntrl @r{character class}
|
|
Packit |
709fb3 |
@cindex control characters
|
|
Packit |
709fb3 |
Control characters.
|
|
Packit |
709fb3 |
In ASCII, these characters have octal codes 000
|
|
Packit |
709fb3 |
through 037, and 177 (DEL).
|
|
Packit |
709fb3 |
In other character sets, these are
|
|
Packit |
709fb3 |
the equivalent characters, if any.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:digit:]
|
|
Packit |
709fb3 |
@opindex digit @r{character class}
|
|
Packit |
709fb3 |
@cindex digit characters
|
|
Packit |
709fb3 |
@cindex numeric characters
|
|
Packit |
709fb3 |
Digits: @code{0 1 2 3 4 5 6 7 8 9}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:graph:]
|
|
Packit |
709fb3 |
@opindex graph @r{character class}
|
|
Packit |
709fb3 |
@cindex graphic characters
|
|
Packit |
709fb3 |
Graphical characters:
|
|
Packit |
709fb3 |
@samp{[:alnum:]} and @samp{[:punct:]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:lower:]
|
|
Packit |
709fb3 |
@opindex lower @r{character class}
|
|
Packit |
709fb3 |
@cindex lower-case letters
|
|
Packit |
709fb3 |
Lower-case letters; in the @samp{C} locale and ASCII character
|
|
Packit |
709fb3 |
encoding, this is
|
|
Packit |
709fb3 |
@code{a b c d e f g h i j k l m n o p q r s t u v w x y z}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:print:]
|
|
Packit |
709fb3 |
@opindex print @r{character class}
|
|
Packit |
709fb3 |
@cindex printable characters
|
|
Packit |
709fb3 |
Printable characters:
|
|
Packit |
709fb3 |
@samp{[:alnum:]}, @samp{[:punct:]}, and space.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:punct:]
|
|
Packit |
709fb3 |
@opindex punct @r{character class}
|
|
Packit |
709fb3 |
@cindex punctuation characters
|
|
Packit |
709fb3 |
Punctuation characters; in the @samp{C} locale and ASCII character
|
|
Packit |
709fb3 |
encoding, this is
|
|
Packit |
709fb3 |
@code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:space:]
|
|
Packit |
709fb3 |
@opindex space @r{character class}
|
|
Packit |
709fb3 |
@cindex space characters
|
|
Packit |
709fb3 |
@cindex whitespace characters
|
|
Packit |
709fb3 |
Space characters: in the @samp{C} locale, this is
|
|
Packit |
709fb3 |
tab, newline, vertical tab, form feed, carriage return, and space.
|
|
Packit |
709fb3 |
@xref{Usage}, for more discussion of matching newlines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:upper:]
|
|
Packit |
709fb3 |
@opindex upper @r{character class}
|
|
Packit |
709fb3 |
@cindex upper-case letters
|
|
Packit |
709fb3 |
Upper-case letters: in the @samp{C} locale and ASCII character
|
|
Packit |
709fb3 |
encoding, this is
|
|
Packit |
709fb3 |
@code{A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:xdigit:]
|
|
Packit |
709fb3 |
@opindex xdigit @r{character class}
|
|
Packit |
709fb3 |
@cindex xdigit class
|
|
Packit |
709fb3 |
@cindex hexadecimal digits
|
|
Packit |
709fb3 |
Hexadecimal digits:
|
|
Packit |
709fb3 |
@code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
Note that the brackets in these class names are
|
|
Packit |
709fb3 |
part of the symbolic names, and must be included in addition to
|
|
Packit |
709fb3 |
the brackets delimiting the bracket expression.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@anchor{invalid-bracket-expr}
|
|
Packit |
709fb3 |
If you mistakenly omit the outer brackets, and search for say, @samp{[:upper:]},
|
|
Packit |
709fb3 |
GNU @command{grep} prints a diagnostic and exits with status 2, on
|
|
Packit |
709fb3 |
the assumption that you did not intend to search for the nominally
|
|
Packit |
709fb3 |
equivalent regular expression: @samp{[:epru]}.
|
|
Packit |
709fb3 |
Set the @env{POSIXLY_CORRECT} environment variable to disable this feature.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Most meta-characters lose their special meaning inside bracket expressions.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @samp
|
|
Packit |
709fb3 |
@item ]
|
|
Packit |
709fb3 |
ends the bracket expression if it's not the first list item.
|
|
Packit |
709fb3 |
So, if you want to make the @samp{]} character a list item,
|
|
Packit |
709fb3 |
you must put it first.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [.
|
|
Packit |
709fb3 |
represents the open collating symbol.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item .]
|
|
Packit |
709fb3 |
represents the close collating symbol.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [=
|
|
Packit |
709fb3 |
represents the open equivalence class.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item =]
|
|
Packit |
709fb3 |
represents the close equivalence class.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item [:
|
|
Packit |
709fb3 |
represents the open character class symbol, and should be followed by a
|
|
Packit |
709fb3 |
valid character class name.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item :]
|
|
Packit |
709fb3 |
represents the close character class symbol.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item -
|
|
Packit |
709fb3 |
represents the range if it's not first or last in a list or the ending point
|
|
Packit |
709fb3 |
of a range.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item ^
|
|
Packit |
709fb3 |
represents the characters not in the list.
|
|
Packit |
709fb3 |
If you want to make the @samp{^}
|
|
Packit |
709fb3 |
character a list item, place it anywhere but first.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node The Backslash Character and Special Expressions
|
|
Packit |
709fb3 |
@section The Backslash Character and Special Expressions
|
|
Packit |
709fb3 |
@cindex backslash
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The @samp{\} character,
|
|
Packit |
709fb3 |
when followed by certain ordinary characters,
|
|
Packit |
709fb3 |
takes a special meaning:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@table @samp
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \b
|
|
Packit |
709fb3 |
Match the empty string at the edge of a word.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \B
|
|
Packit |
709fb3 |
Match the empty string provided it's not at the edge of a word.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \<
|
|
Packit |
709fb3 |
Match the empty string at the beginning of word.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \>
|
|
Packit |
709fb3 |
Match the empty string at the end of word.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \w
|
|
Packit |
709fb3 |
Match word constituent, it is a synonym for @samp{[_[:alnum:]]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \W
|
|
Packit |
709fb3 |
Match non-word constituent, it is a synonym for @samp{[^_[:alnum:]]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \s
|
|
Packit |
709fb3 |
Match whitespace, it is a synonym for @samp{[[:space:]]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item \S
|
|
Packit |
709fb3 |
Match non-whitespace, it is a synonym for @samp{[^[:space:]]}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end table
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
For example, @samp{\brat\b} matches the separate word @samp{rat},
|
|
Packit |
709fb3 |
@samp{\Brat\B} matches @samp{crate} but not @samp{furry rat}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Anchoring
|
|
Packit |
709fb3 |
@section Anchoring
|
|
Packit |
709fb3 |
@cindex anchoring
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The caret @samp{^} and the dollar sign @samp{$} are meta-characters that
|
|
Packit |
709fb3 |
respectively match the empty string at the beginning and end of a line.
|
|
Packit |
709fb3 |
They are termed @dfn{anchors}, since they force the match to be ``anchored''
|
|
Packit |
709fb3 |
to beginning or end of a line, respectively.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Back-references and Subexpressions
|
|
Packit |
709fb3 |
@section Back-references and Subexpressions
|
|
Packit |
709fb3 |
@cindex subexpression
|
|
Packit |
709fb3 |
@cindex back-reference
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The back-reference @samp{\@var{n}}, where @var{n} is a single digit, matches
|
|
Packit |
709fb3 |
the substring previously matched by the @var{n}th parenthesized subexpression
|
|
Packit |
709fb3 |
of the regular expression.
|
|
Packit |
709fb3 |
For example, @samp{(a)\1} matches @samp{aa}.
|
|
Packit |
709fb3 |
When used with alternation, if the group does not participate in the match then
|
|
Packit |
709fb3 |
the back-reference makes the whole match fail.
|
|
Packit |
709fb3 |
For example, @samp{a(.)|b\1}
|
|
Packit |
709fb3 |
will not match @samp{ba}.
|
|
Packit |
709fb3 |
When multiple regular expressions are given with
|
|
Packit |
709fb3 |
@option{-e} or from a file (@samp{-f @var{file}}),
|
|
Packit |
709fb3 |
back-references are local to each expression.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Basic vs Extended
|
|
Packit |
709fb3 |
@section Basic vs Extended Regular Expressions
|
|
Packit |
709fb3 |
@cindex basic regular expressions
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
In basic regular expressions the meta-characters @samp{?}, @samp{+},
|
|
Packit |
709fb3 |
@samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning;
|
|
Packit |
709fb3 |
instead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{},
|
|
Packit |
709fb3 |
@samp{\|}, @samp{\(}, and @samp{\)}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex interval specifications
|
|
Packit |
709fb3 |
Traditional @command{egrep} did not support the @samp{@{} meta-character,
|
|
Packit |
709fb3 |
and some @command{egrep} implementations support @samp{\@{} instead, so
|
|
Packit |
709fb3 |
portable scripts should avoid @samp{@{} in @samp{grep@ -E} patterns and
|
|
Packit |
709fb3 |
should use @samp{[@{]} to match a literal @samp{@{}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
GNU @command{grep@ -E} attempts to support traditional usage by
|
|
Packit |
709fb3 |
assuming that @samp{@{} is not special if it would be the start of an
|
|
Packit |
709fb3 |
invalid interval specification.
|
|
Packit |
709fb3 |
For example, the command
|
|
Packit |
709fb3 |
@samp{grep@ -E@ '@{1'} searches for the two-character string @samp{@{1}
|
|
Packit |
709fb3 |
instead of reporting a syntax error in the regular expression.
|
|
Packit |
709fb3 |
POSIX allows this behavior as an extension, but portable scripts
|
|
Packit |
709fb3 |
should avoid it.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Usage
|
|
Packit |
709fb3 |
@chapter Usage
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex usage, examples
|
|
Packit |
709fb3 |
Here is an example command that invokes GNU @command{grep}:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -i 'hello.*world' menu.h main.c
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
This lists all lines in the files @file{menu.h} and @file{main.c} that
|
|
Packit |
709fb3 |
contain the string @samp{hello} followed by the string @samp{world};
|
|
Packit |
709fb3 |
this is because @samp{.*} matches zero or more characters within a line.
|
|
Packit |
709fb3 |
@xref{Regular Expressions}.
|
|
Packit |
709fb3 |
The @option{-i} option causes @command{grep}
|
|
Packit |
709fb3 |
to ignore case, causing it to match the line @samp{Hello, world!}, which
|
|
Packit |
709fb3 |
it would not otherwise match.
|
|
Packit |
709fb3 |
@xref{Invoking}, for more details about
|
|
Packit |
709fb3 |
how to invoke @command{grep}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex using @command{grep}, Q&A
|
|
Packit |
709fb3 |
@cindex FAQ about @command{grep} usage
|
|
Packit |
709fb3 |
Here are some common questions and answers about @command{grep} usage.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@enumerate
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
How can I list just the names of matching files?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -l 'main' *.c
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
lists the names of all C files in the current directory whose contents
|
|
Packit |
709fb3 |
mention @samp{main}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
How do I search directories recursively?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -r 'hello' /home/gigi
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
searches for @samp{hello} in all files
|
|
Packit |
709fb3 |
under the @file{/home/gigi} directory.
|
|
Packit |
709fb3 |
For more control over which files are searched,
|
|
Packit |
709fb3 |
use @command{find}, @command{grep}, and @command{xargs}.
|
|
Packit |
709fb3 |
For example, the following command searches only C files:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
find /home/gigi -name '*.c' -print0 | xargs -0r grep -H 'hello'
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This differs from the command:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -H 'hello' *.c
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
which merely looks for @samp{hello} in all files in the current
|
|
Packit |
709fb3 |
directory whose names end in @samp{.c}.
|
|
Packit |
709fb3 |
The @samp{find ...} command line above is more similar to the command:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -rH --include='*.c' 'hello' /home/gigi
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
What if a pattern has a leading @samp{-}?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -e '--cut here--' *
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
searches for all lines matching @samp{--cut here--}.
|
|
Packit |
709fb3 |
Without @option{-e},
|
|
Packit |
709fb3 |
@command{grep} would attempt to parse @samp{--cut here--} as a list of
|
|
Packit |
709fb3 |
options.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Suppose I want to search for a whole word, not a part of a word?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -w 'hello' *
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
searches only for instances of @samp{hello} that are entire words;
|
|
Packit |
709fb3 |
it does not match @samp{Othello}.
|
|
Packit |
709fb3 |
For more control, use @samp{\<} and
|
|
Packit |
709fb3 |
@samp{\>} to match the start and end of words.
|
|
Packit |
709fb3 |
For example:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep 'hello\>' *
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
searches only for words ending in @samp{hello}, so it matches the word
|
|
Packit |
709fb3 |
@samp{Othello}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
How do I output context around the matching lines?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -C 2 'hello' *
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
prints two lines of context around each matching line.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
How do I force @command{grep} to print the name of the file?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Append @file{/dev/null}:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep 'eli' /etc/passwd /dev/null
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
gets you:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
/etc/passwd:eli:x:2098:1000:Eli Smith:/home/eli:/bin/bash
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Alternatively, use @option{-H}, which is a GNU extension:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -H 'eli' /etc/passwd
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Why do people use strange regular expressions on @command{ps} output?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
ps -ef | grep '[c]ron'
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
If the pattern had been written without the square brackets, it would
|
|
Packit |
709fb3 |
have matched not only the @command{ps} output line for @command{cron},
|
|
Packit |
709fb3 |
but also the @command{ps} output line for @command{grep}.
|
|
Packit |
709fb3 |
Note that on some platforms,
|
|
Packit |
709fb3 |
@command{ps} limits the output to the width of the screen;
|
|
Packit |
709fb3 |
@command{grep} does not have any limit on the length of a line
|
|
Packit |
709fb3 |
except the available memory.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Why does @command{grep} report ``Binary file matches''?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
If @command{grep} listed all matching ``lines'' from a binary file, it
|
|
Packit |
709fb3 |
would probably generate output that is not useful, and it might even
|
|
Packit |
709fb3 |
muck up your display.
|
|
Packit |
709fb3 |
So GNU @command{grep} suppresses output from
|
|
Packit |
709fb3 |
files that appear to be binary files.
|
|
Packit |
709fb3 |
To force GNU @command{grep}
|
|
Packit |
709fb3 |
to output lines even from files that appear to be binary, use the
|
|
Packit |
709fb3 |
@option{-a} or @samp{--binary-files=text} option.
|
|
Packit |
709fb3 |
To eliminate the
|
|
Packit |
709fb3 |
``Binary file matches'' messages, use the @option{-I} or
|
|
Packit |
709fb3 |
@samp{--binary-files=without-match} option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Why doesn't @samp{grep -lv} print non-matching file names?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@samp{grep -lv} lists the names of all files containing one or more
|
|
Packit |
709fb3 |
lines that do not match.
|
|
Packit |
709fb3 |
To list the names of all files that contain no
|
|
Packit |
709fb3 |
matching lines, use the @option{-L} or @option{--files-without-match}
|
|
Packit |
709fb3 |
option.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
I can do ``OR'' with @samp{|}, but what about ``AND''?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep 'paul' /etc/motd | grep 'franc,ois'
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@noindent
|
|
Packit |
709fb3 |
finds all lines that contain both @samp{paul} and @samp{franc,ois}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Why does the empty pattern match every input line?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The @command{grep} command searches for lines that contain strings
|
|
Packit |
709fb3 |
that match a pattern. Every line contains the empty string, so an
|
|
Packit |
709fb3 |
empty pattern causes @command{grep} to find a match on each line. It
|
|
Packit |
709fb3 |
is not the only such pattern: @samp{^}, @samp{$}, @samp{.*}, and many
|
|
Packit |
709fb3 |
other patterns cause @command{grep} to match every line.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
To match empty lines, use the pattern @samp{^$}. To match blank
|
|
Packit |
709fb3 |
lines, use the pattern @samp{^[[:blank:]]*$}. To match no lines at
|
|
Packit |
709fb3 |
all, use the command @samp{grep -f /dev/null}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
How can I search in both standard input and in files?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Use the special file name @samp{-}:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
cat /etc/passwd | grep 'alain' - /etc/motd
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
@cindex palindromes
|
|
Packit |
709fb3 |
How to express palindromes in a regular expression?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
It can be done by using back-references;
|
|
Packit |
709fb3 |
for example,
|
|
Packit |
709fb3 |
a palindrome of 4 characters can be written with a BRE:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
grep -w -e '\(.\)\(.\).\2\1' file
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
It matches the word ``radar'' or ``civic.''
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Guglielmo Bondioni proposed a single RE
|
|
Packit |
709fb3 |
that finds all palindromes up to 19 characters long
|
|
Packit |
709fb3 |
using @w{9 subexpressions} and @w{9 back-references}:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@smallexample
|
|
Packit |
709fb3 |
grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file
|
|
Packit |
709fb3 |
@end smallexample
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Note this is done by using GNU ERE extensions;
|
|
Packit |
709fb3 |
it might not be portable to other implementations of @command{grep}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Why is this back-reference failing?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
echo 'ba' | grep -E '(a)\1|b\1'
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This gives no output, because the first alternate @samp{(a)\1} does not match,
|
|
Packit |
709fb3 |
as there is no @samp{aa} in the input, so the @samp{\1} in the second alternate
|
|
Packit |
709fb3 |
has nothing to refer back to, meaning it will never match anything.
|
|
Packit |
709fb3 |
(The second alternate in this example can only match
|
|
Packit |
709fb3 |
if the first alternate has matched---making the second one superfluous.)
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
How can I match across lines?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Standard grep cannot do this, as it is fundamentally line-based.
|
|
Packit |
709fb3 |
Therefore, merely using the @code{[:space:]} character class does not
|
|
Packit |
709fb3 |
match newlines in the way you might expect.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
With the GNU @command{grep} option @option{-z} (@option{--null-data}), each
|
|
Packit |
709fb3 |
input and output ``line'' is null-terminated; @pxref{Other Options}. Thus,
|
|
Packit |
709fb3 |
you can match newlines in the input, but typically if there is a match
|
|
Packit |
709fb3 |
the entire input is output, so this usage is often combined with
|
|
Packit |
709fb3 |
output-suppressing options like @option{-q}, e.g.:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
printf 'foo\nbar\n' | grep -z -q 'foo[[:space:]]\+bar'
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
If this does not suffice, you can transform the input
|
|
Packit |
709fb3 |
before giving it to @command{grep}, or turn to @command{awk},
|
|
Packit |
709fb3 |
@command{sed}, @command{perl}, or many other utilities that are
|
|
Packit |
709fb3 |
designed to operate across lines.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
What do @command{grep}, @command{fgrep}, and @command{egrep} stand for?
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The name @command{grep} comes from the way line editing was done on Unix.
|
|
Packit |
709fb3 |
For example,
|
|
Packit |
709fb3 |
@command{ed} uses the following syntax
|
|
Packit |
709fb3 |
to print a list of matching lines on the screen:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@example
|
|
Packit |
709fb3 |
global/regular expression/print
|
|
Packit |
709fb3 |
g/re/p
|
|
Packit |
709fb3 |
@end example
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@command{fgrep} stands for Fixed @command{grep};
|
|
Packit |
709fb3 |
@command{egrep} stands for Extended @command{grep}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@end enumerate
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Performance
|
|
Packit |
709fb3 |
@chapter Performance
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex performance
|
|
Packit |
709fb3 |
Typically @command{grep} is an efficient way to search text. However,
|
|
Packit |
709fb3 |
it can be quite slow in some cases, and it can search large files
|
|
Packit |
709fb3 |
where even minor performance tweaking can help significantly.
|
|
Packit |
709fb3 |
Although the algorithm used by @command{grep} is an implementation
|
|
Packit |
709fb3 |
detail that can change from release to release, understanding its
|
|
Packit |
709fb3 |
basic strengths and weaknesses can help you improve its performance.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The @command{grep} command operates partly via a set of automata that
|
|
Packit |
709fb3 |
are designed for efficiency, and partly via a slower matcher that
|
|
Packit |
709fb3 |
takes over when the fast matchers run into unusual features like
|
|
Packit |
709fb3 |
back-references. When feasible, the Boyer--Moore fast string
|
|
Packit |
709fb3 |
searching algorithm is used to match a single fixed pattern, and the
|
|
Packit |
709fb3 |
Aho--Corasick algorithm is used to match multiple fixed patterns.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex locales
|
|
Packit |
709fb3 |
Generally speaking @command{grep} operates more efficiently in
|
|
Packit |
709fb3 |
single-byte locales, since it can avoid the special processing needed
|
|
Packit |
709fb3 |
for multi-byte characters. If your pattern will work just as well
|
|
Packit |
709fb3 |
that way, setting @env{LC_ALL} to a single-byte locale can help
|
|
Packit |
709fb3 |
performance considerably. Setting @samp{LC_ALL='C'} can be
|
|
Packit |
709fb3 |
particularly efficient, as @command{grep} is tuned for that locale.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex case insensitive search
|
|
Packit |
709fb3 |
Outside the @samp{C} locale, case-insensitive search, and search for
|
|
Packit |
709fb3 |
bracket expressions like @samp{[a-z]} and @samp{[[=a=]b]}, can be
|
|
Packit |
709fb3 |
surprisingly inefficient due to difficulties in fast portable access to
|
|
Packit |
709fb3 |
concepts like multi-character collating elements.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex back-references
|
|
Packit |
709fb3 |
A back-reference such as @samp{\1} can hurt performance significantly
|
|
Packit |
709fb3 |
in some cases, since back-references cannot in general be implemented
|
|
Packit |
709fb3 |
via a finite state automaton, and instead trigger a backtracking
|
|
Packit |
709fb3 |
algorithm that can be quite inefficient. For example, although the
|
|
Packit |
709fb3 |
pattern @samp{^(.*)\1@{14@}(.*)\2@{13@}$} matches only lines whose
|
|
Packit |
709fb3 |
lengths can be written as a sum @math{15x + 14y} for nonnegative
|
|
Packit |
709fb3 |
integers @math{x} and @math{y}, the pattern matcher does not perform
|
|
Packit |
709fb3 |
linear Diophantine analysis and instead backtracks through all
|
|
Packit |
709fb3 |
possible matching strings, using an algorithm that is exponential in
|
|
Packit |
709fb3 |
the worst case.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex holes in files
|
|
Packit |
709fb3 |
On some operating systems that support files with holes---large
|
|
Packit |
709fb3 |
regions of zeros that are not physically present on secondary
|
|
Packit |
709fb3 |
storage---@command{grep} can skip over the holes efficiently without
|
|
Packit |
709fb3 |
needing to read the zeros. This optimization is not available if the
|
|
Packit |
709fb3 |
@option{-a} (@option{--text}) option is used (@pxref{File and
|
|
Packit |
709fb3 |
Directory Selection}), unless the @option{-z} (@option{--null-data})
|
|
Packit |
709fb3 |
option is also used (@pxref{Other Options}).
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
For more about the algorithms used by @command{grep} and about
|
|
Packit |
709fb3 |
related string matching algorithms, see:
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@frenchspacing on
|
|
Packit |
709fb3 |
@itemize @bullet
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Aho AV. Algorithms for finding patterns in strings.
|
|
Packit |
709fb3 |
In: van Leeuwen J. @emph{Handbook of Theoretical Computer Science}, vol. A.
|
|
Packit |
709fb3 |
New York: Elsevier; 1990. p. 255--300.
|
|
Packit |
709fb3 |
This surveys classic string matching algorithms, some of which are
|
|
Packit |
709fb3 |
used by @command{grep}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Aho AV, Corasick MJ. Efficient string matching: an aid to bibliographic search.
|
|
Packit |
709fb3 |
@emph{CACM}. 1975;18(6):333--40.
|
|
Packit |
709fb3 |
@url{http://dx.doi.org/10.1145/360825.360855}.
|
|
Packit |
709fb3 |
This introduces the Aho--Corasick algorithm.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Boyer RS, Moore JS. A fast string searching algorithm.
|
|
Packit |
709fb3 |
@emph{CACM}. 1977;20(10):762--72.
|
|
Packit |
709fb3 |
@url{http://dx.doi.org/10.1145/359842.359859}.
|
|
Packit |
709fb3 |
This introduces the Boyer--Moore algorithm.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@item
|
|
Packit |
709fb3 |
Faro S, Lecroq T. The exact online string matching problem: a review
|
|
Packit |
709fb3 |
of the most recent results.
|
|
Packit |
709fb3 |
@emph{ACM Comput Surv}. 2013;45(2):13.
|
|
Packit |
709fb3 |
@url{http://dx.doi.org/10.1145/2431211.2431212}.
|
|
Packit |
709fb3 |
This surveys string matching algorithms that might help improve the
|
|
Packit |
709fb3 |
performance of @command{grep} in the future.
|
|
Packit |
709fb3 |
@end itemize
|
|
Packit |
709fb3 |
@frenchspacing off
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Reporting Bugs
|
|
Packit |
709fb3 |
@chapter Reporting bugs
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@cindex bugs, reporting
|
|
Packit |
709fb3 |
Bug reports can be found at the
|
|
Packit |
709fb3 |
@url{http://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep,
|
|
Packit |
709fb3 |
GNU bug report logs for @command{grep}}.
|
|
Packit |
709fb3 |
If you find a bug not listed there, please email it to
|
|
Packit |
709fb3 |
@email{bug-grep@@gnu.org} to create a new bug report.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@section Known Bugs
|
|
Packit |
709fb3 |
@cindex Bugs, known
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Large repetition counts in the @samp{@{n,m@}} construct may cause
|
|
Packit |
709fb3 |
@command{grep} to use lots of memory.
|
|
Packit |
709fb3 |
In addition, certain other
|
|
Packit |
709fb3 |
obscure regular expressions require exponential time and
|
|
Packit |
709fb3 |
space, and may cause @command{grep} to run out of memory.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Back-references are very slow, and may require exponential time.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Copying
|
|
Packit |
709fb3 |
@chapter Copying
|
|
Packit |
709fb3 |
@cindex copying
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
GNU @command{grep} is licensed under the GNU GPL, which makes it @dfn{free
|
|
Packit |
709fb3 |
software}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
The ``free'' in ``free software'' refers to liberty, not price. As
|
|
Packit |
709fb3 |
some GNU project advocates like to point out, think of ``free speech''
|
|
Packit |
709fb3 |
rather than ``free beer''. In short, you have the right (freedom) to
|
|
Packit |
709fb3 |
run and change @command{grep} and distribute it to other people, and---if you
|
|
Packit |
709fb3 |
want---charge money for doing either. The important restriction is
|
|
Packit |
709fb3 |
that you have to grant your recipients the same rights and impose the
|
|
Packit |
709fb3 |
same restrictions.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This general method of licensing software is sometimes called
|
|
Packit |
709fb3 |
@dfn{open source}. The GNU project prefers the term ``free software''
|
|
Packit |
709fb3 |
for reasons outlined at
|
|
Packit |
709fb3 |
@url{http://www.gnu.org/philosophy/open-source-misses-the-point.html}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This manual is free documentation in the same sense. The
|
|
Packit |
709fb3 |
documentation license is included below. The license for the program
|
|
Packit |
709fb3 |
is available with the source code, or at
|
|
Packit |
709fb3 |
@url{http://www.gnu.org/licenses/gpl.html}.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@menu
|
|
Packit |
709fb3 |
* GNU Free Documentation License::
|
|
Packit |
709fb3 |
@end menu
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node GNU Free Documentation License
|
|
Packit |
709fb3 |
@section GNU Free Documentation License
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@include fdl.texi
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@node Index
|
|
Packit |
709fb3 |
@unnumbered Index
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@printindex cp
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
@bye
|