195b7b453SJohn Marino\input texinfo @c -*-texinfo-*- 295b7b453SJohn Marino@c %**start of header 395b7b453SJohn Marino@setfilename grep.info 495b7b453SJohn Marino@include version.texi 595b7b453SJohn Marino@settitle GNU Grep @value{VERSION} 695b7b453SJohn Marino 795b7b453SJohn Marino@c Combine indices. 895b7b453SJohn Marino@syncodeindex ky cp 995b7b453SJohn Marino@syncodeindex pg cp 1095b7b453SJohn Marino@syncodeindex tp cp 1195b7b453SJohn Marino@defcodeindex op 1295b7b453SJohn Marino@syncodeindex op cp 1395b7b453SJohn Marino@syncodeindex vr cp 1495b7b453SJohn Marino@c %**end of header 1595b7b453SJohn Marino 16680a9cb8SJohn Marino@documentencoding UTF-8 17*09d4459fSDaniel Fojt@c These two require Texinfo 5.0 or later, so use the older 18*09d4459fSDaniel Fojt@c equivalent @set variables supported in 4.11 and later. 19*09d4459fSDaniel Fojt@ignore 20*09d4459fSDaniel Fojt@codequotebacktick on 21*09d4459fSDaniel Fojt@codequoteundirected on 22*09d4459fSDaniel Fojt@end ignore 23*09d4459fSDaniel Fojt@set txicodequoteundirected 24*09d4459fSDaniel Fojt@set txicodequotebacktick 25*09d4459fSDaniel Fojt@iftex 26*09d4459fSDaniel Fojt@c TeX sometimes fails to hyphenate, so help it here. 27*09d4459fSDaniel Fojt@hyphenation{spec-i-fied} 28*09d4459fSDaniel Fojt@end iftex 29680a9cb8SJohn Marino 3095b7b453SJohn Marino@copying 3195b7b453SJohn MarinoThis manual is for @command{grep}, a pattern matching engine. 3295b7b453SJohn Marino 33*09d4459fSDaniel FojtCopyright @copyright{} 1999--2002, 2005, 2008--2020 Free Software Foundation, 34cf28ed85SJohn MarinoInc. 3595b7b453SJohn Marino 3695b7b453SJohn Marino@quotation 3795b7b453SJohn MarinoPermission is granted to copy, distribute and/or modify this document 3895b7b453SJohn Marinounder the terms of the GNU Free Documentation License, Version 1.3 or 3995b7b453SJohn Marinoany later version published by the Free Software Foundation; with no 4095b7b453SJohn MarinoInvariant Sections, with no Front-Cover Texts, and with no Back-Cover 4195b7b453SJohn MarinoTexts. A copy of the license is included in the section entitled 4295b7b453SJohn Marino``GNU Free Documentation License''. 4395b7b453SJohn Marino@end quotation 4495b7b453SJohn Marino@end copying 4595b7b453SJohn Marino 4695b7b453SJohn Marino@dircategory Text creation and manipulation 4795b7b453SJohn Marino@direntry 48*09d4459fSDaniel Fojt* grep: (grep). Print lines that match patterns. 4995b7b453SJohn Marino@end direntry 5095b7b453SJohn Marino 5195b7b453SJohn Marino@titlepage 52*09d4459fSDaniel Fojt@title GNU Grep: Print lines that match patterns 5395b7b453SJohn Marino@subtitle version @value{VERSION}, @value{UPDATED} 5495b7b453SJohn Marino@author Alain Magloire et al. 5595b7b453SJohn Marino@page 5695b7b453SJohn Marino@vskip 0pt plus 1filll 5795b7b453SJohn Marino@insertcopying 5895b7b453SJohn Marino@end titlepage 5995b7b453SJohn Marino 6095b7b453SJohn Marino@contents 6195b7b453SJohn Marino 6295b7b453SJohn Marino 6395b7b453SJohn Marino@ifnottex 6495b7b453SJohn Marino@node Top 6595b7b453SJohn Marino@top grep 6695b7b453SJohn Marino 67*09d4459fSDaniel Fojt@command{grep} prints lines that contain a match for one or more patterns. 6895b7b453SJohn Marino 6995b7b453SJohn MarinoThis manual is for version @value{VERSION} of GNU Grep. 7095b7b453SJohn Marino 7195b7b453SJohn Marino@insertcopying 7295b7b453SJohn Marino@end ifnottex 7395b7b453SJohn Marino 7495b7b453SJohn Marino@menu 7595b7b453SJohn Marino* Introduction:: Introduction. 7695b7b453SJohn Marino* Invoking:: Command-line options, environment, exit status. 7795b7b453SJohn Marino* Regular Expressions:: Regular Expressions. 7895b7b453SJohn Marino* Usage:: Examples. 79*09d4459fSDaniel Fojt* Performance:: Performance tuning. 8095b7b453SJohn Marino* Reporting Bugs:: Reporting Bugs. 8195b7b453SJohn Marino* Copying:: License terms for this manual. 8295b7b453SJohn Marino* Index:: Combined index. 8395b7b453SJohn Marino@end menu 8495b7b453SJohn Marino 8595b7b453SJohn Marino 8695b7b453SJohn Marino@node Introduction 8795b7b453SJohn Marino@chapter Introduction 8895b7b453SJohn Marino 89*09d4459fSDaniel Fojt@cindex searching for patterns 9095b7b453SJohn Marino 91*09d4459fSDaniel FojtGiven one or more patterns, @command{grep} searches input files 92*09d4459fSDaniel Fojtfor matches to the patterns. 9395b7b453SJohn MarinoWhen it finds a match in a line, 9495b7b453SJohn Marinoit copies the line to standard output (by default), 9595b7b453SJohn Marinoor produces whatever other sort of output you have requested with options. 9695b7b453SJohn Marino 9795b7b453SJohn MarinoThough @command{grep} expects to do the matching on text, 9895b7b453SJohn Marinoit has no limits on input line length other than available memory, 9995b7b453SJohn Marinoand it can match arbitrary characters within a line. 10095b7b453SJohn MarinoIf the final byte of an input file is not a newline, 10195b7b453SJohn Marino@command{grep} silently supplies one. 10295b7b453SJohn MarinoSince newline is also a separator for the list of patterns, 10395b7b453SJohn Marinothere is no way to match newline characters in a text. 10495b7b453SJohn Marino 10595b7b453SJohn Marino 10695b7b453SJohn Marino@node Invoking 10795b7b453SJohn Marino@chapter Invoking @command{grep} 10895b7b453SJohn Marino 10995b7b453SJohn MarinoThe general synopsis of the @command{grep} command line is 11095b7b453SJohn Marino 11195b7b453SJohn Marino@example 112*09d4459fSDaniel Fojtgrep [@var{option}...] [@var{patterns}] [@var{file}...] 11395b7b453SJohn Marino@end example 11495b7b453SJohn Marino 11595b7b453SJohn Marino@noindent 116*09d4459fSDaniel FojtThere can be zero or more @var{option} arguments, and zero or more 117*09d4459fSDaniel Fojt@var{file} arguments. The @var{patterns} argument contains one or 118*09d4459fSDaniel Fojtmore patterns separated by newlines, and is omitted when patterns are 119*09d4459fSDaniel Fojtgiven via the @samp{-e@ @var{patterns}} or @samp{-f@ @var{file}} 120*09d4459fSDaniel Fojtoptions. Typically @var{patterns} should be quoted when 121*09d4459fSDaniel Fojt@command{grep} is used in a shell command. 12295b7b453SJohn Marino 12395b7b453SJohn Marino@menu 12495b7b453SJohn Marino* Command-line Options:: Short and long names, grouped by category. 12595b7b453SJohn Marino* Environment Variables:: POSIX, GNU generic, and GNU grep specific. 12695b7b453SJohn Marino* Exit Status:: Exit status returned by @command{grep}. 12795b7b453SJohn Marino* grep Programs:: @command{grep} programs. 12895b7b453SJohn Marino@end menu 12995b7b453SJohn Marino 13095b7b453SJohn Marino@node Command-line Options 13195b7b453SJohn Marino@section Command-line Options 13295b7b453SJohn Marino 13395b7b453SJohn Marino@command{grep} comes with a rich set of options: 134cf28ed85SJohn Marinosome from POSIX and some being GNU extensions. 135cf28ed85SJohn MarinoLong option names are always a GNU extension, 136cf28ed85SJohn Marinoeven for options that are from POSIX specifications. 137cf28ed85SJohn MarinoOptions that are specified by POSIX, 13895b7b453SJohn Marinounder their short names, 13995b7b453SJohn Marinoare explicitly marked as such 140cf28ed85SJohn Marinoto facilitate POSIX-portable programming. 14195b7b453SJohn MarinoA few option names are provided 14295b7b453SJohn Marinofor compatibility with older or more exotic implementations. 14395b7b453SJohn Marino 14495b7b453SJohn Marino@menu 14595b7b453SJohn Marino* Generic Program Information:: 14695b7b453SJohn Marino* Matching Control:: 14795b7b453SJohn Marino* General Output Control:: 14895b7b453SJohn Marino* Output Line Prefix Control:: 14995b7b453SJohn Marino* Context Line Control:: 15095b7b453SJohn Marino* File and Directory Selection:: 15195b7b453SJohn Marino* Other Options:: 15295b7b453SJohn Marino@end menu 15395b7b453SJohn Marino 15495b7b453SJohn MarinoSeveral additional options control 15595b7b453SJohn Marinowhich variant of the @command{grep} matching engine is used. 15695b7b453SJohn Marino@xref{grep Programs}. 15795b7b453SJohn Marino 15895b7b453SJohn Marino@node Generic Program Information 15995b7b453SJohn Marino@subsection Generic Program Information 16095b7b453SJohn Marino 161cf28ed85SJohn Marino@table @option 16295b7b453SJohn Marino 16395b7b453SJohn Marino@item --help 16495b7b453SJohn Marino@opindex --help 16595b7b453SJohn Marino@cindex usage summary, printing 16695b7b453SJohn MarinoPrint a usage message briefly summarizing the command-line options 16795b7b453SJohn Marinoand the bug-reporting address, then exit. 16895b7b453SJohn Marino 16995b7b453SJohn Marino@item -V 17095b7b453SJohn Marino@itemx --version 17195b7b453SJohn Marino@opindex -V 17295b7b453SJohn Marino@opindex --version 17395b7b453SJohn Marino@cindex version, printing 17495b7b453SJohn MarinoPrint the version number of @command{grep} to the standard output stream. 17595b7b453SJohn MarinoThis version number should be included in all bug reports. 17695b7b453SJohn Marino 17795b7b453SJohn Marino@end table 17895b7b453SJohn Marino 17995b7b453SJohn Marino@node Matching Control 18095b7b453SJohn Marino@subsection Matching Control 18195b7b453SJohn Marino 182cf28ed85SJohn Marino@table @option 18395b7b453SJohn Marino 184*09d4459fSDaniel Fojt@item -e @var{patterns} 185*09d4459fSDaniel Fojt@itemx --regexp=@var{patterns} 18695b7b453SJohn Marino@opindex -e 187*09d4459fSDaniel Fojt@opindex --regexp=@var{patterns} 188*09d4459fSDaniel Fojt@cindex patterns option 189*09d4459fSDaniel FojtUse @var{patterns} as one or more patterns; newlines within 190*09d4459fSDaniel Fojt@var{patterns} separate each pattern from the next. 191*09d4459fSDaniel FojtIf this option is used multiple times or is combined with the 192*09d4459fSDaniel Fojt@option{-f} (@option{--file}) option, search for all patterns given. 193*09d4459fSDaniel FojtTypically @var{patterns} should be quoted when @command{grep} is used 194*09d4459fSDaniel Fojtin a shell command. 195cf28ed85SJohn Marino(@option{-e} is specified by POSIX.) 19695b7b453SJohn Marino 19795b7b453SJohn Marino@item -f @var{file} 19895b7b453SJohn Marino@itemx --file=@var{file} 19995b7b453SJohn Marino@opindex -f 20095b7b453SJohn Marino@opindex --file 201*09d4459fSDaniel Fojt@cindex patterns from file 20295b7b453SJohn MarinoObtain patterns from @var{file}, one per line. 203*09d4459fSDaniel FojtIf this option is used multiple times or is combined with the 204*09d4459fSDaniel Fojt@option{-e} (@option{--regexp}) option, search for all patterns given. 20595b7b453SJohn MarinoThe empty file contains zero patterns, and therefore matches nothing. 206cf28ed85SJohn Marino(@option{-f} is specified by POSIX.) 20795b7b453SJohn Marino 20895b7b453SJohn Marino@item -i 20995b7b453SJohn Marino@itemx -y 21095b7b453SJohn Marino@itemx --ignore-case 21195b7b453SJohn Marino@opindex -i 21295b7b453SJohn Marino@opindex -y 21395b7b453SJohn Marino@opindex --ignore-case 21495b7b453SJohn Marino@cindex case insensitive search 215*09d4459fSDaniel FojtIgnore case distinctions in patterns and input data, 216*09d4459fSDaniel Fojtso that characters that differ only in case 217680a9cb8SJohn Marinomatch each other. Although this is straightforward when letters 218680a9cb8SJohn Marinodiffer in case only via lowercase-uppercase pairs, the behavior is 219680a9cb8SJohn Marinounspecified in other situations. For example, uppercase ``S'' has an 220680a9cb8SJohn Marinounusual lowercase counterpart ``ſ'' (Unicode character U+017F, LATIN 221680a9cb8SJohn MarinoSMALL LETTER LONG S) in many locales, and it is unspecified whether 222680a9cb8SJohn Marinothis unusual character matches ``S'' or ``s'' even though uppercasing 223680a9cb8SJohn Marinoit yields ``S''. Another example: the lowercase German letter ``ß'' 224680a9cb8SJohn Marino(U+00DF, LATIN SMALL LETTER SHARP S) is normally capitalized as the 225680a9cb8SJohn Marinotwo-character string ``SS'' but it does not match ``SS'', and it might 226680a9cb8SJohn Marinonot match the uppercase letter ``ẞ'' (U+1E9E, LATIN CAPITAL LETTER 227680a9cb8SJohn MarinoSHARP S) even though lowercasing the latter yields the former. 228680a9cb8SJohn Marino 229cf28ed85SJohn Marino@option{-y} is an obsolete synonym that is provided for compatibility. 230cf28ed85SJohn Marino(@option{-i} is specified by POSIX.) 23195b7b453SJohn Marino 232*09d4459fSDaniel Fojt@item --no-ignore-case 233*09d4459fSDaniel Fojt@opindex --no-ignore-case 234*09d4459fSDaniel FojtDo not ignore case distinctions in patterns and input data. This is 235*09d4459fSDaniel Fojtthe default. This option is useful for passing to shell scripts that 236*09d4459fSDaniel Fojtalready use @option{-i}, in order to cancel its effects because the 237*09d4459fSDaniel Fojttwo options override each other. 238*09d4459fSDaniel Fojt 23995b7b453SJohn Marino@item -v 24095b7b453SJohn Marino@itemx --invert-match 24195b7b453SJohn Marino@opindex -v 24295b7b453SJohn Marino@opindex --invert-match 24395b7b453SJohn Marino@cindex invert matching 24495b7b453SJohn Marino@cindex print non-matching lines 24595b7b453SJohn MarinoInvert the sense of matching, to select non-matching lines. 246cf28ed85SJohn Marino(@option{-v} is specified by POSIX.) 24795b7b453SJohn Marino 24895b7b453SJohn Marino@item -w 24995b7b453SJohn Marino@itemx --word-regexp 25095b7b453SJohn Marino@opindex -w 25195b7b453SJohn Marino@opindex --word-regexp 25295b7b453SJohn Marino@cindex matching whole words 25395b7b453SJohn MarinoSelect only those lines containing matches that form whole words. 25495b7b453SJohn MarinoThe test is that the matching substring must either 25595b7b453SJohn Marinobe at the beginning of the line, 25695b7b453SJohn Marinoor preceded by a non-word constituent character. 25795b7b453SJohn MarinoSimilarly, 25895b7b453SJohn Marinoit must be either at the end of the line 25995b7b453SJohn Marinoor followed by a non-word constituent character. 260*09d4459fSDaniel FojtWord constituent characters are letters, digits, and the underscore. 261*09d4459fSDaniel FojtThis option has no effect if @option{-x} is also specified. 262*09d4459fSDaniel Fojt 263*09d4459fSDaniel FojtBecause the @option{-w} option can match a substring that does not 264*09d4459fSDaniel Fojtbegin and end with word constituents, it differs from surrounding a 265*09d4459fSDaniel Fojtregular expression with @samp{\<} and @samp{\>}. For example, although 266*09d4459fSDaniel Fojt@samp{grep -w @@} matches a line containing only @samp{@@}, @samp{grep 267*09d4459fSDaniel Fojt'\<@@\>'} cannot match any line because @samp{@@} is not a 268*09d4459fSDaniel Fojtword constituent. @xref{The Backslash Character and Special 269*09d4459fSDaniel FojtExpressions}. 27095b7b453SJohn Marino 27195b7b453SJohn Marino@item -x 27295b7b453SJohn Marino@itemx --line-regexp 27395b7b453SJohn Marino@opindex -x 27495b7b453SJohn Marino@opindex --line-regexp 27595b7b453SJohn Marino@cindex match the whole line 27695b7b453SJohn MarinoSelect only those matches that exactly match the whole line. 277*09d4459fSDaniel FojtFor regular expression patterns, this is like parenthesizing each 278*09d4459fSDaniel Fojtpattern and then surrounding it with @samp{^} and @samp{$}. 279cf28ed85SJohn Marino(@option{-x} is specified by POSIX.) 28095b7b453SJohn Marino 28195b7b453SJohn Marino@end table 28295b7b453SJohn Marino 28395b7b453SJohn Marino@node General Output Control 28495b7b453SJohn Marino@subsection General Output Control 28595b7b453SJohn Marino 286cf28ed85SJohn Marino@table @option 28795b7b453SJohn Marino 28895b7b453SJohn Marino@item -c 28995b7b453SJohn Marino@itemx --count 29095b7b453SJohn Marino@opindex -c 29195b7b453SJohn Marino@opindex --count 29295b7b453SJohn Marino@cindex counting lines 29395b7b453SJohn MarinoSuppress normal output; 29495b7b453SJohn Marinoinstead print a count of matching lines for each input file. 295cf28ed85SJohn MarinoWith the @option{-v} (@option{--invert-match}) option, 29695b7b453SJohn Marinocount non-matching lines. 297cf28ed85SJohn Marino(@option{-c} is specified by POSIX.) 29895b7b453SJohn Marino 29995b7b453SJohn Marino@item --color[=@var{WHEN}] 30095b7b453SJohn Marino@itemx --colour[=@var{WHEN}] 30195b7b453SJohn Marino@opindex --color 30295b7b453SJohn Marino@opindex --colour 30395b7b453SJohn Marino@cindex highlight, color, colour 30495b7b453SJohn MarinoSurround the matched (non-empty) strings, matching lines, context lines, 30595b7b453SJohn Marinofile names, line numbers, byte offsets, and separators (for fields and 30695b7b453SJohn Marinogroups of context lines) with escape sequences to display them in color 30795b7b453SJohn Marinoon the terminal. 308cf28ed85SJohn MarinoThe colors are defined by the environment variable @env{GREP_COLORS} 30995b7b453SJohn Marinoand default to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36} 31095b7b453SJohn Marinofor bold red matched text, magenta file names, green line numbers, 31195b7b453SJohn Marinogreen byte offsets, cyan separators, and default terminal colors otherwise. 312cf28ed85SJohn MarinoThe deprecated environment variable @env{GREP_COLOR} is still supported, 31395b7b453SJohn Marinobut its setting does not have priority; 314a8597f6cSJohn Marinoit defaults to @samp{01;31} (bold red) 31595b7b453SJohn Marinowhich only covers the color for matched text. 31695b7b453SJohn Marino@var{WHEN} is @samp{never}, @samp{always}, or @samp{auto}. 31795b7b453SJohn Marino 31895b7b453SJohn Marino@item -L 31995b7b453SJohn Marino@itemx --files-without-match 32095b7b453SJohn Marino@opindex -L 32195b7b453SJohn Marino@opindex --files-without-match 32295b7b453SJohn Marino@cindex files which don't match 32395b7b453SJohn MarinoSuppress normal output; 32495b7b453SJohn Marinoinstead print the name of each input file from which 32595b7b453SJohn Marinono output would normally have been printed. 326cf28ed85SJohn MarinoThe scanning of each file stops on the first match. 32795b7b453SJohn Marino 32895b7b453SJohn Marino@item -l 32995b7b453SJohn Marino@itemx --files-with-matches 33095b7b453SJohn Marino@opindex -l 33195b7b453SJohn Marino@opindex --files-with-matches 33295b7b453SJohn Marino@cindex names of matching files 33395b7b453SJohn MarinoSuppress normal output; 33495b7b453SJohn Marinoinstead print the name of each input file from which 33595b7b453SJohn Marinooutput would normally have been printed. 336cf28ed85SJohn MarinoThe scanning of each file stops on the first match. 337cf28ed85SJohn Marino(@option{-l} is specified by POSIX.) 33895b7b453SJohn Marino 33995b7b453SJohn Marino@item -m @var{num} 34095b7b453SJohn Marino@itemx --max-count=@var{num} 34195b7b453SJohn Marino@opindex -m 34295b7b453SJohn Marino@opindex --max-count 34395b7b453SJohn Marino@cindex max-count 344*09d4459fSDaniel FojtStop after the first @var{num} selected lines. 34595b7b453SJohn MarinoIf the input is standard input from a regular file, 346*09d4459fSDaniel Fojtand @var{num} selected lines are output, 34795b7b453SJohn Marino@command{grep} ensures that the standard input is positioned 348*09d4459fSDaniel Fojtjust after the last selected line before exiting, 34995b7b453SJohn Marinoregardless of the presence of trailing context lines. 35095b7b453SJohn MarinoThis enables a calling process to resume a search. 35195b7b453SJohn MarinoFor example, the following shell script makes use of it: 35295b7b453SJohn Marino 35395b7b453SJohn Marino@example 354*09d4459fSDaniel Fojtwhile grep -m 1 'PATTERN' 35595b7b453SJohn Marinodo 35695b7b453SJohn Marino echo xxxx 35795b7b453SJohn Marinodone < FILE 35895b7b453SJohn Marino@end example 35995b7b453SJohn Marino 36095b7b453SJohn MarinoBut the following probably will not work because a pipe is not a regular 36195b7b453SJohn Marinofile: 36295b7b453SJohn Marino 36395b7b453SJohn Marino@example 36495b7b453SJohn Marino# This probably will not work. 36595b7b453SJohn Marinocat FILE | 366*09d4459fSDaniel Fojtwhile grep -m 1 'PATTERN' 36795b7b453SJohn Marinodo 36895b7b453SJohn Marino echo xxxx 36995b7b453SJohn Marinodone 37095b7b453SJohn Marino@end example 37195b7b453SJohn Marino 372*09d4459fSDaniel Fojt@cindex context lines 373*09d4459fSDaniel FojtWhen @command{grep} stops after @var{num} selected lines, 37495b7b453SJohn Marinoit outputs any trailing context lines. 375cf28ed85SJohn MarinoWhen the @option{-c} or @option{--count} option is also used, 37695b7b453SJohn Marino@command{grep} does not output a count greater than @var{num}. 377cf28ed85SJohn MarinoWhen the @option{-v} or @option{--invert-match} option is also used, 37895b7b453SJohn Marino@command{grep} stops after outputting @var{num} non-matching lines. 37995b7b453SJohn Marino 38095b7b453SJohn Marino@item -o 38195b7b453SJohn Marino@itemx --only-matching 38295b7b453SJohn Marino@opindex -o 38395b7b453SJohn Marino@opindex --only-matching 38495b7b453SJohn Marino@cindex only matching 38595b7b453SJohn MarinoPrint only the matched (non-empty) parts of matching lines, 38695b7b453SJohn Marinowith each such part on a separate output line. 387*09d4459fSDaniel FojtOutput lines use the same delimiters as input, and delimiters are null 388*09d4459fSDaniel Fojtbytes if @option{-z} (@option{--null-data}) is also used (@pxref{Other 389*09d4459fSDaniel FojtOptions}). 39095b7b453SJohn Marino 39195b7b453SJohn Marino@item -q 39295b7b453SJohn Marino@itemx --quiet 39395b7b453SJohn Marino@itemx --silent 39495b7b453SJohn Marino@opindex -q 39595b7b453SJohn Marino@opindex --quiet 39695b7b453SJohn Marino@opindex --silent 39795b7b453SJohn Marino@cindex quiet, silent 39895b7b453SJohn MarinoQuiet; do not write anything to standard output. 39995b7b453SJohn MarinoExit immediately with zero status if any match is found, 40095b7b453SJohn Marinoeven if an error was detected. 401cf28ed85SJohn MarinoAlso see the @option{-s} or @option{--no-messages} option. 402cf28ed85SJohn Marino(@option{-q} is specified by POSIX.) 40395b7b453SJohn Marino 40495b7b453SJohn Marino@item -s 40595b7b453SJohn Marino@itemx --no-messages 40695b7b453SJohn Marino@opindex -s 40795b7b453SJohn Marino@opindex --no-messages 40895b7b453SJohn Marino@cindex suppress error messages 40995b7b453SJohn MarinoSuppress error messages about nonexistent or unreadable files. 41095b7b453SJohn MarinoPortability note: 411cf28ed85SJohn Marinounlike GNU @command{grep}, 412cf28ed85SJohn Marino7th Edition Unix @command{grep} did not conform to POSIX, 413cf28ed85SJohn Marinobecause it lacked @option{-q} 414cf28ed85SJohn Marinoand its @option{-s} option behaved like 415cf28ed85SJohn MarinoGNU @command{grep}'s @option{-q} option.@footnote{Of course, 7th Edition 416cf28ed85SJohn MarinoUnix predated POSIX by several years!} 417cf28ed85SJohn MarinoUSG-style @command{grep} also lacked @option{-q} 418cf28ed85SJohn Marinobut its @option{-s} option behaved like GNU @command{grep}'s. 41995b7b453SJohn MarinoPortable shell scripts should avoid both 420cf28ed85SJohn Marino@option{-q} and @option{-s} and should redirect 42195b7b453SJohn Marinostandard and error output to @file{/dev/null} instead. 422cf28ed85SJohn Marino(@option{-s} is specified by POSIX.) 42395b7b453SJohn Marino 42495b7b453SJohn Marino@end table 42595b7b453SJohn Marino 42695b7b453SJohn Marino@node Output Line Prefix Control 42795b7b453SJohn Marino@subsection Output Line Prefix Control 42895b7b453SJohn Marino 42995b7b453SJohn MarinoWhen several prefix fields are to be output, 43095b7b453SJohn Marinothe order is always file name, line number, and byte offset, 43195b7b453SJohn Marinoregardless of the order in which these options were specified. 43295b7b453SJohn Marino 433cf28ed85SJohn Marino@table @option 43495b7b453SJohn Marino 43595b7b453SJohn Marino@item -b 43695b7b453SJohn Marino@itemx --byte-offset 43795b7b453SJohn Marino@opindex -b 43895b7b453SJohn Marino@opindex --byte-offset 43995b7b453SJohn Marino@cindex byte offset 44095b7b453SJohn MarinoPrint the 0-based byte offset within the input file 44195b7b453SJohn Marinobefore each line of output. 442cf28ed85SJohn MarinoIf @option{-o} (@option{--only-matching}) is specified, 44395b7b453SJohn Marinoprint the offset of the matching part itself. 44495b7b453SJohn Marino 44595b7b453SJohn Marino@item -H 44695b7b453SJohn Marino@itemx --with-filename 44795b7b453SJohn Marino@opindex -H 44895b7b453SJohn Marino@opindex --with-filename 44995b7b453SJohn Marino@cindex with filename prefix 45095b7b453SJohn MarinoPrint the file name for each match. 45195b7b453SJohn MarinoThis is the default when there is more than one file to search. 45295b7b453SJohn Marino 45395b7b453SJohn Marino@item -h 45495b7b453SJohn Marino@itemx --no-filename 45595b7b453SJohn Marino@opindex -h 45695b7b453SJohn Marino@opindex --no-filename 45795b7b453SJohn Marino@cindex no filename prefix 45895b7b453SJohn MarinoSuppress the prefixing of file names on output. 45995b7b453SJohn MarinoThis is the default when there is only one file 46095b7b453SJohn Marino(or only standard input) to search. 46195b7b453SJohn Marino 46295b7b453SJohn Marino@item --label=@var{LABEL} 46395b7b453SJohn Marino@opindex --label 46495b7b453SJohn Marino@cindex changing name of standard input 46595b7b453SJohn MarinoDisplay input actually coming from standard input 466*09d4459fSDaniel Fojtas input coming from file @var{LABEL}. 467*09d4459fSDaniel FojtThis can be useful for commands that transform a file's contents 468*09d4459fSDaniel Fojtbefore searching; e.g.: 46995b7b453SJohn Marino 47095b7b453SJohn Marino@example 471*09d4459fSDaniel Fojtgzip -cd foo.gz | grep --label=foo -H 'some pattern' 47295b7b453SJohn Marino@end example 47395b7b453SJohn Marino 47495b7b453SJohn Marino@item -n 47595b7b453SJohn Marino@itemx --line-number 47695b7b453SJohn Marino@opindex -n 47795b7b453SJohn Marino@opindex --line-number 47895b7b453SJohn Marino@cindex line numbering 47995b7b453SJohn MarinoPrefix each line of output with the 1-based line number within its input file. 480cf28ed85SJohn Marino(@option{-n} is specified by POSIX.) 48195b7b453SJohn Marino 48295b7b453SJohn Marino@item -T 48395b7b453SJohn Marino@itemx --initial-tab 48495b7b453SJohn Marino@opindex -T 48595b7b453SJohn Marino@opindex --initial-tab 48695b7b453SJohn Marino@cindex tab-aligned content lines 48795b7b453SJohn MarinoMake sure that the first character of actual line content lies on a tab stop, 48895b7b453SJohn Marinoso that the alignment of tabs looks normal. 48995b7b453SJohn MarinoThis is useful with options that prefix their output to the actual content: 490cf28ed85SJohn Marino@option{-H}, @option{-n}, and @option{-b}. 491*09d4459fSDaniel FojtThis may also prepend spaces to output line numbers and byte offsets 492*09d4459fSDaniel Fojtso that lines from a single file all start at the same column. 49395b7b453SJohn Marino 49495b7b453SJohn Marino@item -Z 49595b7b453SJohn Marino@itemx --null 49695b7b453SJohn Marino@opindex -Z 49795b7b453SJohn Marino@opindex --null 49895b7b453SJohn Marino@cindex zero-terminated file names 499dc7c36e4SJohn MarinoOutput a zero byte (the ASCII NUL character) 50095b7b453SJohn Marinoinstead of the character that normally follows a file name. 50195b7b453SJohn MarinoFor example, 50295b7b453SJohn Marino@samp{grep -lZ} outputs a zero byte after each file name 50395b7b453SJohn Marinoinstead of the usual newline. 50495b7b453SJohn MarinoThis option makes the output unambiguous, 50595b7b453SJohn Marinoeven in the presence of file names containing unusual characters like newlines. 50695b7b453SJohn MarinoThis option can be used with commands like 50795b7b453SJohn Marino@samp{find -print0}, @samp{perl -0}, @samp{sort -z}, and @samp{xargs -0} 50895b7b453SJohn Marinoto process arbitrary file names, 50995b7b453SJohn Marinoeven those that contain newline characters. 51095b7b453SJohn Marino 51195b7b453SJohn Marino@end table 51295b7b453SJohn Marino 51395b7b453SJohn Marino@node Context Line Control 51495b7b453SJohn Marino@subsection Context Line Control 51595b7b453SJohn Marino 516*09d4459fSDaniel Fojt@cindex context lines 517*09d4459fSDaniel Fojt@dfn{Context lines} are non-matching lines that are near a matching line. 518*09d4459fSDaniel FojtThey are output only if one of the following options are used. 51995b7b453SJohn MarinoRegardless of how these options are set, 520*09d4459fSDaniel Fojt@command{grep} never outputs any given line more than once. 521cf28ed85SJohn MarinoIf the @option{-o} (@option{--only-matching}) option is specified, 52295b7b453SJohn Marinothese options have no effect and a warning is given upon their use. 52395b7b453SJohn Marino 524cf28ed85SJohn Marino@table @option 52595b7b453SJohn Marino 52695b7b453SJohn Marino@item -A @var{num} 52795b7b453SJohn Marino@itemx --after-context=@var{num} 52895b7b453SJohn Marino@opindex -A 52995b7b453SJohn Marino@opindex --after-context 53095b7b453SJohn Marino@cindex after context 53195b7b453SJohn Marino@cindex context lines, after match 53295b7b453SJohn MarinoPrint @var{num} lines of trailing context after matching lines. 53395b7b453SJohn Marino 53495b7b453SJohn Marino@item -B @var{num} 53595b7b453SJohn Marino@itemx --before-context=@var{num} 53695b7b453SJohn Marino@opindex -B 53795b7b453SJohn Marino@opindex --before-context 53895b7b453SJohn Marino@cindex before context 53995b7b453SJohn Marino@cindex context lines, before match 54095b7b453SJohn MarinoPrint @var{num} lines of leading context before matching lines. 54195b7b453SJohn Marino 54295b7b453SJohn Marino@item -C @var{num} 54395b7b453SJohn Marino@itemx -@var{num} 54495b7b453SJohn Marino@itemx --context=@var{num} 54595b7b453SJohn Marino@opindex -C 54695b7b453SJohn Marino@opindex --context 54795b7b453SJohn Marino@opindex -@var{num} 548*09d4459fSDaniel Fojt@cindex context lines 54995b7b453SJohn MarinoPrint @var{num} lines of leading and trailing output context. 55095b7b453SJohn Marino 55195b7b453SJohn Marino@item --group-separator=@var{string} 55295b7b453SJohn Marino@opindex --group-separator 55395b7b453SJohn Marino@cindex group separator 55495b7b453SJohn MarinoWhen @option{-A}, @option{-B} or @option{-C} are in use, 555680a9cb8SJohn Marinoprint @var{string} instead of @option{--} between groups of lines. 55695b7b453SJohn Marino 55795b7b453SJohn Marino@item --no-group-separator 55895b7b453SJohn Marino@opindex --group-separator 55995b7b453SJohn Marino@cindex group separator 56095b7b453SJohn MarinoWhen @option{-A}, @option{-B} or @option{-C} are in use, 561680a9cb8SJohn Marinodo not print a separator between groups of lines. 56295b7b453SJohn Marino 56395b7b453SJohn Marino@end table 56495b7b453SJohn Marino 565cf28ed85SJohn MarinoHere are some points about how @command{grep} chooses 566cf28ed85SJohn Marinothe separator to print between prefix fields and line content: 567cf28ed85SJohn Marino 568cf28ed85SJohn Marino@itemize @bullet 569cf28ed85SJohn Marino@item 57095b7b453SJohn MarinoMatching lines normally use @samp{:} as a separator 57195b7b453SJohn Marinobetween prefix fields and actual line content. 572cf28ed85SJohn Marino 573cf28ed85SJohn Marino@item 57495b7b453SJohn MarinoContext (i.e., non-matching) lines use @samp{-} instead. 575cf28ed85SJohn Marino 576cf28ed85SJohn Marino@item 577680a9cb8SJohn MarinoWhen context is not specified, 57895b7b453SJohn Marinomatching lines are simply output one right after another. 579cf28ed85SJohn Marino 580cf28ed85SJohn Marino@item 581680a9cb8SJohn MarinoWhen context is specified, 58295b7b453SJohn Marinolines that are adjacent in the input form a group 58395b7b453SJohn Marinoand are output one right after another, while 584680a9cb8SJohn Marinoby default a separator appears between non-adjacent groups. 585cf28ed85SJohn Marino 586cf28ed85SJohn Marino@item 587cf28ed85SJohn MarinoThe default separator 588680a9cb8SJohn Marinois a @samp{--} line; its presence and appearance 589cf28ed85SJohn Marinocan be changed with the options above. 590cf28ed85SJohn Marino 591cf28ed85SJohn Marino@item 592cf28ed85SJohn MarinoEach group may contain 59395b7b453SJohn Marinoseveral matching lines when they are close enough to each other 594680a9cb8SJohn Marinothat two adjacent groups connect and can merge into a single 595680a9cb8SJohn Marinocontiguous one. 596cf28ed85SJohn Marino@end itemize 59795b7b453SJohn Marino 59895b7b453SJohn Marino@node File and Directory Selection 59995b7b453SJohn Marino@subsection File and Directory Selection 60095b7b453SJohn Marino 601cf28ed85SJohn Marino@table @option 60295b7b453SJohn Marino 60395b7b453SJohn Marino@item -a 60495b7b453SJohn Marino@itemx --text 60595b7b453SJohn Marino@opindex -a 60695b7b453SJohn Marino@opindex --text 60795b7b453SJohn Marino@cindex suppress binary data 60895b7b453SJohn Marino@cindex binary files 60995b7b453SJohn MarinoProcess a binary file as if it were text; 61095b7b453SJohn Marinothis is equivalent to the @samp{--binary-files=text} option. 61195b7b453SJohn Marino 612cf28ed85SJohn Marino@item --binary-files=@var{type} 61395b7b453SJohn Marino@opindex --binary-files 61495b7b453SJohn Marino@cindex binary files 615*09d4459fSDaniel FojtIf a file's data or metadata 616a8597f6cSJohn Marinoindicate that the file contains binary data, 61795b7b453SJohn Marinoassume that the file is of type @var{type}. 618*09d4459fSDaniel FojtNon-text bytes indicate binary data; these are either output bytes that are 619*09d4459fSDaniel Fojtimproperly encoded for the current locale (@pxref{Environment 620*09d4459fSDaniel FojtVariables}), or null input bytes when the 621dc7c36e4SJohn Marino@option{-z} (@option{--null-data}) option is not given (@pxref{Other 622dc7c36e4SJohn MarinoOptions}). 623dc7c36e4SJohn Marino 624*09d4459fSDaniel FojtBy default, @var{type} is @samp{binary}, and @command{grep} 625*09d4459fSDaniel Fojtsuppresses output after null input binary data is discovered, 626*09d4459fSDaniel Fojtand suppresses output lines that contain improperly encoded data. 627*09d4459fSDaniel FojtWhen some output is suppressed, @command{grep} follows any output 628*09d4459fSDaniel Fojtwith a one-line message saying that a binary file matches. 629cf28ed85SJohn Marino 63095b7b453SJohn MarinoIf @var{type} is @samp{without-match}, 631*09d4459fSDaniel Fojtwhen @command{grep} discovers null input binary data 632*09d4459fSDaniel Fojtit assumes that the rest of the file does not match; 633cf28ed85SJohn Marinothis is equivalent to the @option{-I} option. 634cf28ed85SJohn Marino 63595b7b453SJohn MarinoIf @var{type} is @samp{text}, 636*09d4459fSDaniel Fojt@command{grep} processes binary data as if it were text; 637cf28ed85SJohn Marinothis is equivalent to the @option{-a} option. 638cf28ed85SJohn Marino 639*09d4459fSDaniel FojtWhen @var{type} is @samp{binary}, @command{grep} may treat non-text 640*09d4459fSDaniel Fojtbytes as line terminators even without the @option{-z} 641*09d4459fSDaniel Fojt(@option{--null-data}) option. This means choosing @samp{binary} 642*09d4459fSDaniel Fojtversus @samp{text} can affect whether a pattern matches a file. For 643*09d4459fSDaniel Fojtexample, when @var{type} is @samp{binary} the pattern @samp{q$} might 644*09d4459fSDaniel Fojtmatch @samp{q} immediately followed by a null byte, even though this 645*09d4459fSDaniel Fojtis not matched when @var{type} is @samp{text}. Conversely, when 646*09d4459fSDaniel Fojt@var{type} is @samp{binary} the pattern @samp{.} (period) might not 647*09d4459fSDaniel Fojtmatch a null byte. 648*09d4459fSDaniel Fojt 649*09d4459fSDaniel Fojt@emph{Warning:} The @option{-a} (@option{--binary-files=text}) option 650*09d4459fSDaniel Fojtmight output binary garbage, which can have nasty side effects if the 651*09d4459fSDaniel Fojtoutput is a terminal and if the terminal driver interprets some of it 652*09d4459fSDaniel Fojtas commands. On the other hand, when reading files whose text 653*09d4459fSDaniel Fojtencodings are unknown, it can be helpful to use @option{-a} or to set 654*09d4459fSDaniel Fojt@samp{LC_ALL='C'} in the environment, in order to find more matches 655*09d4459fSDaniel Fojteven if the matches are unsafe for direct display. 65695b7b453SJohn Marino 65795b7b453SJohn Marino@item -D @var{action} 65895b7b453SJohn Marino@itemx --devices=@var{action} 65995b7b453SJohn Marino@opindex -D 66095b7b453SJohn Marino@opindex --devices 66195b7b453SJohn Marino@cindex device search 66295b7b453SJohn MarinoIf an input file is a device, FIFO, or socket, use @var{action} to process it. 663cf28ed85SJohn MarinoIf @var{action} is @samp{read}, 664cf28ed85SJohn Marinoall devices are read just as if they were ordinary files. 66595b7b453SJohn MarinoIf @var{action} is @samp{skip}, 66695b7b453SJohn Marinodevices, FIFOs, and sockets are silently skipped. 667cf28ed85SJohn MarinoBy default, devices are read if they are on the command line or if the 668cf28ed85SJohn Marino@option{-R} (@option{--dereference-recursive}) option is used, and are 669cf28ed85SJohn Marinoskipped if they are encountered recursively and the @option{-r} 670cf28ed85SJohn Marino(@option{--recursive}) option is used. 671cf28ed85SJohn MarinoThis option has no effect on a file that is read via standard input. 67295b7b453SJohn Marino 67395b7b453SJohn Marino@item -d @var{action} 67495b7b453SJohn Marino@itemx --directories=@var{action} 67595b7b453SJohn Marino@opindex -d 67695b7b453SJohn Marino@opindex --directories 67795b7b453SJohn Marino@cindex directory search 678cf28ed85SJohn Marino@cindex symbolic links 67995b7b453SJohn MarinoIf an input file is a directory, use @var{action} to process it. 68095b7b453SJohn MarinoBy default, @var{action} is @samp{read}, 68195b7b453SJohn Marinowhich means that directories are read just as if they were ordinary files 68295b7b453SJohn Marino(some operating systems and file systems disallow this, 68395b7b453SJohn Marinoand will cause @command{grep} 68495b7b453SJohn Marinoto print error messages for every directory or silently skip them). 68595b7b453SJohn MarinoIf @var{action} is @samp{skip}, directories are silently skipped. 68695b7b453SJohn MarinoIf @var{action} is @samp{recurse}, 687cf28ed85SJohn Marino@command{grep} reads all files under each directory, recursively, 688cf28ed85SJohn Marinofollowing command-line symbolic links and skipping other symlinks; 689cf28ed85SJohn Marinothis is equivalent to the @option{-r} option. 69095b7b453SJohn Marino 69195b7b453SJohn Marino@item --exclude=@var{glob} 69295b7b453SJohn Marino@opindex --exclude 69395b7b453SJohn Marino@cindex exclude files 69495b7b453SJohn Marino@cindex searching directory trees 695*09d4459fSDaniel FojtSkip any command-line file with a name suffix that matches the pattern 696*09d4459fSDaniel Fojt@var{glob}, using wildcard matching; a name suffix is either the whole 697*09d4459fSDaniel Fojtname, or a trailing part that starts with a non-slash character 698*09d4459fSDaniel Fojtimmediately after a slash (@samp{/}) in the name. 699*09d4459fSDaniel FojtWhen searching recursively, skip any subfile whose base 700dc7c36e4SJohn Marinoname matches @var{glob}; the base name is the part after the last 701*09d4459fSDaniel Fojtslash. A pattern can use 70295b7b453SJohn Marino@samp{*}, @samp{?}, and @samp{[}...@samp{]} as wildcards, 70395b7b453SJohn Marinoand @code{\} to quote a wildcard or backslash character literally. 70495b7b453SJohn Marino 70595b7b453SJohn Marino@item --exclude-from=@var{file} 70695b7b453SJohn Marino@opindex --exclude-from 70795b7b453SJohn Marino@cindex exclude files 70895b7b453SJohn Marino@cindex searching directory trees 709dc7c36e4SJohn MarinoSkip files whose name matches any of the patterns 71095b7b453SJohn Marinoread from @var{file} (using wildcard matching as described 711cf28ed85SJohn Marinounder @option{--exclude}). 71295b7b453SJohn Marino 713dc7c36e4SJohn Marino@item --exclude-dir=@var{glob} 71495b7b453SJohn Marino@opindex --exclude-dir 71595b7b453SJohn Marino@cindex exclude directories 716*09d4459fSDaniel FojtSkip any command-line directory with a name suffix that matches the 717*09d4459fSDaniel Fojtpattern @var{glob}. When searching recursively, skip any subdirectory 718*09d4459fSDaniel Fojtwhose base name matches @var{glob}. Ignore any redundant trailing 719*09d4459fSDaniel Fojtslashes in @var{glob}. 72095b7b453SJohn Marino 72195b7b453SJohn Marino@item -I 72295b7b453SJohn MarinoProcess a binary file as if it did not contain matching data; 72395b7b453SJohn Marinothis is equivalent to the @samp{--binary-files=without-match} option. 72495b7b453SJohn Marino 72595b7b453SJohn Marino@item --include=@var{glob} 72695b7b453SJohn Marino@opindex --include 72795b7b453SJohn Marino@cindex include files 72895b7b453SJohn Marino@cindex searching directory trees 729dc7c36e4SJohn MarinoSearch only files whose name matches @var{glob}, 730dc7c36e4SJohn Marinousing wildcard matching as described under @option{--exclude}. 73195b7b453SJohn Marino 73295b7b453SJohn Marino@item -r 73395b7b453SJohn Marino@itemx --recursive 73495b7b453SJohn Marino@opindex -r 73595b7b453SJohn Marino@opindex --recursive 73695b7b453SJohn Marino@cindex recursive search 73795b7b453SJohn Marino@cindex searching directory trees 738cf28ed85SJohn Marino@cindex symbolic links 739cf28ed85SJohn MarinoFor each directory operand, 74095b7b453SJohn Marinoread and process all files in that directory, recursively. 741cf28ed85SJohn MarinoFollow symbolic links on the command line, but skip symlinks 742cf28ed85SJohn Marinothat are encountered recursively. 743dc7c36e4SJohn MarinoNote that if no file operand is given, grep searches the working directory. 74495b7b453SJohn MarinoThis is the same as the @samp{--directories=recurse} option. 74595b7b453SJohn Marino 746cf28ed85SJohn Marino@item -R 747cf28ed85SJohn Marino@itemx --dereference-recursive 748cf28ed85SJohn Marino@opindex -R 749cf28ed85SJohn Marino@opindex --dereference-recursive 750cf28ed85SJohn Marino@cindex recursive search 751cf28ed85SJohn Marino@cindex searching directory trees 752cf28ed85SJohn Marino@cindex symbolic links 753cf28ed85SJohn MarinoFor each directory operand, read and process all files in that 754cf28ed85SJohn Marinodirectory, recursively, following all symbolic links. 755cf28ed85SJohn Marino 75695b7b453SJohn Marino@end table 75795b7b453SJohn Marino 75895b7b453SJohn Marino@node Other Options 75995b7b453SJohn Marino@subsection Other Options 76095b7b453SJohn Marino 761cf28ed85SJohn Marino@table @option 76295b7b453SJohn Marino 763*09d4459fSDaniel Fojt@item -- 764*09d4459fSDaniel Fojt@opindex -- 765*09d4459fSDaniel Fojt@cindex option delimiter 766*09d4459fSDaniel FojtDelimit the option list. Later arguments, if any, are treated as 767*09d4459fSDaniel Fojtoperands even if they begin with @samp{-}. For example, @samp{grep PAT -- 768*09d4459fSDaniel Fojt-file1 file2} searches for the pattern PAT in the files named @file{-file1} 769*09d4459fSDaniel Fojtand @file{file2}. 770*09d4459fSDaniel Fojt 77195b7b453SJohn Marino@item --line-buffered 77295b7b453SJohn Marino@opindex --line-buffered 77395b7b453SJohn Marino@cindex line buffering 77495b7b453SJohn MarinoUse line buffering on output. 77595b7b453SJohn MarinoThis can cause a performance penalty. 77695b7b453SJohn Marino 77795b7b453SJohn Marino@item -U 77895b7b453SJohn Marino@itemx --binary 77995b7b453SJohn Marino@opindex -U 78095b7b453SJohn Marino@opindex --binary 781*09d4459fSDaniel Fojt@cindex MS-Windows binary I/O 782*09d4459fSDaniel Fojt@cindex binary I/O 783*09d4459fSDaniel FojtOn platforms that distinguish between text and binary I/O, 784*09d4459fSDaniel Fojtuse the latter when reading and writing files other 785*09d4459fSDaniel Fojtthan the user's terminal, so that all input bytes are read and written 786*09d4459fSDaniel Fojtas-is. This overrides the default behavior where @command{grep} 787*09d4459fSDaniel Fojtfollows the operating system's advice whether to use text or binary 788*09d4459fSDaniel FojtI/O@. On MS-Windows when @command{grep} uses text I/O it reads a 789*09d4459fSDaniel Fojtcarriage return--newline pair as a newline and a Control-Z as 790*09d4459fSDaniel Fojtend-of-file, and it writes a newline as a carriage return--newline 791*09d4459fSDaniel Fojtpair. 792*09d4459fSDaniel Fojt 793*09d4459fSDaniel FojtWhen using text I/O @option{--byte-offset} (@option{-b}) counts and 794*09d4459fSDaniel Fojt@option{--binary-files} heuristics apply to input data after text-I/O 795*09d4459fSDaniel Fojtprocessing. Also, the @option{--binary-files} heuristics need not agree 796*09d4459fSDaniel Fojtwith the @option{--binary} option; that is, they may treat the data as 797*09d4459fSDaniel Fojttext even if @option{--binary} is given, or vice versa. 798*09d4459fSDaniel Fojt@xref{File and Directory Selection}. 799*09d4459fSDaniel Fojt 800*09d4459fSDaniel FojtThis option has no effect on GNU and other POSIX-compatible platforms, 801*09d4459fSDaniel Fojtwhich do not distinguish text from binary I/O. 80295b7b453SJohn Marino 80395b7b453SJohn Marino@item -z 80495b7b453SJohn Marino@itemx --null-data 80595b7b453SJohn Marino@opindex -z 80695b7b453SJohn Marino@opindex --null-data 80795b7b453SJohn Marino@cindex zero-terminated lines 808*09d4459fSDaniel FojtTreat input and output data as sequences of lines, each terminated by 809*09d4459fSDaniel Fojta zero byte (the ASCII NUL character) instead of a newline. 810cf28ed85SJohn MarinoLike the @option{-Z} or @option{--null} option, 81195b7b453SJohn Marinothis option can be used with commands like 81295b7b453SJohn Marino@samp{sort -z} to process arbitrary file names. 81395b7b453SJohn Marino 81495b7b453SJohn Marino@end table 81595b7b453SJohn Marino 81695b7b453SJohn Marino@node Environment Variables 81795b7b453SJohn Marino@section Environment Variables 81895b7b453SJohn Marino 81995b7b453SJohn MarinoThe behavior of @command{grep} is affected 82095b7b453SJohn Marinoby the following environment variables. 82195b7b453SJohn Marino 822dc7c36e4SJohn Marino@vindex LANGUAGE @r{environment variable} 823dc7c36e4SJohn Marino@vindex LC_ALL @r{environment variable} 824dc7c36e4SJohn Marino@vindex LC_MESSAGES @r{environment variable} 825dc7c36e4SJohn Marino@vindex LANG @r{environment variable} 82695b7b453SJohn MarinoThe locale for category @w{@code{LC_@var{foo}}} 82795b7b453SJohn Marinois specified by examining the three environment variables 82895b7b453SJohn Marino@env{LC_ALL}, @w{@env{LC_@var{foo}}}, and @env{LANG}, 82995b7b453SJohn Marinoin that order. 83095b7b453SJohn MarinoThe first of these variables that is set specifies the locale. 83195b7b453SJohn MarinoFor example, if @env{LC_ALL} is not set, 832dc7c36e4SJohn Marinobut @env{LC_COLLATE} is set to @samp{pt_BR}, 83395b7b453SJohn Marinothen the Brazilian Portuguese locale is used 834dc7c36e4SJohn Marinofor the @env{LC_COLLATE} category. 835dc7c36e4SJohn MarinoAs a special case for @env{LC_MESSAGES} only, the environment variable 836dc7c36e4SJohn Marino@env{LANGUAGE} can contain a colon-separated list of languages that 837dc7c36e4SJohn Marinooverrides the three environment variables that ordinarily specify 838dc7c36e4SJohn Marinothe @env{LC_MESSAGES} category. 83995b7b453SJohn MarinoThe @samp{C} locale is used if none of these environment variables are set, 84095b7b453SJohn Marinoif the locale catalog is not installed, 84195b7b453SJohn Marinoor if @command{grep} was not compiled 842cf28ed85SJohn Marinowith national language support (NLS). 843*09d4459fSDaniel FojtThe shell command @code{locale -a} lists locales that are currently available. 844cf28ed85SJohn Marino 845cf28ed85SJohn MarinoMany of the environment variables in the following list let you 846cf28ed85SJohn Marinocontrol highlighting using 847cf28ed85SJohn MarinoSelect Graphic Rendition (SGR) 848cf28ed85SJohn Marinocommands interpreted by the terminal or terminal emulator. 849cf28ed85SJohn Marino(See the 850cf28ed85SJohn Marinosection 851cf28ed85SJohn Marinoin the documentation of your text terminal 852cf28ed85SJohn Marinofor permitted values and their meanings as character attributes.) 853cf28ed85SJohn MarinoThese substring values are integers in decimal representation 854cf28ed85SJohn Marinoand can be concatenated with semicolons. 855cf28ed85SJohn Marino@command{grep} takes care of assembling the result 856cf28ed85SJohn Marinointo a complete SGR sequence (@samp{\33[}...@samp{m}). 857cf28ed85SJohn MarinoCommon values to concatenate include 858cf28ed85SJohn Marino@samp{1} for bold, 859cf28ed85SJohn Marino@samp{4} for underline, 860cf28ed85SJohn Marino@samp{5} for blink, 861cf28ed85SJohn Marino@samp{7} for inverse, 862cf28ed85SJohn Marino@samp{39} for default foreground color, 863cf28ed85SJohn Marino@samp{30} to @samp{37} for foreground colors, 864cf28ed85SJohn Marino@samp{90} to @samp{97} for 16-color mode foreground colors, 865cf28ed85SJohn Marino@samp{38;5;0} to @samp{38;5;255} 866cf28ed85SJohn Marinofor 88-color and 256-color modes foreground colors, 867cf28ed85SJohn Marino@samp{49} for default background color, 868cf28ed85SJohn Marino@samp{40} to @samp{47} for background colors, 869cf28ed85SJohn Marino@samp{100} to @samp{107} for 16-color mode background colors, 870cf28ed85SJohn Marinoand @samp{48;5;0} to @samp{48;5;255} 871cf28ed85SJohn Marinofor 88-color and 256-color modes background colors. 872cf28ed85SJohn Marino 873cf28ed85SJohn MarinoThe two-letter names used in the @env{GREP_COLORS} environment variable 874cf28ed85SJohn Marino(and some of the others) refer to terminal ``capabilities,'' the ability 875cf28ed85SJohn Marinoof a terminal to highlight text, or change its color, and so on. 876cf28ed85SJohn MarinoThese capabilities are stored in an online database and accessed by 877cf28ed85SJohn Marinothe @code{terminfo} library. 87895b7b453SJohn Marino 87995b7b453SJohn Marino@cindex environment variables 88095b7b453SJohn Marino 88195b7b453SJohn Marino@table @env 88295b7b453SJohn Marino 88395b7b453SJohn Marino@item GREP_OPTIONS 88495b7b453SJohn Marino@vindex GREP_OPTIONS @r{environment variable} 88595b7b453SJohn Marino@cindex default options environment variable 88695b7b453SJohn MarinoThis variable specifies default options to be placed in front of any 88795b7b453SJohn Marinoexplicit options. 888dc7c36e4SJohn MarinoAs this causes problems when writing portable scripts, this feature 889dc7c36e4SJohn Marinowill be removed in a future release of @command{grep}, and @command{grep} 890dc7c36e4SJohn Marinowarns if it is used. Please use an alias or script instead. 891dc7c36e4SJohn MarinoFor example, if @command{grep} is in the directory @samp{/usr/bin} you 892dc7c36e4SJohn Marinocan prepend @file{$HOME/bin} to your @env{PATH} and create an 893dc7c36e4SJohn Marinoexecutable script @file{$HOME/bin/grep} containing the following: 89495b7b453SJohn Marino 895dc7c36e4SJohn Marino@example 896dc7c36e4SJohn Marino#! /bin/sh 897dc7c36e4SJohn Marinoexport PATH=/usr/bin 898dc7c36e4SJohn Marinoexec grep --color=auto --devices=skip "$@@" 899dc7c36e4SJohn Marino@end example 900cf28ed85SJohn Marino 90195b7b453SJohn Marino@item GREP_COLOR 90295b7b453SJohn Marino@vindex GREP_COLOR @r{environment variable} 90395b7b453SJohn Marino@cindex highlight markers 90495b7b453SJohn MarinoThis variable specifies the color used to highlight matched (non-empty) text. 905cf28ed85SJohn MarinoIt is deprecated in favor of @env{GREP_COLORS}, but still supported. 906cf28ed85SJohn MarinoThe @samp{mt}, @samp{ms}, and @samp{mc} capabilities of @env{GREP_COLORS} 90795b7b453SJohn Marinohave priority over it. 90895b7b453SJohn MarinoIt can only specify the color used to highlight 90995b7b453SJohn Marinothe matching non-empty text in any matching line 910cf28ed85SJohn Marino(a selected line when the @option{-v} command-line option is omitted, 911cf28ed85SJohn Marinoor a context line when @option{-v} is specified). 91295b7b453SJohn MarinoThe default is @samp{01;31}, 91395b7b453SJohn Marinowhich means a bold red foreground text on the terminal's default background. 91495b7b453SJohn Marino 91595b7b453SJohn Marino@item GREP_COLORS 91695b7b453SJohn Marino@vindex GREP_COLORS @r{environment variable} 91795b7b453SJohn Marino@cindex highlight markers 91895b7b453SJohn MarinoThis variable specifies the colors and other attributes 91995b7b453SJohn Marinoused to highlight various parts of the output. 920cf28ed85SJohn MarinoIts value is a colon-separated list of @code{terminfo} capabilities 92195b7b453SJohn Marinothat defaults to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36} 92295b7b453SJohn Marinowith the @samp{rv} and @samp{ne} boolean capabilities omitted (i.e., false). 92395b7b453SJohn MarinoSupported capabilities are as follows. 92495b7b453SJohn Marino 92595b7b453SJohn Marino@table @code 92695b7b453SJohn Marino@item sl= 92795b7b453SJohn Marino@vindex sl GREP_COLORS @r{capability} 92895b7b453SJohn MarinoSGR substring for whole selected lines 92995b7b453SJohn Marino(i.e., 930cf28ed85SJohn Marinomatching lines when the @option{-v} command-line option is omitted, 931cf28ed85SJohn Marinoor non-matching lines when @option{-v} is specified). 93295b7b453SJohn MarinoIf however the boolean @samp{rv} capability 933cf28ed85SJohn Marinoand the @option{-v} command-line option are both specified, 93495b7b453SJohn Marinoit applies to context matching lines instead. 93595b7b453SJohn MarinoThe default is empty (i.e., the terminal's default color pair). 93695b7b453SJohn Marino 93795b7b453SJohn Marino@item cx= 93895b7b453SJohn Marino@vindex cx GREP_COLORS @r{capability} 93995b7b453SJohn MarinoSGR substring for whole context lines 94095b7b453SJohn Marino(i.e., 941cf28ed85SJohn Marinonon-matching lines when the @option{-v} command-line option is omitted, 942cf28ed85SJohn Marinoor matching lines when @option{-v} is specified). 94395b7b453SJohn MarinoIf however the boolean @samp{rv} capability 944cf28ed85SJohn Marinoand the @option{-v} command-line option are both specified, 94595b7b453SJohn Marinoit applies to selected non-matching lines instead. 94695b7b453SJohn MarinoThe default is empty (i.e., the terminal's default color pair). 94795b7b453SJohn Marino 94895b7b453SJohn Marino@item rv 94995b7b453SJohn Marino@vindex rv GREP_COLORS @r{capability} 95095b7b453SJohn MarinoBoolean value that reverses (swaps) the meanings of 95195b7b453SJohn Marinothe @samp{sl=} and @samp{cx=} capabilities 952cf28ed85SJohn Marinowhen the @option{-v} command-line option is specified. 95395b7b453SJohn MarinoThe default is false (i.e., the capability is omitted). 95495b7b453SJohn Marino 95595b7b453SJohn Marino@item mt=01;31 95695b7b453SJohn Marino@vindex mt GREP_COLORS @r{capability} 95795b7b453SJohn MarinoSGR substring for matching non-empty text in any matching line 95895b7b453SJohn Marino(i.e., 959cf28ed85SJohn Marinoa selected line when the @option{-v} command-line option is omitted, 960cf28ed85SJohn Marinoor a context line when @option{-v} is specified). 96195b7b453SJohn MarinoSetting this is equivalent to setting both @samp{ms=} and @samp{mc=} 96295b7b453SJohn Marinoat once to the same value. 96395b7b453SJohn MarinoThe default is a bold red text foreground over the current line background. 96495b7b453SJohn Marino 96595b7b453SJohn Marino@item ms=01;31 96695b7b453SJohn Marino@vindex ms GREP_COLORS @r{capability} 96795b7b453SJohn MarinoSGR substring for matching non-empty text in a selected line. 968cf28ed85SJohn Marino(This is used only when the @option{-v} command-line option is omitted.) 96995b7b453SJohn MarinoThe effect of the @samp{sl=} (or @samp{cx=} if @samp{rv}) capability 970cf28ed85SJohn Marinoremains active when this takes effect. 97195b7b453SJohn MarinoThe default is a bold red text foreground over the current line background. 97295b7b453SJohn Marino 97395b7b453SJohn Marino@item mc=01;31 97495b7b453SJohn Marino@vindex mc GREP_COLORS @r{capability} 97595b7b453SJohn MarinoSGR substring for matching non-empty text in a context line. 976cf28ed85SJohn Marino(This is used only when the @option{-v} command-line option is specified.) 97795b7b453SJohn MarinoThe effect of the @samp{cx=} (or @samp{sl=} if @samp{rv}) capability 978cf28ed85SJohn Marinoremains active when this takes effect. 97995b7b453SJohn MarinoThe default is a bold red text foreground over the current line background. 98095b7b453SJohn Marino 98195b7b453SJohn Marino@item fn=35 98295b7b453SJohn Marino@vindex fn GREP_COLORS @r{capability} 98395b7b453SJohn MarinoSGR substring for file names prefixing any content line. 98495b7b453SJohn MarinoThe default is a magenta text foreground over the terminal's default background. 98595b7b453SJohn Marino 98695b7b453SJohn Marino@item ln=32 98795b7b453SJohn Marino@vindex ln GREP_COLORS @r{capability} 98895b7b453SJohn MarinoSGR substring for line numbers prefixing any content line. 98995b7b453SJohn MarinoThe default is a green text foreground over the terminal's default background. 99095b7b453SJohn Marino 99195b7b453SJohn Marino@item bn=32 99295b7b453SJohn Marino@vindex bn GREP_COLORS @r{capability} 99395b7b453SJohn MarinoSGR substring for byte offsets prefixing any content line. 99495b7b453SJohn MarinoThe default is a green text foreground over the terminal's default background. 99595b7b453SJohn Marino 99695b7b453SJohn Marino@item se=36 99795b7b453SJohn Marino@vindex fn GREP_COLORS @r{capability} 99895b7b453SJohn MarinoSGR substring for separators that are inserted 99995b7b453SJohn Marinobetween selected line fields (@samp{:}), 100095b7b453SJohn Marinobetween context line fields (@samp{-}), 100195b7b453SJohn Marinoand between groups of adjacent lines 100295b7b453SJohn Marinowhen nonzero context is specified (@samp{--}). 100395b7b453SJohn MarinoThe default is a cyan text foreground over the terminal's default background. 100495b7b453SJohn Marino 100595b7b453SJohn Marino@item ne 100695b7b453SJohn Marino@vindex ne GREP_COLORS @r{capability} 100795b7b453SJohn MarinoBoolean value that prevents clearing to the end of line 100895b7b453SJohn Marinousing Erase in Line (EL) to Right (@samp{\33[K}) 100995b7b453SJohn Marinoeach time a colorized item ends. 101095b7b453SJohn MarinoThis is needed on terminals on which EL is not supported. 101195b7b453SJohn MarinoIt is otherwise useful on terminals 101295b7b453SJohn Marinofor which the @code{back_color_erase} 1013cf28ed85SJohn Marino(@code{bce}) boolean @code{terminfo} capability does not apply, 101495b7b453SJohn Marinowhen the chosen highlight colors do not affect the background, 101595b7b453SJohn Marinoor when EL is too slow or causes too much flicker. 101695b7b453SJohn MarinoThe default is false (i.e., the capability is omitted). 101795b7b453SJohn Marino@end table 101895b7b453SJohn Marino 101995b7b453SJohn MarinoNote that boolean capabilities have no @samp{=}... part. 102095b7b453SJohn MarinoThey are omitted (i.e., false) by default and become true when specified. 102195b7b453SJohn Marino 102295b7b453SJohn Marino 102395b7b453SJohn Marino@item LC_ALL 102495b7b453SJohn Marino@itemx LC_COLLATE 102595b7b453SJohn Marino@itemx LANG 102695b7b453SJohn Marino@vindex LC_ALL @r{environment variable} 102795b7b453SJohn Marino@vindex LC_COLLATE @r{environment variable} 102895b7b453SJohn Marino@vindex LANG @r{environment variable} 102995b7b453SJohn Marino@cindex character type 103095b7b453SJohn Marino@cindex national language support 103195b7b453SJohn Marino@cindex NLS 1032dc7c36e4SJohn MarinoThese variables specify the locale for the @env{LC_COLLATE} category, 1033680a9cb8SJohn Marinowhich might affect how range expressions like @samp{[a-z]} are 1034680a9cb8SJohn Marinointerpreted. 103595b7b453SJohn Marino 103695b7b453SJohn Marino@item LC_ALL 103795b7b453SJohn Marino@itemx LC_CTYPE 103895b7b453SJohn Marino@itemx LANG 103995b7b453SJohn Marino@vindex LC_ALL @r{environment variable} 104095b7b453SJohn Marino@vindex LC_CTYPE @r{environment variable} 104195b7b453SJohn Marino@vindex LANG @r{environment variable} 1042*09d4459fSDaniel Fojt@cindex encoding error 1043*09d4459fSDaniel Fojt@cindex null character 1044dc7c36e4SJohn MarinoThese variables specify the locale for the @env{LC_CTYPE} category, 104595b7b453SJohn Marinowhich determines the type of characters, 104695b7b453SJohn Marinoe.g., which characters are whitespace. 1047*09d4459fSDaniel FojtThis category also determines the character encoding, that is, whether 1048*09d4459fSDaniel Fojttext is encoded in UTF-8, ASCII, or some other encoding. In the 1049*09d4459fSDaniel Fojt@samp{C} or @samp{POSIX} locale, all characters are encoded as a 1050*09d4459fSDaniel Fojtsingle byte and every byte is a valid character. 1051*09d4459fSDaniel FojtIn more-complex encodings such as UTF-8, a sequence of multiple bytes 1052*09d4459fSDaniel Fojtmay be needed to represent a character, and some bytes may be encoding 1053*09d4459fSDaniel Fojterrors that do not contribute to the representation of any character. 1054*09d4459fSDaniel FojtPOSIX does not specify the behavior of @command{grep} when patterns or 1055*09d4459fSDaniel Fojtinput data contain encoding errors or null characters, so portable 1056*09d4459fSDaniel Fojtscripts should avoid such usage. As an extension to POSIX, GNU 1057*09d4459fSDaniel Fojt@command{grep} treats null characters like any other character. 1058*09d4459fSDaniel FojtHowever, unless the @option{-a} (@option{--binary-files=text}) option 1059*09d4459fSDaniel Fojtis used, the presence of null characters in input or of encoding 1060*09d4459fSDaniel Fojterrors in output causes GNU @command{grep} to treat the file as binary 1061*09d4459fSDaniel Fojtand suppress details about matches. @xref{File and Directory 1062*09d4459fSDaniel FojtSelection}. 106395b7b453SJohn Marino 1064dc7c36e4SJohn Marino@item LANGUAGE 1065dc7c36e4SJohn Marino@itemx LC_ALL 106695b7b453SJohn Marino@itemx LC_MESSAGES 106795b7b453SJohn Marino@itemx LANG 1068dc7c36e4SJohn Marino@vindex LANGUAGE @r{environment variable} 106995b7b453SJohn Marino@vindex LC_ALL @r{environment variable} 107095b7b453SJohn Marino@vindex LC_MESSAGES @r{environment variable} 107195b7b453SJohn Marino@vindex LANG @r{environment variable} 107295b7b453SJohn Marino@cindex language of messages 107395b7b453SJohn Marino@cindex message language 107495b7b453SJohn Marino@cindex national language support 107595b7b453SJohn Marino@cindex translation of message language 1076dc7c36e4SJohn MarinoThese variables specify the locale for the @env{LC_MESSAGES} category, 107795b7b453SJohn Marinowhich determines the language that @command{grep} uses for messages. 107895b7b453SJohn MarinoThe default @samp{C} locale uses American English messages. 107995b7b453SJohn Marino 108095b7b453SJohn Marino@item POSIXLY_CORRECT 108195b7b453SJohn Marino@vindex POSIXLY_CORRECT @r{environment variable} 1082cf28ed85SJohn MarinoIf set, @command{grep} behaves as POSIX requires; otherwise, 1083cf28ed85SJohn Marino@command{grep} behaves more like other GNU programs. 1084cf28ed85SJohn MarinoPOSIX 108595b7b453SJohn Marinorequires that options that 108695b7b453SJohn Marinofollow file names must be treated as file names; 108795b7b453SJohn Marinoby default, 108895b7b453SJohn Marinosuch options are permuted to the front of the operand list 108995b7b453SJohn Marinoand are treated as options. 1090dc7c36e4SJohn MarinoAlso, @env{POSIXLY_CORRECT} disables special handling of an 109195b7b453SJohn Marinoinvalid bracket expression. @xref{invalid-bracket-expr}. 109295b7b453SJohn Marino 109395b7b453SJohn Marino@item _@var{N}_GNU_nonoption_argv_flags_ 109495b7b453SJohn Marino@vindex _@var{N}_GNU_nonoption_argv_flags_ @r{environment variable} 109595b7b453SJohn Marino(Here @code{@var{N}} is @command{grep}'s numeric process ID.) 109695b7b453SJohn MarinoIf the @var{i}th character of this environment variable's value is @samp{1}, 109795b7b453SJohn Marinodo not consider the @var{i}th operand of @command{grep} to be an option, 109895b7b453SJohn Marinoeven if it appears to be one. 109995b7b453SJohn MarinoA shell can put this variable in the environment for each command it runs, 110095b7b453SJohn Marinospecifying which operands are the results of file name wildcard expansion 110195b7b453SJohn Marinoand therefore should not be treated as options. 1102cf28ed85SJohn MarinoThis behavior is available only with the GNU C library, 1103dc7c36e4SJohn Marinoand only when @env{POSIXLY_CORRECT} is not set. 110495b7b453SJohn Marino 110595b7b453SJohn Marino@end table 110695b7b453SJohn Marino 110795b7b453SJohn Marino 110895b7b453SJohn Marino@node Exit Status 110995b7b453SJohn Marino@section Exit Status 111095b7b453SJohn Marino@cindex exit status 111195b7b453SJohn Marino@cindex return status 111295b7b453SJohn Marino 1113dc7c36e4SJohn MarinoNormally the exit status is 0 if a line is selected, 1 if no lines 1114dc7c36e4SJohn Marinowere selected, and 2 if an error occurred. However, if the 1115*09d4459fSDaniel Fojt@option{-L} or @option{--files-without-match} is used, the exit status 1116*09d4459fSDaniel Fojtis 0 if a file is listed, 1 if no files were listed, and 2 if an error 1117*09d4459fSDaniel Fojtoccurred. Also, if the 1118dc7c36e4SJohn Marino@option{-q} or @option{--quiet} or @option{--silent} option is used 1119dc7c36e4SJohn Marinoand a line is selected, the exit status is 0 even if an error 1120dc7c36e4SJohn Marinooccurred. Other @command{grep} implementations may exit with status 1121dc7c36e4SJohn Marinogreater than 2 on error. 112295b7b453SJohn Marino 112395b7b453SJohn Marino@node grep Programs 112495b7b453SJohn Marino@section @command{grep} Programs 112595b7b453SJohn Marino@cindex @command{grep} programs 1126cf28ed85SJohn Marino@cindex variants of @command{grep} 112795b7b453SJohn Marino 112895b7b453SJohn Marino@command{grep} searches the named input files 1129*09d4459fSDaniel Fojtfor lines containing a match to the given patterns. 113095b7b453SJohn MarinoBy default, @command{grep} prints the matching lines. 1131cf28ed85SJohn MarinoA file named @file{-} stands for standard input. 1132cf28ed85SJohn MarinoIf no input is specified, @command{grep} searches the working 1133cf28ed85SJohn Marinodirectory @file{.} if given a command-line option specifying 1134cf28ed85SJohn Marinorecursion; otherwise, @command{grep} searches standard input. 113595b7b453SJohn MarinoThere are four major variants of @command{grep}, 113695b7b453SJohn Marinocontrolled by the following options. 113795b7b453SJohn Marino 1138cf28ed85SJohn Marino@table @option 113995b7b453SJohn Marino 114095b7b453SJohn Marino@item -G 114195b7b453SJohn Marino@itemx --basic-regexp 114295b7b453SJohn Marino@opindex -G 114395b7b453SJohn Marino@opindex --basic-regexp 114495b7b453SJohn Marino@cindex matching basic regular expressions 1145*09d4459fSDaniel FojtInterpret patterns as basic regular expressions (BREs). 114695b7b453SJohn MarinoThis is the default. 114795b7b453SJohn Marino 114895b7b453SJohn Marino@item -E 114995b7b453SJohn Marino@itemx --extended-regexp 115095b7b453SJohn Marino@opindex -E 115195b7b453SJohn Marino@opindex --extended-regexp 115295b7b453SJohn Marino@cindex matching extended regular expressions 1153*09d4459fSDaniel FojtInterpret patterns as extended regular expressions (EREs). 1154cf28ed85SJohn Marino(@option{-E} is specified by POSIX.) 115595b7b453SJohn Marino 115695b7b453SJohn Marino@item -F 115795b7b453SJohn Marino@itemx --fixed-strings 115895b7b453SJohn Marino@opindex -F 115995b7b453SJohn Marino@opindex --fixed-strings 116095b7b453SJohn Marino@cindex matching fixed strings 1161*09d4459fSDaniel FojtInterpret patterns as fixed strings, not regular expressions. 1162cf28ed85SJohn Marino(@option{-F} is specified by POSIX.) 116395b7b453SJohn Marino 116495b7b453SJohn Marino@item -P 116595b7b453SJohn Marino@itemx --perl-regexp 116695b7b453SJohn Marino@opindex -P 116795b7b453SJohn Marino@opindex --perl-regexp 1168*09d4459fSDaniel Fojt@cindex matching Perl-compatible regular expressions 1169*09d4459fSDaniel FojtInterpret patterns as Perl-compatible regular expressions (PCREs). 1170*09d4459fSDaniel FojtPCRE support is here to stay, but consider this option experimental when 1171*09d4459fSDaniel Fojtcombined with the @option{-z} (@option{--null-data}) option, and note that 117295b7b453SJohn Marino@samp{grep@ -P} may warn of unimplemented features. 1173*09d4459fSDaniel Fojt@xref{Other Options}. 117495b7b453SJohn Marino 117595b7b453SJohn Marino@end table 117695b7b453SJohn Marino 117795b7b453SJohn MarinoIn addition, 117895b7b453SJohn Marinotwo variant programs @command{egrep} and @command{fgrep} are available. 117995b7b453SJohn Marino@command{egrep} is the same as @samp{grep@ -E}. 118095b7b453SJohn Marino@command{fgrep} is the same as @samp{grep@ -F}. 118195b7b453SJohn MarinoDirect invocation as either 118295b7b453SJohn Marino@command{egrep} or @command{fgrep} is deprecated, 118395b7b453SJohn Marinobut is provided to allow historical applications 118495b7b453SJohn Marinothat rely on them to run unmodified. 118595b7b453SJohn Marino 118695b7b453SJohn Marino 118795b7b453SJohn Marino@node Regular Expressions 118895b7b453SJohn Marino@chapter Regular Expressions 118995b7b453SJohn Marino@cindex regular expressions 119095b7b453SJohn Marino 119195b7b453SJohn MarinoA @dfn{regular expression} is a pattern that describes a set of strings. 119295b7b453SJohn MarinoRegular expressions are constructed analogously to arithmetic expressions, 119395b7b453SJohn Marinoby using various operators to combine smaller expressions. 119495b7b453SJohn Marino@command{grep} understands 119595b7b453SJohn Marinothree different versions of regular expression syntax: 1196*09d4459fSDaniel Fojtbasic (BRE), extended (ERE), and Perl-compatible (PCRE). 1197cf28ed85SJohn MarinoIn GNU @command{grep}, 1198cf28ed85SJohn Marinothere is no difference in available functionality between the basic and 119995b7b453SJohn Marinoextended syntaxes. 120095b7b453SJohn MarinoIn other implementations, basic regular expressions are less powerful. 120195b7b453SJohn MarinoThe following description applies to extended regular expressions; 120295b7b453SJohn Marinodifferences for basic regular expressions are summarized afterwards. 1203*09d4459fSDaniel FojtPerl-compatible regular expressions give additional functionality, and 1204*09d4459fSDaniel Fojtare documented in the @i{pcresyntax}(3) and @i{pcrepattern}(3) manual 1205*09d4459fSDaniel Fojtpages, but work only if PCRE is available in the system. 120695b7b453SJohn Marino 120795b7b453SJohn Marino@menu 120895b7b453SJohn Marino* Fundamental Structure:: 120995b7b453SJohn Marino* Character Classes and Bracket Expressions:: 121095b7b453SJohn Marino* The Backslash Character and Special Expressions:: 121195b7b453SJohn Marino* Anchoring:: 121295b7b453SJohn Marino* Back-references and Subexpressions:: 121395b7b453SJohn Marino* Basic vs Extended:: 121495b7b453SJohn Marino@end menu 121595b7b453SJohn Marino 121695b7b453SJohn Marino@node Fundamental Structure 121795b7b453SJohn Marino@section Fundamental Structure 121895b7b453SJohn Marino 121995b7b453SJohn MarinoThe fundamental building blocks are the regular expressions that match 122095b7b453SJohn Marinoa single character. 122195b7b453SJohn MarinoMost characters, including all letters and digits, 122295b7b453SJohn Marinoare regular expressions that match themselves. 122395b7b453SJohn MarinoAny meta-character 122495b7b453SJohn Marinowith special meaning may be quoted by preceding it with a backslash. 122595b7b453SJohn Marino 122695b7b453SJohn Marino@opindex . 122795b7b453SJohn Marino@cindex dot 122895b7b453SJohn Marino@cindex period 122995b7b453SJohn MarinoThe period @samp{.} matches any single character. 1230*09d4459fSDaniel FojtIt is unspecified whether @samp{.} matches an encoding error. 1231*09d4459fSDaniel Fojt 1232*09d4459fSDaniel FojtA regular expression may be followed by one of several 1233*09d4459fSDaniel Fojtrepetition operators: 1234*09d4459fSDaniel Fojt 1235*09d4459fSDaniel Fojt@table @samp 123695b7b453SJohn Marino 123795b7b453SJohn Marino@item ? 123895b7b453SJohn Marino@opindex ? 123995b7b453SJohn Marino@cindex question mark 124095b7b453SJohn Marino@cindex match expression at most once 124195b7b453SJohn MarinoThe preceding item is optional and will be matched at most once. 124295b7b453SJohn Marino 124395b7b453SJohn Marino@item * 124495b7b453SJohn Marino@opindex * 124595b7b453SJohn Marino@cindex asterisk 124695b7b453SJohn Marino@cindex match expression zero or more times 124795b7b453SJohn MarinoThe preceding item will be matched zero or more times. 124895b7b453SJohn Marino 124995b7b453SJohn Marino@item + 125095b7b453SJohn Marino@opindex + 125195b7b453SJohn Marino@cindex plus sign 125295b7b453SJohn Marino@cindex match expression one or more times 125395b7b453SJohn MarinoThe preceding item will be matched one or more times. 125495b7b453SJohn Marino 125595b7b453SJohn Marino@item @{@var{n}@} 125695b7b453SJohn Marino@opindex @{@var{n}@} 125795b7b453SJohn Marino@cindex braces, one argument 125895b7b453SJohn Marino@cindex match expression @var{n} times 125995b7b453SJohn MarinoThe preceding item is matched exactly @var{n} times. 126095b7b453SJohn Marino 126195b7b453SJohn Marino@item @{@var{n},@} 126295b7b453SJohn Marino@opindex @{@var{n},@} 126395b7b453SJohn Marino@cindex braces, second argument omitted 126495b7b453SJohn Marino@cindex match expression @var{n} or more times 126595b7b453SJohn MarinoThe preceding item is matched @var{n} or more times. 126695b7b453SJohn Marino 126795b7b453SJohn Marino@item @{,@var{m}@} 126895b7b453SJohn Marino@opindex @{,@var{m}@} 126995b7b453SJohn Marino@cindex braces, first argument omitted 127095b7b453SJohn Marino@cindex match expression at most @var{m} times 127195b7b453SJohn MarinoThe preceding item is matched at most @var{m} times. 1272680a9cb8SJohn MarinoThis is a GNU extension. 127395b7b453SJohn Marino 127495b7b453SJohn Marino@item @{@var{n},@var{m}@} 127595b7b453SJohn Marino@opindex @{@var{n},@var{m}@} 127695b7b453SJohn Marino@cindex braces, two arguments 127795b7b453SJohn Marino@cindex match expression from @var{n} to @var{m} times 127895b7b453SJohn MarinoThe preceding item is matched at least @var{n} times, but not more than 127995b7b453SJohn Marino@var{m} times. 128095b7b453SJohn Marino 128195b7b453SJohn Marino@end table 128295b7b453SJohn Marino 1283cf28ed85SJohn MarinoThe empty regular expression matches the empty string. 128495b7b453SJohn MarinoTwo regular expressions may be concatenated; 128595b7b453SJohn Marinothe resulting regular expression 128695b7b453SJohn Marinomatches any string formed by concatenating two substrings 128795b7b453SJohn Marinothat respectively match the concatenated expressions. 128895b7b453SJohn Marino 128995b7b453SJohn MarinoTwo regular expressions may be joined by the infix operator @samp{|}; 129095b7b453SJohn Marinothe resulting regular expression 1291cf28ed85SJohn Marinomatches any string matching either alternate expression. 129295b7b453SJohn Marino 129395b7b453SJohn MarinoRepetition takes precedence over concatenation, 129495b7b453SJohn Marinowhich in turn takes precedence over alternation. 129595b7b453SJohn MarinoA whole expression may be enclosed in parentheses 129695b7b453SJohn Marinoto override these precedence rules and form a subexpression. 1297dc7c36e4SJohn MarinoAn unmatched @samp{)} matches just itself. 129895b7b453SJohn Marino 129995b7b453SJohn Marino@node Character Classes and Bracket Expressions 130095b7b453SJohn Marino@section Character Classes and Bracket Expressions 130195b7b453SJohn Marino 130295b7b453SJohn Marino@cindex bracket expression 130395b7b453SJohn Marino@cindex character class 130495b7b453SJohn MarinoA @dfn{bracket expression} is a list of characters enclosed by @samp{[} and 130595b7b453SJohn Marino@samp{]}. 1306*09d4459fSDaniel FojtIt matches any single character in that list. 1307*09d4459fSDaniel FojtIf the first character of the list is the caret @samp{^}, 1308*09d4459fSDaniel Fojtthen it matches any character @strong{not} in the list, 1309*09d4459fSDaniel Fojtand it is unspecified whether it matches an encoding error. 131095b7b453SJohn MarinoFor example, the regular expression 1311*09d4459fSDaniel Fojt@samp{[0123456789]} matches any single digit, 1312*09d4459fSDaniel Fojtwhereas @samp{[^()]} matches any single character that is not 1313*09d4459fSDaniel Fojtan opening or closing parenthesis, and might or might not match an 1314*09d4459fSDaniel Fojtencoding error. 131595b7b453SJohn Marino 131695b7b453SJohn Marino@cindex range expression 131795b7b453SJohn MarinoWithin a bracket expression, a @dfn{range expression} consists of two 131895b7b453SJohn Marinocharacters separated by a hyphen. 131995b7b453SJohn MarinoIt matches any single character that 1320680a9cb8SJohn Marinosorts between the two characters, inclusive. 1321680a9cb8SJohn MarinoIn the default C locale, the sorting sequence is the native character 1322680a9cb8SJohn Marinoorder; for example, @samp{[a-d]} is equivalent to @samp{[abcd]}. 1323680a9cb8SJohn MarinoIn other locales, the sorting sequence is not specified, and 1324680a9cb8SJohn Marino@samp{[a-d]} might be equivalent to @samp{[abcd]} or to 1325680a9cb8SJohn Marino@samp{[aBbCcDd]}, or it might fail to match any character, or the set of 1326680a9cb8SJohn Marinocharacters that it matches might even be erratic. 132795b7b453SJohn MarinoTo obtain the traditional interpretation 132895b7b453SJohn Marinoof bracket expressions, you can use the @samp{C} locale by setting the 132995b7b453SJohn Marino@env{LC_ALL} environment variable to the value @samp{C}. 133095b7b453SJohn Marino 133195b7b453SJohn MarinoFinally, certain named classes of characters are predefined within 133295b7b453SJohn Marinobracket expressions, as follows. 1333dc7c36e4SJohn MarinoTheir interpretation depends on the @env{LC_CTYPE} locale; 1334cf28ed85SJohn Marinofor example, @samp{[[:alnum:]]} means the character class of numbers and letters 1335cf28ed85SJohn Marinoin the current locale. 133695b7b453SJohn Marino 133795b7b453SJohn Marino@cindex classes of characters 133895b7b453SJohn Marino@cindex character classes 133995b7b453SJohn Marino@table @samp 134095b7b453SJohn Marino 134195b7b453SJohn Marino@item [:alnum:] 134295b7b453SJohn Marino@opindex alnum @r{character class} 134395b7b453SJohn Marino@cindex alphanumeric characters 134495b7b453SJohn MarinoAlphanumeric characters: 1345dc7c36e4SJohn Marino@samp{[:alpha:]} and @samp{[:digit:]}; in the @samp{C} locale and ASCII 1346dc7c36e4SJohn Marinocharacter encoding, this is the same as @samp{[0-9A-Za-z]}. 134795b7b453SJohn Marino 134895b7b453SJohn Marino@item [:alpha:] 134995b7b453SJohn Marino@opindex alpha @r{character class} 135095b7b453SJohn Marino@cindex alphabetic characters 135195b7b453SJohn MarinoAlphabetic characters: 1352dc7c36e4SJohn Marino@samp{[:lower:]} and @samp{[:upper:]}; in the @samp{C} locale and ASCII 1353dc7c36e4SJohn Marinocharacter encoding, this is the same as @samp{[A-Za-z]}. 135495b7b453SJohn Marino 135595b7b453SJohn Marino@item [:blank:] 135695b7b453SJohn Marino@opindex blank @r{character class} 135795b7b453SJohn Marino@cindex blank characters 135895b7b453SJohn MarinoBlank characters: 135995b7b453SJohn Marinospace and tab. 136095b7b453SJohn Marino 136195b7b453SJohn Marino@item [:cntrl:] 136295b7b453SJohn Marino@opindex cntrl @r{character class} 136395b7b453SJohn Marino@cindex control characters 136495b7b453SJohn MarinoControl characters. 1365cf28ed85SJohn MarinoIn ASCII, these characters have octal codes 000 1366dc7c36e4SJohn Marinothrough 037, and 177 (DEL). 136795b7b453SJohn MarinoIn other character sets, these are 136895b7b453SJohn Marinothe equivalent characters, if any. 136995b7b453SJohn Marino 137095b7b453SJohn Marino@item [:digit:] 137195b7b453SJohn Marino@opindex digit @r{character class} 137295b7b453SJohn Marino@cindex digit characters 137395b7b453SJohn Marino@cindex numeric characters 137495b7b453SJohn MarinoDigits: @code{0 1 2 3 4 5 6 7 8 9}. 137595b7b453SJohn Marino 137695b7b453SJohn Marino@item [:graph:] 137795b7b453SJohn Marino@opindex graph @r{character class} 137895b7b453SJohn Marino@cindex graphic characters 137995b7b453SJohn MarinoGraphical characters: 138095b7b453SJohn Marino@samp{[:alnum:]} and @samp{[:punct:]}. 138195b7b453SJohn Marino 138295b7b453SJohn Marino@item [:lower:] 138395b7b453SJohn Marino@opindex lower @r{character class} 138495b7b453SJohn Marino@cindex lower-case letters 1385cf28ed85SJohn MarinoLower-case letters; in the @samp{C} locale and ASCII character 1386cf28ed85SJohn Marinoencoding, this is 138795b7b453SJohn Marino@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}. 138895b7b453SJohn Marino 138995b7b453SJohn Marino@item [:print:] 139095b7b453SJohn Marino@opindex print @r{character class} 139195b7b453SJohn Marino@cindex printable characters 139295b7b453SJohn MarinoPrintable characters: 139395b7b453SJohn Marino@samp{[:alnum:]}, @samp{[:punct:]}, and space. 139495b7b453SJohn Marino 139595b7b453SJohn Marino@item [:punct:] 139695b7b453SJohn Marino@opindex punct @r{character class} 139795b7b453SJohn Marino@cindex punctuation characters 1398cf28ed85SJohn MarinoPunctuation characters; in the @samp{C} locale and ASCII character 1399cf28ed85SJohn Marinoencoding, this is 140095b7b453SJohn Marino@code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}. 140195b7b453SJohn Marino 140295b7b453SJohn Marino@item [:space:] 140395b7b453SJohn Marino@opindex space @r{character class} 140495b7b453SJohn Marino@cindex space characters 140595b7b453SJohn Marino@cindex whitespace characters 1406cf28ed85SJohn MarinoSpace characters: in the @samp{C} locale, this is 140795b7b453SJohn Marinotab, newline, vertical tab, form feed, carriage return, and space. 140895b7b453SJohn Marino@xref{Usage}, for more discussion of matching newlines. 140995b7b453SJohn Marino 141095b7b453SJohn Marino@item [:upper:] 141195b7b453SJohn Marino@opindex upper @r{character class} 141295b7b453SJohn Marino@cindex upper-case letters 1413cf28ed85SJohn MarinoUpper-case letters: in the @samp{C} locale and ASCII character 1414cf28ed85SJohn Marinoencoding, this is 141595b7b453SJohn Marino@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}. 141695b7b453SJohn Marino 141795b7b453SJohn Marino@item [:xdigit:] 141895b7b453SJohn Marino@opindex xdigit @r{character class} 141995b7b453SJohn Marino@cindex xdigit class 142095b7b453SJohn Marino@cindex hexadecimal digits 142195b7b453SJohn MarinoHexadecimal digits: 142295b7b453SJohn Marino@code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}. 142395b7b453SJohn Marino 142495b7b453SJohn Marino@end table 1425cf28ed85SJohn MarinoNote that the brackets in these class names are 142695b7b453SJohn Marinopart of the symbolic names, and must be included in addition to 1427cf28ed85SJohn Marinothe brackets delimiting the bracket expression. 142895b7b453SJohn Marino 142995b7b453SJohn Marino@anchor{invalid-bracket-expr} 143095b7b453SJohn MarinoIf you mistakenly omit the outer brackets, and search for say, @samp{[:upper:]}, 143195b7b453SJohn MarinoGNU @command{grep} prints a diagnostic and exits with status 2, on 143295b7b453SJohn Marinothe assumption that you did not intend to search for the nominally 143395b7b453SJohn Marinoequivalent regular expression: @samp{[:epru]}. 1434dc7c36e4SJohn MarinoSet the @env{POSIXLY_CORRECT} environment variable to disable this feature. 143595b7b453SJohn Marino 143695b7b453SJohn MarinoMost meta-characters lose their special meaning inside bracket expressions. 143795b7b453SJohn Marino 143895b7b453SJohn Marino@table @samp 143995b7b453SJohn Marino@item ] 144095b7b453SJohn Marinoends the bracket expression if it's not the first list item. 144195b7b453SJohn MarinoSo, if you want to make the @samp{]} character a list item, 144295b7b453SJohn Marinoyou must put it first. 144395b7b453SJohn Marino 144495b7b453SJohn Marino@item [. 144595b7b453SJohn Marinorepresents the open collating symbol. 144695b7b453SJohn Marino 144795b7b453SJohn Marino@item .] 144895b7b453SJohn Marinorepresents the close collating symbol. 144995b7b453SJohn Marino 145095b7b453SJohn Marino@item [= 145195b7b453SJohn Marinorepresents the open equivalence class. 145295b7b453SJohn Marino 145395b7b453SJohn Marino@item =] 145495b7b453SJohn Marinorepresents the close equivalence class. 145595b7b453SJohn Marino 145695b7b453SJohn Marino@item [: 1457dc7c36e4SJohn Marinorepresents the open character class symbol, and should be followed by a 1458dc7c36e4SJohn Marinovalid character class name. 145995b7b453SJohn Marino 146095b7b453SJohn Marino@item :] 146195b7b453SJohn Marinorepresents the close character class symbol. 146295b7b453SJohn Marino 146395b7b453SJohn Marino@item - 146495b7b453SJohn Marinorepresents the range if it's not first or last in a list or the ending point 146595b7b453SJohn Marinoof a range. 146695b7b453SJohn Marino 146795b7b453SJohn Marino@item ^ 146895b7b453SJohn Marinorepresents the characters not in the list. 146995b7b453SJohn MarinoIf you want to make the @samp{^} 147095b7b453SJohn Marinocharacter a list item, place it anywhere but first. 147195b7b453SJohn Marino 147295b7b453SJohn Marino@end table 147395b7b453SJohn Marino 147495b7b453SJohn Marino@node The Backslash Character and Special Expressions 147595b7b453SJohn Marino@section The Backslash Character and Special Expressions 147695b7b453SJohn Marino@cindex backslash 147795b7b453SJohn Marino 147895b7b453SJohn MarinoThe @samp{\} character, 147995b7b453SJohn Marinowhen followed by certain ordinary characters, 148095b7b453SJohn Marinotakes a special meaning: 148195b7b453SJohn Marino 148295b7b453SJohn Marino@table @samp 148395b7b453SJohn Marino 1484cf28ed85SJohn Marino@item \b 148595b7b453SJohn MarinoMatch the empty string at the edge of a word. 148695b7b453SJohn Marino 1487cf28ed85SJohn Marino@item \B 148895b7b453SJohn MarinoMatch the empty string provided it's not at the edge of a word. 148995b7b453SJohn Marino 1490cf28ed85SJohn Marino@item \< 149195b7b453SJohn MarinoMatch the empty string at the beginning of word. 149295b7b453SJohn Marino 1493cf28ed85SJohn Marino@item \> 149495b7b453SJohn MarinoMatch the empty string at the end of word. 149595b7b453SJohn Marino 1496cf28ed85SJohn Marino@item \w 1497cf28ed85SJohn MarinoMatch word constituent, it is a synonym for @samp{[_[:alnum:]]}. 149895b7b453SJohn Marino 1499cf28ed85SJohn Marino@item \W 1500cf28ed85SJohn MarinoMatch non-word constituent, it is a synonym for @samp{[^_[:alnum:]]}. 150195b7b453SJohn Marino 1502cf28ed85SJohn Marino@item \s 150395b7b453SJohn MarinoMatch whitespace, it is a synonym for @samp{[[:space:]]}. 150495b7b453SJohn Marino 1505cf28ed85SJohn Marino@item \S 150695b7b453SJohn MarinoMatch non-whitespace, it is a synonym for @samp{[^[:space:]]}. 150795b7b453SJohn Marino 150895b7b453SJohn Marino@end table 150995b7b453SJohn Marino 151095b7b453SJohn MarinoFor example, @samp{\brat\b} matches the separate word @samp{rat}, 151195b7b453SJohn Marino@samp{\Brat\B} matches @samp{crate} but not @samp{furry rat}. 151295b7b453SJohn Marino 151395b7b453SJohn Marino@node Anchoring 151495b7b453SJohn Marino@section Anchoring 151595b7b453SJohn Marino@cindex anchoring 151695b7b453SJohn Marino 151795b7b453SJohn MarinoThe caret @samp{^} and the dollar sign @samp{$} are meta-characters that 151895b7b453SJohn Marinorespectively match the empty string at the beginning and end of a line. 1519cf28ed85SJohn MarinoThey are termed @dfn{anchors}, since they force the match to be ``anchored'' 1520cf28ed85SJohn Marinoto beginning or end of a line, respectively. 152195b7b453SJohn Marino 152295b7b453SJohn Marino@node Back-references and Subexpressions 152395b7b453SJohn Marino@section Back-references and Subexpressions 152495b7b453SJohn Marino@cindex subexpression 152595b7b453SJohn Marino@cindex back-reference 152695b7b453SJohn Marino 152795b7b453SJohn MarinoThe back-reference @samp{\@var{n}}, where @var{n} is a single digit, matches 152895b7b453SJohn Marinothe substring previously matched by the @var{n}th parenthesized subexpression 152995b7b453SJohn Marinoof the regular expression. 153095b7b453SJohn MarinoFor example, @samp{(a)\1} matches @samp{aa}. 153195b7b453SJohn MarinoWhen used with alternation, if the group does not participate in the match then 153295b7b453SJohn Marinothe back-reference makes the whole match fail. 153395b7b453SJohn MarinoFor example, @samp{a(.)|b\1} 153495b7b453SJohn Marinowill not match @samp{ba}. 153595b7b453SJohn MarinoWhen multiple regular expressions are given with 1536cf28ed85SJohn Marino@option{-e} or from a file (@samp{-f @var{file}}), 153795b7b453SJohn Marinoback-references are local to each expression. 153895b7b453SJohn Marino 1539*09d4459fSDaniel Fojt@xref{Known Bugs}, for some known problems with back-references. 1540*09d4459fSDaniel Fojt 154195b7b453SJohn Marino@node Basic vs Extended 154295b7b453SJohn Marino@section Basic vs Extended Regular Expressions 154395b7b453SJohn Marino@cindex basic regular expressions 154495b7b453SJohn Marino 154595b7b453SJohn MarinoIn basic regular expressions the meta-characters @samp{?}, @samp{+}, 154695b7b453SJohn Marino@samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning; 154795b7b453SJohn Marinoinstead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{}, 154895b7b453SJohn Marino@samp{\|}, @samp{\(}, and @samp{\)}. 154995b7b453SJohn Marino 155095b7b453SJohn Marino@cindex interval specifications 155195b7b453SJohn MarinoTraditional @command{egrep} did not support the @samp{@{} meta-character, 155295b7b453SJohn Marinoand some @command{egrep} implementations support @samp{\@{} instead, so 155395b7b453SJohn Marinoportable scripts should avoid @samp{@{} in @samp{grep@ -E} patterns and 155495b7b453SJohn Marinoshould use @samp{[@{]} to match a literal @samp{@{}. 155595b7b453SJohn Marino 1556cf28ed85SJohn MarinoGNU @command{grep@ -E} attempts to support traditional usage by 155795b7b453SJohn Marinoassuming that @samp{@{} is not special if it would be the start of an 155895b7b453SJohn Marinoinvalid interval specification. 155995b7b453SJohn MarinoFor example, the command 156095b7b453SJohn Marino@samp{grep@ -E@ '@{1'} searches for the two-character string @samp{@{1} 156195b7b453SJohn Marinoinstead of reporting a syntax error in the regular expression. 1562cf28ed85SJohn MarinoPOSIX allows this behavior as an extension, but portable scripts 156395b7b453SJohn Marinoshould avoid it. 156495b7b453SJohn Marino 156595b7b453SJohn Marino 156695b7b453SJohn Marino@node Usage 156795b7b453SJohn Marino@chapter Usage 156895b7b453SJohn Marino 156995b7b453SJohn Marino@cindex usage, examples 1570cf28ed85SJohn MarinoHere is an example command that invokes GNU @command{grep}: 157195b7b453SJohn Marino 157295b7b453SJohn Marino@example 157395b7b453SJohn Marinogrep -i 'hello.*world' menu.h main.c 157495b7b453SJohn Marino@end example 157595b7b453SJohn Marino 157695b7b453SJohn Marino@noindent 157795b7b453SJohn MarinoThis lists all lines in the files @file{menu.h} and @file{main.c} that 157895b7b453SJohn Marinocontain the string @samp{hello} followed by the string @samp{world}; 157995b7b453SJohn Marinothis is because @samp{.*} matches zero or more characters within a line. 158095b7b453SJohn Marino@xref{Regular Expressions}. 1581cf28ed85SJohn MarinoThe @option{-i} option causes @command{grep} 158295b7b453SJohn Marinoto ignore case, causing it to match the line @samp{Hello, world!}, which 158395b7b453SJohn Marinoit would not otherwise match. 1584*09d4459fSDaniel Fojt 1585*09d4459fSDaniel FojtHere is a more complex example session, 1586*09d4459fSDaniel Fojtshowing the location and contents of any line 1587*09d4459fSDaniel Fojtcontaining @samp{f} and ending in @samp{.c}, 1588*09d4459fSDaniel Fojtwithin all files in the current directory whose names 1589*09d4459fSDaniel Fojtcontain @samp{g} and end in @samp{.h}. 1590*09d4459fSDaniel FojtThe @option{-n} option outputs line numbers, the @option{--} argument 1591*09d4459fSDaniel Fojttreats any later arguments starting with @samp{-} as file names not 1592*09d4459fSDaniel Fojtoptions, and the empty file @file{/dev/null} causes file names to be output 1593*09d4459fSDaniel Fojteven if only one file name happens to be of the form @samp{*g*.h}. 1594*09d4459fSDaniel Fojt 1595*09d4459fSDaniel Fojt@example 1596*09d4459fSDaniel Fojt$ @kbd{grep -n -- 'f.*\.c$' *g*.h /dev/null} 1597*09d4459fSDaniel Fojtargmatch.h:1:/* definitions and prototypes for argmatch.c 1598*09d4459fSDaniel Fojt@end example 1599*09d4459fSDaniel Fojt 1600*09d4459fSDaniel Fojt@noindent 1601*09d4459fSDaniel FojtThe only line that contains a match is line 1 of @file{argmatch.h}. 1602*09d4459fSDaniel FojtNote that the regular expression syntax used in the pattern differs 1603*09d4459fSDaniel Fojtfrom the globbing syntax that the shell uses to match file names. 1604*09d4459fSDaniel Fojt 160595b7b453SJohn Marino@xref{Invoking}, for more details about 160695b7b453SJohn Marinohow to invoke @command{grep}. 160795b7b453SJohn Marino 160895b7b453SJohn Marino@cindex using @command{grep}, Q&A 160995b7b453SJohn Marino@cindex FAQ about @command{grep} usage 161095b7b453SJohn MarinoHere are some common questions and answers about @command{grep} usage. 161195b7b453SJohn Marino 161295b7b453SJohn Marino@enumerate 161395b7b453SJohn Marino 161495b7b453SJohn Marino@item 161595b7b453SJohn MarinoHow can I list just the names of matching files? 161695b7b453SJohn Marino 161795b7b453SJohn Marino@example 1618*09d4459fSDaniel Fojtgrep -l 'main' test-*.c 161995b7b453SJohn Marino@end example 162095b7b453SJohn Marino 162195b7b453SJohn Marino@noindent 1622*09d4459fSDaniel Fojtlists names of @samp{test-*.c} files in the current directory whose contents 162395b7b453SJohn Marinomention @samp{main}. 162495b7b453SJohn Marino 162595b7b453SJohn Marino@item 162695b7b453SJohn MarinoHow do I search directories recursively? 162795b7b453SJohn Marino 162895b7b453SJohn Marino@example 162995b7b453SJohn Marinogrep -r 'hello' /home/gigi 163095b7b453SJohn Marino@end example 163195b7b453SJohn Marino 163295b7b453SJohn Marino@noindent 163395b7b453SJohn Marinosearches for @samp{hello} in all files 163495b7b453SJohn Marinounder the @file{/home/gigi} directory. 163595b7b453SJohn MarinoFor more control over which files are searched, 1636*09d4459fSDaniel Fojtuse @command{find} and @command{grep}. 163795b7b453SJohn MarinoFor example, the following command searches only C files: 163895b7b453SJohn Marino 163995b7b453SJohn Marino@example 1640*09d4459fSDaniel Fojtfind /home/gigi -name '*.c' ! -type d \ 1641*09d4459fSDaniel Fojt -exec grep -H 'hello' '@{@}' + 164295b7b453SJohn Marino@end example 164395b7b453SJohn Marino 164495b7b453SJohn MarinoThis differs from the command: 164595b7b453SJohn Marino 164695b7b453SJohn Marino@example 1647*09d4459fSDaniel Fojtgrep -H 'hello' /home/gigi/*.c 164895b7b453SJohn Marino@end example 164995b7b453SJohn Marino 1650*09d4459fSDaniel Fojtwhich merely looks for @samp{hello} in non-hidden C files in 1651*09d4459fSDaniel Fojt@file{/home/gigi} whose names end in @samp{.c}. 1652*09d4459fSDaniel FojtThe @command{find} command line above is more similar to the command: 165395b7b453SJohn Marino 165495b7b453SJohn Marino@example 1655*09d4459fSDaniel Fojtgrep -r --include='*.c' 'hello' /home/gigi 165695b7b453SJohn Marino@end example 165795b7b453SJohn Marino 165895b7b453SJohn Marino@item 1659*09d4459fSDaniel FojtWhat if a pattern or file has a leading @samp{-}? 166095b7b453SJohn Marino 166195b7b453SJohn Marino@example 1662*09d4459fSDaniel Fojtgrep -- '--cut here--' * 166395b7b453SJohn Marino@end example 166495b7b453SJohn Marino 166595b7b453SJohn Marino@noindent 166695b7b453SJohn Marinosearches for all lines matching @samp{--cut here--}. 1667*09d4459fSDaniel FojtWithout @option{--}, 166895b7b453SJohn Marino@command{grep} would attempt to parse @samp{--cut here--} as a list of 1669*09d4459fSDaniel Fojtoptions, and there would be similar problems with any file names 1670*09d4459fSDaniel Fojtbeginning with @samp{-}. 1671*09d4459fSDaniel Fojt 1672*09d4459fSDaniel FojtAlternatively, you can prevent misinterpretation of leading @samp{-} 1673*09d4459fSDaniel Fojtby using @option{-e} for patterns and leading @samp{./} for files: 1674*09d4459fSDaniel Fojt 1675*09d4459fSDaniel Fojt@example 1676*09d4459fSDaniel Fojtgrep -e '--cut here--' ./* 1677*09d4459fSDaniel Fojt@end example 167895b7b453SJohn Marino 167995b7b453SJohn Marino@item 168095b7b453SJohn MarinoSuppose I want to search for a whole word, not a part of a word? 168195b7b453SJohn Marino 168295b7b453SJohn Marino@example 1683*09d4459fSDaniel Fojtgrep -w 'hello' test*.log 168495b7b453SJohn Marino@end example 168595b7b453SJohn Marino 168695b7b453SJohn Marino@noindent 168795b7b453SJohn Marinosearches only for instances of @samp{hello} that are entire words; 168895b7b453SJohn Marinoit does not match @samp{Othello}. 168995b7b453SJohn MarinoFor more control, use @samp{\<} and 169095b7b453SJohn Marino@samp{\>} to match the start and end of words. 169195b7b453SJohn MarinoFor example: 169295b7b453SJohn Marino 169395b7b453SJohn Marino@example 1694*09d4459fSDaniel Fojtgrep 'hello\>' test*.log 169595b7b453SJohn Marino@end example 169695b7b453SJohn Marino 169795b7b453SJohn Marino@noindent 169895b7b453SJohn Marinosearches only for words ending in @samp{hello}, so it matches the word 169995b7b453SJohn Marino@samp{Othello}. 170095b7b453SJohn Marino 170195b7b453SJohn Marino@item 170295b7b453SJohn MarinoHow do I output context around the matching lines? 170395b7b453SJohn Marino 170495b7b453SJohn Marino@example 1705*09d4459fSDaniel Fojtgrep -C 2 'hello' test*.log 170695b7b453SJohn Marino@end example 170795b7b453SJohn Marino 170895b7b453SJohn Marino@noindent 170995b7b453SJohn Marinoprints two lines of context around each matching line. 171095b7b453SJohn Marino 171195b7b453SJohn Marino@item 171295b7b453SJohn MarinoHow do I force @command{grep} to print the name of the file? 171395b7b453SJohn Marino 171495b7b453SJohn MarinoAppend @file{/dev/null}: 171595b7b453SJohn Marino 171695b7b453SJohn Marino@example 171795b7b453SJohn Marinogrep 'eli' /etc/passwd /dev/null 171895b7b453SJohn Marino@end example 171995b7b453SJohn Marino 172095b7b453SJohn Marinogets you: 172195b7b453SJohn Marino 172295b7b453SJohn Marino@example 172395b7b453SJohn Marino/etc/passwd:eli:x:2098:1000:Eli Smith:/home/eli:/bin/bash 172495b7b453SJohn Marino@end example 172595b7b453SJohn Marino 1726cf28ed85SJohn MarinoAlternatively, use @option{-H}, which is a GNU extension: 172795b7b453SJohn Marino 172895b7b453SJohn Marino@example 172995b7b453SJohn Marinogrep -H 'eli' /etc/passwd 173095b7b453SJohn Marino@end example 173195b7b453SJohn Marino 173295b7b453SJohn Marino@item 173395b7b453SJohn MarinoWhy do people use strange regular expressions on @command{ps} output? 173495b7b453SJohn Marino 173595b7b453SJohn Marino@example 173695b7b453SJohn Marinops -ef | grep '[c]ron' 173795b7b453SJohn Marino@end example 173895b7b453SJohn Marino 173995b7b453SJohn MarinoIf the pattern had been written without the square brackets, it would 174095b7b453SJohn Marinohave matched not only the @command{ps} output line for @command{cron}, 174195b7b453SJohn Marinobut also the @command{ps} output line for @command{grep}. 174295b7b453SJohn MarinoNote that on some platforms, 174395b7b453SJohn Marino@command{ps} limits the output to the width of the screen; 174495b7b453SJohn Marino@command{grep} does not have any limit on the length of a line 174595b7b453SJohn Marinoexcept the available memory. 174695b7b453SJohn Marino 174795b7b453SJohn Marino@item 174895b7b453SJohn MarinoWhy does @command{grep} report ``Binary file matches''? 174995b7b453SJohn Marino 175095b7b453SJohn MarinoIf @command{grep} listed all matching ``lines'' from a binary file, it 175195b7b453SJohn Marinowould probably generate output that is not useful, and it might even 175295b7b453SJohn Marinomuck up your display. 1753cf28ed85SJohn MarinoSo GNU @command{grep} suppresses output from 175495b7b453SJohn Marinofiles that appear to be binary files. 1755cf28ed85SJohn MarinoTo force GNU @command{grep} 175695b7b453SJohn Marinoto output lines even from files that appear to be binary, use the 1757cf28ed85SJohn Marino@option{-a} or @samp{--binary-files=text} option. 175895b7b453SJohn MarinoTo eliminate the 1759cf28ed85SJohn Marino``Binary file matches'' messages, use the @option{-I} or 176095b7b453SJohn Marino@samp{--binary-files=without-match} option. 176195b7b453SJohn Marino 176295b7b453SJohn Marino@item 176395b7b453SJohn MarinoWhy doesn't @samp{grep -lv} print non-matching file names? 176495b7b453SJohn Marino 176595b7b453SJohn Marino@samp{grep -lv} lists the names of all files containing one or more 176695b7b453SJohn Marinolines that do not match. 176795b7b453SJohn MarinoTo list the names of all files that contain no 1768cf28ed85SJohn Marinomatching lines, use the @option{-L} or @option{--files-without-match} 176995b7b453SJohn Marinooption. 177095b7b453SJohn Marino 177195b7b453SJohn Marino@item 1772cf28ed85SJohn MarinoI can do ``OR'' with @samp{|}, but what about ``AND''? 177395b7b453SJohn Marino 177495b7b453SJohn Marino@example 177595b7b453SJohn Marinogrep 'paul' /etc/motd | grep 'franc,ois' 177695b7b453SJohn Marino@end example 177795b7b453SJohn Marino 177895b7b453SJohn Marino@noindent 177995b7b453SJohn Marinofinds all lines that contain both @samp{paul} and @samp{franc,ois}. 178095b7b453SJohn Marino 178195b7b453SJohn Marino@item 1782cf28ed85SJohn MarinoWhy does the empty pattern match every input line? 1783cf28ed85SJohn Marino 1784cf28ed85SJohn MarinoThe @command{grep} command searches for lines that contain strings 1785cf28ed85SJohn Marinothat match a pattern. Every line contains the empty string, so an 1786cf28ed85SJohn Marinoempty pattern causes @command{grep} to find a match on each line. It 1787*09d4459fSDaniel Fojtis not the only such pattern: @samp{^}, @samp{$}, and many 1788cf28ed85SJohn Marinoother patterns cause @command{grep} to match every line. 1789cf28ed85SJohn Marino 1790cf28ed85SJohn MarinoTo match empty lines, use the pattern @samp{^$}. To match blank 1791cf28ed85SJohn Marinolines, use the pattern @samp{^[[:blank:]]*$}. To match no lines at 1792cf28ed85SJohn Marinoall, use the command @samp{grep -f /dev/null}. 1793cf28ed85SJohn Marino 1794cf28ed85SJohn Marino@item 179595b7b453SJohn MarinoHow can I search in both standard input and in files? 179695b7b453SJohn Marino 179795b7b453SJohn MarinoUse the special file name @samp{-}: 179895b7b453SJohn Marino 179995b7b453SJohn Marino@example 180095b7b453SJohn Marinocat /etc/passwd | grep 'alain' - /etc/motd 180195b7b453SJohn Marino@end example 180295b7b453SJohn Marino 180395b7b453SJohn Marino@item 180495b7b453SJohn MarinoWhy is this back-reference failing? 180595b7b453SJohn Marino 180695b7b453SJohn Marino@example 180795b7b453SJohn Marinoecho 'ba' | grep -E '(a)\1|b\1' 180895b7b453SJohn Marino@end example 180995b7b453SJohn Marino 181095b7b453SJohn MarinoThis gives no output, because the first alternate @samp{(a)\1} does not match, 181195b7b453SJohn Marinoas there is no @samp{aa} in the input, so the @samp{\1} in the second alternate 181295b7b453SJohn Marinohas nothing to refer back to, meaning it will never match anything. 181395b7b453SJohn Marino(The second alternate in this example can only match 181495b7b453SJohn Marinoif the first alternate has matched---making the second one superfluous.) 181595b7b453SJohn Marino 181695b7b453SJohn Marino@item 181795b7b453SJohn MarinoHow can I match across lines? 181895b7b453SJohn Marino 181995b7b453SJohn MarinoStandard grep cannot do this, as it is fundamentally line-based. 182095b7b453SJohn MarinoTherefore, merely using the @code{[:space:]} character class does not 1821680a9cb8SJohn Marinomatch newlines in the way you might expect. 182295b7b453SJohn Marino 1823dc7c36e4SJohn MarinoWith the GNU @command{grep} option @option{-z} (@option{--null-data}), each 1824*09d4459fSDaniel Fojtinput and output ``line'' is null-terminated; @pxref{Other Options}. Thus, 1825680a9cb8SJohn Marinoyou can match newlines in the input, but typically if there is a match 1826680a9cb8SJohn Marinothe entire input is output, so this usage is often combined with 1827680a9cb8SJohn Marinooutput-suppressing options like @option{-q}, e.g.: 182895b7b453SJohn Marino 182995b7b453SJohn Marino@example 183095b7b453SJohn Marinoprintf 'foo\nbar\n' | grep -z -q 'foo[[:space:]]\+bar' 183195b7b453SJohn Marino@end example 183295b7b453SJohn Marino 1833680a9cb8SJohn MarinoIf this does not suffice, you can transform the input 183495b7b453SJohn Marinobefore giving it to @command{grep}, or turn to @command{awk}, 183595b7b453SJohn Marino@command{sed}, @command{perl}, or many other utilities that are 183695b7b453SJohn Marinodesigned to operate across lines. 183795b7b453SJohn Marino 183895b7b453SJohn Marino@item 183995b7b453SJohn MarinoWhat do @command{grep}, @command{fgrep}, and @command{egrep} stand for? 184095b7b453SJohn Marino 184195b7b453SJohn MarinoThe name @command{grep} comes from the way line editing was done on Unix. 184295b7b453SJohn MarinoFor example, 184395b7b453SJohn Marino@command{ed} uses the following syntax 184495b7b453SJohn Marinoto print a list of matching lines on the screen: 184595b7b453SJohn Marino 184695b7b453SJohn Marino@example 184795b7b453SJohn Marinoglobal/regular expression/print 184895b7b453SJohn Marinog/re/p 184995b7b453SJohn Marino@end example 185095b7b453SJohn Marino 185195b7b453SJohn Marino@command{fgrep} stands for Fixed @command{grep}; 185295b7b453SJohn Marino@command{egrep} stands for Extended @command{grep}. 185395b7b453SJohn Marino 185495b7b453SJohn Marino@end enumerate 185595b7b453SJohn Marino 185695b7b453SJohn Marino 1857*09d4459fSDaniel Fojt@node Performance 1858*09d4459fSDaniel Fojt@chapter Performance 1859*09d4459fSDaniel Fojt 1860*09d4459fSDaniel Fojt@cindex performance 1861*09d4459fSDaniel FojtTypically @command{grep} is an efficient way to search text. However, 1862*09d4459fSDaniel Fojtit can be quite slow in some cases, and it can search large files 1863*09d4459fSDaniel Fojtwhere even minor performance tweaking can help significantly. 1864*09d4459fSDaniel FojtAlthough the algorithm used by @command{grep} is an implementation 1865*09d4459fSDaniel Fojtdetail that can change from release to release, understanding its 1866*09d4459fSDaniel Fojtbasic strengths and weaknesses can help you improve its performance. 1867*09d4459fSDaniel Fojt 1868*09d4459fSDaniel FojtThe @command{grep} command operates partly via a set of automata that 1869*09d4459fSDaniel Fojtare designed for efficiency, and partly via a slower matcher that 1870*09d4459fSDaniel Fojttakes over when the fast matchers run into unusual features like 1871*09d4459fSDaniel Fojtback-references. When feasible, the Boyer--Moore fast string 1872*09d4459fSDaniel Fojtsearching algorithm is used to match a single fixed pattern, and the 1873*09d4459fSDaniel FojtAho--Corasick algorithm is used to match multiple fixed patterns. 1874*09d4459fSDaniel Fojt 1875*09d4459fSDaniel Fojt@cindex locales 1876*09d4459fSDaniel FojtGenerally speaking @command{grep} operates more efficiently in 1877*09d4459fSDaniel Fojtsingle-byte locales, since it can avoid the special processing needed 1878*09d4459fSDaniel Fojtfor multi-byte characters. If your patterns will work just as well 1879*09d4459fSDaniel Fojtthat way, setting @env{LC_ALL} to a single-byte locale can help 1880*09d4459fSDaniel Fojtperformance considerably. Setting @samp{LC_ALL='C'} can be 1881*09d4459fSDaniel Fojtparticularly efficient, as @command{grep} is tuned for that locale. 1882*09d4459fSDaniel Fojt 1883*09d4459fSDaniel Fojt@cindex case insensitive search 1884*09d4459fSDaniel FojtOutside the @samp{C} locale, case-insensitive search, and search for 1885*09d4459fSDaniel Fojtbracket expressions like @samp{[a-z]} and @samp{[[=a=]b]}, can be 1886*09d4459fSDaniel Fojtsurprisingly inefficient due to difficulties in fast portable access to 1887*09d4459fSDaniel Fojtconcepts like multi-character collating elements. 1888*09d4459fSDaniel Fojt 1889*09d4459fSDaniel Fojt@cindex back-references 1890*09d4459fSDaniel FojtA back-reference such as @samp{\1} can hurt performance significantly 1891*09d4459fSDaniel Fojtin some cases, since back-references cannot in general be implemented 1892*09d4459fSDaniel Fojtvia a finite state automaton, and instead trigger a backtracking 1893*09d4459fSDaniel Fojtalgorithm that can be quite inefficient. For example, although the 1894*09d4459fSDaniel Fojtpattern @samp{^(.*)\1@{14@}(.*)\2@{13@}$} matches only lines whose 1895*09d4459fSDaniel Fojtlengths can be written as a sum @math{15x + 14y} for nonnegative 1896*09d4459fSDaniel Fojtintegers @math{x} and @math{y}, the pattern matcher does not perform 1897*09d4459fSDaniel Fojtlinear Diophantine analysis and instead backtracks through all 1898*09d4459fSDaniel Fojtpossible matching strings, using an algorithm that is exponential in 1899*09d4459fSDaniel Fojtthe worst case. 1900*09d4459fSDaniel Fojt 1901*09d4459fSDaniel Fojt@cindex holes in files 1902*09d4459fSDaniel FojtOn some operating systems that support files with holes---large 1903*09d4459fSDaniel Fojtregions of zeros that are not physically present on secondary 1904*09d4459fSDaniel Fojtstorage---@command{grep} can skip over the holes efficiently without 1905*09d4459fSDaniel Fojtneeding to read the zeros. This optimization is not available if the 1906*09d4459fSDaniel Fojt@option{-a} (@option{--binary-files=text}) option is used (@pxref{File and 1907*09d4459fSDaniel FojtDirectory Selection}), unless the @option{-z} (@option{--null-data}) 1908*09d4459fSDaniel Fojtoption is also used (@pxref{Other Options}). 1909*09d4459fSDaniel Fojt 1910*09d4459fSDaniel FojtFor more about the algorithms used by @command{grep} and about 1911*09d4459fSDaniel Fojtrelated string matching algorithms, see: 1912*09d4459fSDaniel Fojt 1913*09d4459fSDaniel Fojt@frenchspacing on 1914*09d4459fSDaniel Fojt@itemize @bullet 1915*09d4459fSDaniel Fojt@item 1916*09d4459fSDaniel FojtAho AV. Algorithms for finding patterns in strings. 1917*09d4459fSDaniel FojtIn: van Leeuwen J. @emph{Handbook of Theoretical Computer Science}, vol. A. 1918*09d4459fSDaniel FojtNew York: Elsevier; 1990. p. 255--300. 1919*09d4459fSDaniel FojtThis surveys classic string matching algorithms, some of which are 1920*09d4459fSDaniel Fojtused by @command{grep}. 1921*09d4459fSDaniel Fojt 1922*09d4459fSDaniel Fojt@item 1923*09d4459fSDaniel FojtAho AV, Corasick MJ. Efficient string matching: an aid to bibliographic search. 1924*09d4459fSDaniel Fojt@emph{CACM}. 1975;18(6):333--40. 1925*09d4459fSDaniel Fojt@url{https://dx.doi.org/10.1145/360825.360855}. 1926*09d4459fSDaniel FojtThis introduces the Aho--Corasick algorithm. 1927*09d4459fSDaniel Fojt 1928*09d4459fSDaniel Fojt@item 1929*09d4459fSDaniel FojtBoyer RS, Moore JS. A fast string searching algorithm. 1930*09d4459fSDaniel Fojt@emph{CACM}. 1977;20(10):762--72. 1931*09d4459fSDaniel Fojt@url{https://dx.doi.org/10.1145/359842.359859}. 1932*09d4459fSDaniel FojtThis introduces the Boyer--Moore algorithm. 1933*09d4459fSDaniel Fojt 1934*09d4459fSDaniel Fojt@item 1935*09d4459fSDaniel FojtFaro S, Lecroq T. The exact online string matching problem: a review 1936*09d4459fSDaniel Fojtof the most recent results. 1937*09d4459fSDaniel Fojt@emph{ACM Comput Surv}. 2013;45(2):13. 1938*09d4459fSDaniel Fojt@url{https://dx.doi.org/10.1145/2431211.2431212}. 1939*09d4459fSDaniel FojtThis surveys string matching algorithms that might help improve the 1940*09d4459fSDaniel Fojtperformance of @command{grep} in the future. 1941*09d4459fSDaniel Fojt@end itemize 1942*09d4459fSDaniel Fojt@frenchspacing off 1943*09d4459fSDaniel Fojt 194495b7b453SJohn Marino@node Reporting Bugs 194595b7b453SJohn Marino@chapter Reporting bugs 194695b7b453SJohn Marino 194795b7b453SJohn Marino@cindex bugs, reporting 1948dc7c36e4SJohn MarinoBug reports can be found at the 1949*09d4459fSDaniel Fojt@url{https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep, 1950dc7c36e4SJohn MarinoGNU bug report logs for @command{grep}}. 1951dc7c36e4SJohn MarinoIf you find a bug not listed there, please email it to 1952dc7c36e4SJohn Marino@email{bug-grep@@gnu.org} to create a new bug report. 195395b7b453SJohn Marino 1954*09d4459fSDaniel Fojt@menu 1955*09d4459fSDaniel Fojt* Known Bugs:: 1956*09d4459fSDaniel Fojt@end menu 1957*09d4459fSDaniel Fojt 1958*09d4459fSDaniel Fojt@node Known Bugs 195995b7b453SJohn Marino@section Known Bugs 196095b7b453SJohn Marino@cindex Bugs, known 196195b7b453SJohn Marino 196295b7b453SJohn MarinoLarge repetition counts in the @samp{@{n,m@}} construct may cause 196395b7b453SJohn Marino@command{grep} to use lots of memory. 196495b7b453SJohn MarinoIn addition, certain other 196595b7b453SJohn Marinoobscure regular expressions require exponential time and 196695b7b453SJohn Marinospace, and may cause @command{grep} to run out of memory. 196795b7b453SJohn Marino 1968*09d4459fSDaniel FojtBack-references can greatly slow down matching, as they can generate 1969*09d4459fSDaniel Fojtexponentially many matching possibilities that can consume both time 1970*09d4459fSDaniel Fojtand memory to explore. Also, the POSIX specification for 1971*09d4459fSDaniel Fojtback-references is at times unclear. Furthermore, many regular 1972*09d4459fSDaniel Fojtexpression implementations have back-reference bugs that can cause 1973*09d4459fSDaniel Fojtprograms to return incorrect answers or even crash, and fixing these 1974*09d4459fSDaniel Fojtbugs has often been low-priority---for example, as of 2019 the GNU C 1975*09d4459fSDaniel Fojtlibrary bug database contained back-reference bugs 52, 10844, 11053, 1976*09d4459fSDaniel Fojtand 25322, with little sign of forthcoming fixes. Luckily, 1977*09d4459fSDaniel Fojtback-references are rarely useful and it should be little trouble to 1978*09d4459fSDaniel Fojtavoid them in practical applications. 197995b7b453SJohn Marino 198095b7b453SJohn Marino 198195b7b453SJohn Marino@node Copying 198295b7b453SJohn Marino@chapter Copying 198395b7b453SJohn Marino@cindex copying 198495b7b453SJohn Marino 1985cf28ed85SJohn MarinoGNU @command{grep} is licensed under the GNU GPL, which makes it @dfn{free 198695b7b453SJohn Marinosoftware}. 198795b7b453SJohn Marino 198895b7b453SJohn MarinoThe ``free'' in ``free software'' refers to liberty, not price. As 198995b7b453SJohn Marinosome GNU project advocates like to point out, think of ``free speech'' 199095b7b453SJohn Marinorather than ``free beer''. In short, you have the right (freedom) to 1991cf28ed85SJohn Marinorun and change @command{grep} and distribute it to other people, and---if you 199295b7b453SJohn Marinowant---charge money for doing either. The important restriction is 199395b7b453SJohn Marinothat you have to grant your recipients the same rights and impose the 199495b7b453SJohn Marinosame restrictions. 199595b7b453SJohn Marino 199695b7b453SJohn MarinoThis general method of licensing software is sometimes called 199795b7b453SJohn Marino@dfn{open source}. The GNU project prefers the term ``free software'' 199895b7b453SJohn Marinofor reasons outlined at 1999*09d4459fSDaniel Fojt@url{https://www.gnu.org/philosophy/open-source-misses-the-point.html}. 200095b7b453SJohn Marino 200195b7b453SJohn MarinoThis manual is free documentation in the same sense. The 200295b7b453SJohn Marinodocumentation license is included below. The license for the program 200395b7b453SJohn Marinois available with the source code, or at 2004*09d4459fSDaniel Fojt@url{https://www.gnu.org/licenses/gpl.html}. 200595b7b453SJohn Marino 200695b7b453SJohn Marino@menu 200795b7b453SJohn Marino* GNU Free Documentation License:: 200895b7b453SJohn Marino@end menu 200995b7b453SJohn Marino 201095b7b453SJohn Marino@node GNU Free Documentation License 201195b7b453SJohn Marino@section GNU Free Documentation License 201295b7b453SJohn Marino 201395b7b453SJohn Marino@include fdl.texi 201495b7b453SJohn Marino 201595b7b453SJohn Marino 201695b7b453SJohn Marino@node Index 201795b7b453SJohn Marino@unnumbered Index 201895b7b453SJohn Marino 201995b7b453SJohn Marino@printindex cp 202095b7b453SJohn Marino 202195b7b453SJohn Marino@bye 2022