VirtualBox

source: kBuild/vendor/grep/3.7/doc/grep.in.1

Last change on this file was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

File size: 33.0 KB
Line 
1.\" GNU grep man page
2.de dT
3.ds Dt \\$2
4..
5.dT Time-stamp: "2019-12-29"
6.\" Update the above date whenever a change to either this file or
7.\" grep.c's 'usage' function results in a nontrivial change to the man page.
8.\" In Emacs, you can update the date by running 'M-x time-stamp'
9.\" after you make a change that you decide is nontrivial.
10.\" It is no big deal to forget to update the date.
11.
12.TH GREP 1 \*(Dt "GNU grep @VERSION@" "User Commands"
13.
14.if !\w|\*(lq| \{\
15.\" groff an-old.tmac does not seem to be in use, so define lq and rq.
16. ie \n(.g \{\
17. ds lq \(lq\"
18. ds rq \(rq\"
19. \}
20. el \{\
21. ds lq ``
22. ds rq ''
23. \}
24.\}
25.
26.if !\w|\*(la| \{\
27.\" groff an-ext.tmac does not seem to be in use, so define the parts of
28.\" it that are used below. For a copy of groff an-ext.tmac, please see:
29.\" https://git.savannah.gnu.org/cgit/groff.git/plain/tmac/an-ext.tmac
30.\" --- Start of lines taken from groff an-ext.tmac
31.
32.\" Check whether we are using grohtml.
33.nr mH 0
34.if \n(.g \
35. if '\*(.T'html' \
36. nr mH 1
37.
38.
39.\" Map mono-width fonts to standard fonts for groff's TTY device.
40.if n \{\
41. do ftr CR R
42. do ftr CI I
43. do ftr CB B
44.\}
45.
46.\" groff has glyph entities for angle brackets.
47.ie \n(.g \{\
48. ds la \(la\"
49. ds ra \(ra\"
50.\}
51.el \{\
52. ds la <\"
53. ds ra >\"
54. \" groff's man macros control hyphenation with this register.
55. nr HY 1
56.\}
57.
58.\" Start URL.
59.de UR
60. ds m1 \\$1\"
61. nh
62. if \\n(mH \{\
63. \" Start diversion in a new environment.
64. do ev URL-div
65. do di URL-div
66. \}
67..
68.
69.
70.\" End URL.
71.de UE
72. ie \\n(mH \{\
73. br
74. di
75. ev
76.
77. \" Has there been one or more input lines for the link text?
78. ie \\n(dn \{\
79. do HTML-NS "<a href=""\\*(m1"">"
80. \" Yes, strip off final newline of diversion and emit it.
81. do chop URL-div
82. do URL-div
83\c
84. do HTML-NS </a>
85. \}
86. el \
87. do HTML-NS "<a href=""\\*(m1"">\\*(m1</a>"
88\&\\$*\"
89. \}
90. el \
91\\*(la\\*(m1\\*(ra\\$*\"
92.
93. hy \\n(HY
94..
95.
96.
97.\" Start email address.
98.de MT
99. ds m1 \\$1\"
100. nh
101. if \\n(mH \{\
102. \" Start diversion in a new environment.
103. do ev URL-div
104. do di URL-div
105. \}
106..
107.
108.
109.\" End email address.
110.de ME
111. ie \\n(mH \{\
112. br
113. di
114. ev
115.
116. \" Has there been one or more input lines for the link text?
117. ie \\n(dn \{\
118. do HTML-NS "<a href=""mailto:\\*(m1"">"
119. \" Yes, strip off final newline of diversion and emit it.
120. do chop URL-div
121. do URL-div
122\c
123. do HTML-NS </a>
124. \}
125. el \
126. do HTML-NS "<a href=""mailto:\\*(m1"">\\*(m1</a>"
127\&\\$*\"
128. \}
129. el \
130\\*(la\\*(m1\\*(ra\\$*\"
131.
132. hy \\n(HY
133..
134.\" --- End of lines taken from groff an-ext.tmac
135.\}
136.
137.hy 0
138.
139.SH NAME
140grep, egrep, fgrep \- print lines that match patterns
141.
142.SH SYNOPSIS
143.B grep
144.RI [ OPTION .\|.\|.]\&
145.I PATTERNS
146.RI [ FILE .\|.\|.]
147.br
148.B grep
149.RI [ OPTION .\|.\|.]\&
150.B \-e
151.I PATTERNS
152\&.\|.\|.\&
153.RI [ FILE .\|.\|.]
154.br
155.B grep
156.RI [ OPTION .\|.\|.]\&
157.B \-f
158.I PATTERN_FILE
159\&.\|.\|.\&
160.RI [ FILE .\|.\|.]
161.
162.SH DESCRIPTION
163.B grep
164searches for
165.I PATTERNS
166in each
167.IR FILE .
168.I PATTERNS
169is one or more patterns separated by newline characters, and
170.B grep
171prints each line that matches a pattern.
172Typically
173.I PATTERNS
174should be quoted when
175.B grep
176is used in a shell command.
177.PP
178A
179.I FILE
180of
181.RB "\*(lq" \- "\*(rq"
182stands for standard input.
183If no
184.I FILE
185is given, recursive searches examine the working directory,
186and nonrecursive searches read standard input.
187.PP
188In addition, the variant programs
189.B egrep
190and
191.B fgrep
192are the same as
193.B "grep\ \-E"
194and
195.BR "grep\ \-F" ,
196respectively.
197These variants are deprecated, but are provided for backward compatibility.
198.
199.SH OPTIONS
200.SS "Generic Program Information"
201.TP
202.B \-\^\-help
203Output a usage message and exit.
204.TP
205.BR \-V ", " \-\^\-version
206Output the version number of
207.B grep
208and exit.
209.SS "Pattern Syntax"
210.TP
211.BR \-E ", " \-\^\-extended\-regexp
212Interpret
213.I PATTERNS
214as extended regular expressions (EREs, see below).
215.TP
216.BR \-F ", " \-\^\-fixed\-strings
217Interpret
218.I PATTERNS
219as fixed strings, not regular expressions.
220.TP
221.BR \-G ", " \-\^\-basic\-regexp
222Interpret
223.I PATTERNS
224as basic regular expressions (BREs, see below).
225This is the default.
226.TP
227.BR \-P ", " \-\^\-perl\-regexp
228Interpret I<PATTERNS> as Perl-compatible regular expressions (PCREs).
229This option is experimental when combined with the
230.B \-z
231.RB ( \-\^\-null\-data )
232option, and
233.B "grep \-P"
234may warn of unimplemented features.
235.SS "Matching Control"
236.TP
237.BI \-e " PATTERNS" "\fR,\fP \-\^\-regexp=" PATTERNS
238Use
239.I PATTERNS
240as the patterns.
241If this option is used multiple times or is combined with the
242.B \-f
243.RB ( \-\^\-file )
244option, search for all patterns given.
245This option can be used to protect a pattern beginning with \*(lq\-\*(rq.
246.TP
247.BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
248Obtain patterns from
249.IR FILE ,
250one per line.
251If this option is used multiple times or is combined with the
252.B \-e
253.RB ( \-\^\-regexp )
254option, search for all patterns given.
255The empty file contains zero patterns, and therefore matches nothing.
256.TP
257.BR \-i ", " \-\^\-ignore\-case
258Ignore case distinctions in patterns and input data,
259so that characters that differ only in case
260match each other.
261.TP
262.B \-\^\-no\-ignore\-case
263Do not ignore case distinctions in patterns and input data.
264This is the default.
265This option is useful for passing to shell scripts that already use
266.BR \-i ,
267to cancel its effects because the two options override each other.
268.TP
269.BR \-v ", " \-\^\-invert\-match
270Invert the sense of matching, to select non-matching lines.
271.TP
272.BR \-w ", " \-\^\-word\-regexp
273Select only those lines containing matches that form whole words.
274The test is that the matching substring must either be at the
275beginning of the line, or preceded by a non-word constituent
276character.
277Similarly, it must be either at the end of the line
278or followed by a non-word constituent character.
279Word-constituent characters are letters, digits, and the underscore.
280This option has no effect if
281.B \-x
282is also specified.
283.TP
284.BR \-x ", " \-\^\-line\-regexp
285Select only those matches that exactly match the whole line.
286For a regular expression pattern, this is like parenthesizing the
287pattern and then surrounding it with
288.B ^
289and
290.BR $ .
291.TP
292.B \-y
293Obsolete synonym for
294.BR \-i .
295.SS "General Output Control"
296.TP
297.BR \-c ", " \-\^\-count
298Suppress normal output; instead print a count of
299matching lines for each input file.
300With the
301.BR \-v ", " \-\^\-invert\-match
302option (see below), count non-matching lines.
303.TP
304.BR \-\^\-color [ =\fIWHEN\fP "], " \-\^\-colour [ =\fIWHEN\fP ]
305Surround the matched (non-empty) strings, matching lines, context lines,
306file names, line numbers, byte offsets, and separators (for fields and
307groups of context lines) with escape sequences to display them in color
308on the terminal.
309The colors are defined by the environment variable
310.BR GREP_COLORS .
311The deprecated environment variable
312.B GREP_COLOR
313is still supported, but its setting does not have priority.
314.I WHEN
315is
316.BR never ", " always ", or " auto .
317.TP
318.BR \-L ", " \-\^\-files\-without\-match
319Suppress normal output; instead print the name
320of each input file from which no output would
321normally have been printed.
322.TP
323.BR \-l ", " \-\^\-files\-with\-matches
324Suppress normal output; instead print
325the name of each input file from which output
326would normally have been printed.
327Scanning each input file stops upon first match.
328.TP
329.BI \-m " NUM" "\fR,\fP \-\^\-max\-count=" NUM
330Stop reading a file after
331.I NUM
332matching lines.
333If the input is standard input from a regular file,
334and
335.I NUM
336matching lines are output,
337.B grep
338ensures that the standard input is positioned to just after the last
339matching line before exiting, regardless of the presence of trailing
340context lines.
341This enables a calling process to resume a search.
342When
343.B grep
344stops after
345.I NUM
346matching lines, it outputs any trailing context lines.
347When the
348.B \-c
349or
350.B \-\^\-count
351option is also used,
352.B grep
353does not output a count greater than
354.IR NUM .
355When the
356.B \-v
357or
358.B \-\^\-invert\-match
359option is also used,
360.B grep
361stops after outputting
362.I NUM
363non-matching lines.
364.TP
365.BR \-o ", " \-\^\-only\-matching
366Print only the matched (non-empty) parts of a matching line,
367with each such part on a separate output line.
368.TP
369.BR \-q ", " \-\^\-quiet ", " \-\^\-silent
370Quiet; do not write anything to standard output.
371Exit immediately with zero status if any match is found,
372even if an error was detected.
373Also see the
374.B \-s
375or
376.B \-\^\-no\-messages
377option.
378.TP
379.BR \-s ", " \-\^\-no\-messages
380Suppress error messages about nonexistent or unreadable files.
381.SS "Output Line Prefix Control"
382.TP
383.BR \-b ", " \-\^\-byte\-offset
384Print the 0-based byte offset within the input file
385before each line of output.
386If
387.B \-o
388.RB ( \-\^\-only\-matching )
389is specified,
390print the offset of the matching part itself.
391.TP
392.BR \-H ", " \-\^\-with\-filename
393Print the file name for each match.
394This is the default when there is more than one file to search.
395This is a GNU extension.
396.TP
397.BR \-h ", " \-\^\-no\-filename
398Suppress the prefixing of file names on output.
399This is the default when there is only one file
400(or only standard input) to search.
401.TP
402.BI \-\^\-label= LABEL
403Display input actually coming from standard input as input coming from file
404.IR LABEL .
405This can be useful for commands that transform a file's contents
406before searching,
407e.g.,
408.BR "gzip \-cd foo.gz | grep \-\^\-label=foo \-H 'some pattern'" .
409See also the
410.B \-H
411option.
412.TP
413.BR \-n ", " \-\^\-line\-number
414Prefix each line of output with the 1-based line number
415within its input file.
416.TP
417.BR \-T ", " \-\^\-initial\-tab
418Make sure that the first character of actual line content lies on a
419tab stop, so that the alignment of tabs looks normal.
420This is useful with options that prefix their output to the actual content:
421.BR \-H , \-n ,
422and
423.BR \-b .
424In order to improve the probability that lines
425from a single file will all start at the same column,
426this also causes the line number and byte offset (if present)
427to be printed in a minimum size field width.
428.TP
429.BR \-Z ", " \-\^\-null
430Output a zero byte (the ASCII
431.B NUL
432character) instead of the character that normally follows a file name.
433For example,
434.B "grep \-lZ"
435outputs a zero byte after each file name instead of the usual newline.
436This option makes the output unambiguous, even in the presence of file
437names containing unusual characters like newlines.
438This option can be used with commands like
439.BR "find \-print0" ,
440.BR "perl \-0" ,
441.BR "sort \-z" ,
442and
443.B "xargs \-0"
444to process arbitrary file names,
445even those that contain newline characters.
446.SS "Context Line Control"
447.TP
448.BI \-A " NUM" "\fR,\fP \-\^\-after\-context=" NUM
449Print
450.I NUM
451lines of trailing context after matching lines.
452Places a line containing a group separator
453.RB ( \-\^\- )
454between contiguous groups of matches.
455With the
456.B \-o
457or
458.B \-\^\-only\-matching
459option, this has no effect and a warning is given.
460.TP
461.BI \-B " NUM" "\fR,\fP \-\^\-before\-context=" NUM
462Print
463.I NUM
464lines of leading context before matching lines.
465Places a line containing a group separator
466.RB ( \-\^\- )
467between contiguous groups of matches.
468With the
469.B \-o
470or
471.B \-\^\-only\-matching
472option, this has no effect and a warning is given.
473.TP
474.BI \-C " NUM" "\fR,\fP \-" NUM "\fR,\fP \-\^\-context=" NUM
475Print
476.I NUM
477lines of output context.
478Places a line containing a group separator
479.RB ( \-\^\- )
480between contiguous groups of matches.
481With the
482.B \-o
483or
484.B \-\^\-only\-matching
485option, this has no effect and a warning is given.
486.TP
487.BI \-\^\-group\-separator= SEP
488When
489.BR \-A ,
490.BR \-B ,
491or
492.B \-C
493are in use, print
494.I SEP
495instead of
496.B \-\^\-
497between groups of lines.
498.TP
499.B \-\^\-no\-group\-separator
500When
501.BR \-A ,
502.BR \-B ,
503or
504.B \-C
505are in use, do not print a separator between groups of lines.
506.SS "File and Directory Selection"
507.TP
508.BR \-a ", " \-\^\-text
509Process a binary file as if it were text; this is equivalent to the
510.B \-\^\-binary\-files=text
511option.
512.TP
513.BI \-\^\-binary\-files= TYPE
514If a file's data or metadata
515indicate that the file contains binary data,
516assume that the file is of type
517.IR TYPE .
518Non-text bytes indicate binary data; these are either output bytes that are
519improperly encoded for the current locale, or null input bytes when the
520.B \-z
521option is not given.
522.IP
523By default,
524.I TYPE
525is
526.BR binary ,
527and
528.B grep
529suppresses output after null input binary data is discovered,
530and suppresses output lines that contain improperly encoded data.
531When some output is suppressed,
532.B grep
533follows any output
534with a one-line message saying that a binary file matches.
535.IP
536If
537.I TYPE
538is
539.BR without\-match ,
540when
541.B grep
542discovers null input binary data it assumes that the rest of the file
543does not match; this is equivalent to the
544.B \-I
545option.
546.IP
547If
548.I TYPE
549is
550.BR text ,
551.B grep
552processes a binary file as if it were text; this is equivalent to the
553.B \-a
554option.
555.IP
556When
557.I type
558is
559.BR binary ,
560.B grep
561may treat non-text bytes as line terminators even without the
562.B \-z
563option. This means choosing
564.B binary
565versus
566.B text
567can affect whether a pattern matches a file. For
568example, when
569.I type
570is
571.B binary
572the pattern
573.B q$ might
574match
575.B q
576immediately followed by a null byte, even though this
577is not matched when
578.I type
579is
580.BR text .
581Conversely, when
582.I type
583is
584.B binary
585the pattern
586.B .\&
587(period) might not match a null byte.
588.IP
589.I Warning:
590The
591.B \-a
592option might output binary garbage,
593which can have nasty side effects if the output is a terminal and if the
594terminal driver interprets some of it as commands.
595On the other hand, when reading files whose text encodings are
596unknown, it can be helpful to use
597.B \-a
598or to set
599.B LC_ALL='C'
600in the environment, in order to find more matches even if the matches
601are unsafe for direct display.
602.TP
603.BI \-D " ACTION" "\fR,\fP \-\^\-devices=" ACTION
604If an input file is a device, FIFO or socket, use
605.I ACTION
606to process it.
607By default,
608.I ACTION
609is
610.BR read ,
611which means that devices are read just as if they were ordinary files.
612If
613.I ACTION
614is
615.BR skip ,
616devices are silently skipped.
617.TP
618.BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
619If an input file is a directory, use
620.I ACTION
621to process it.
622By default,
623.I ACTION
624is
625.BR read ,
626i.e., read directories just as if they were ordinary files.
627If
628.I ACTION
629is
630.BR skip ,
631silently skip directories.
632If
633.I ACTION
634is
635.BR recurse ,
636read all files under each directory, recursively,
637following symbolic links only if they are on the command line.
638This is equivalent to the
639.B \-r
640option.
641.TP
642.BI \-\^\-exclude= GLOB
643Skip any command-line file with a name suffix that matches the pattern
644.IR GLOB ,
645using wildcard matching; a name suffix is either the whole
646name, or a trailing part that starts with a non-slash character
647immediately after a slash
648.RB ( / )
649in the name.
650When searching recursively, skip any subfile whose base name matches
651.IR GLOB ;
652the base name is the part after the last slash.
653A pattern can use
654.BR * ,
655.BR ? ,
656and
657.BR [ .\|.\|. ]\&
658as wildcards, and
659.B \e
660to quote a wildcard or backslash character literally.
661.TP
662.BI \-\^\-exclude\-from= FILE
663Skip files whose base name matches any of the file-name globs read from
664.I FILE
665(using wildcard matching as described under
666.BR \-\^\-exclude ).
667.TP
668.BI \-\^\-exclude\-dir= GLOB
669Skip any command-line directory with a name suffix that matches the
670pattern
671.IR GLOB .
672When searching recursively, skip any subdirectory
673whose base name matches
674.IR GLOB .
675Ignore any redundant trailing slashes in
676.IR GLOB .
677.TP
678.BR \-I
679Process a binary file as if it did not contain matching data; this is
680equivalent to the
681.B \-\^\-binary\-files=without\-match
682option.
683.TP
684.BI \-\^\-include= GLOB
685Search only files whose base name matches
686.I GLOB
687(using wildcard matching as described under
688.BR \-\^\-exclude ).
689If contradictory
690.B \-\^\-include
691and
692.B \-\^\-exclude
693options are given, the last matching one wins.
694If no
695.B \-\^\-include
696or
697.B \-\^\-exclude
698options match, a file is included unless the first such option is
699.BR \-\^\-include .
700.TP
701.BR \-r ", " \-\^\-recursive
702Read all files under each directory, recursively,
703following symbolic links only if they are on the command line.
704Note that if no file operand is given, B<grep> searches the working directory.
705This is equivalent to the
706.B "\-d recurse"
707option.
708.TP
709.BR \-R ", " \-\^\-dereference\-recursive
710Read all files under each directory, recursively.
711Follow all symbolic links, unlike
712.BR \-r .
713.SS "Other Options"
714.TP
715.B \-\^\-line\-buffered
716Use line buffering on output.
717This can cause a performance penalty.
718.TP
719.BR \-U ", " \-\^\-binary
720Treat the file(s) as binary.
721By default, under MS-DOS and MS-Windows,
722.BR grep
723guesses whether a file is text or binary as described for the
724.B \-\^\-binary\-files
725option.
726If
727.BR grep
728decides the file is a text file, it strips the CR characters from the
729original file contents (to make regular expressions with
730.B ^
731and
732.B $
733work correctly).
734Specifying
735.B \-U
736overrules this guesswork, causing all files to be read and passed to the
737matching mechanism verbatim; if the file is a text file with CR/LF
738pairs at the end of each line, this will cause some regular
739expressions to fail.
740This option has no effect on platforms
741other than MS-DOS and MS-Windows.
742.TP
743.BR \-z ", " \-\^\-null\-data
744Treat input and output data as sequences of lines, each terminated by
745a zero byte (the ASCII NUL character) instead of a newline.
746Like the
747.B \-Z
748or
749.B \-\^\-null
750option, this option can be used with commands like
751.B sort -z
752to process arbitrary file names.
753.
754.SH "REGULAR EXPRESSIONS"
755A regular expression is a pattern that describes a set of strings.
756Regular expressions are constructed analogously to arithmetic
757expressions, by using various operators to combine smaller expressions.
758.PP
759.B grep
760understands three different versions of regular expression syntax:
761\*(lqbasic\*(rq (BRE), \*(lqextended\*(rq (ERE) and \*(lqperl\*(rq (PCRE).
762In GNU
763.B grep
764there is no difference in available functionality between basic and
765extended syntaxes.
766In other implementations, basic regular expressions are less powerful.
767The following description applies to extended regular expressions;
768differences for basic regular expressions are summarized afterwards.
769Perl-compatible regular expressions give additional functionality, and are
770documented in B<pcresyntax>(3) and B<pcrepattern>(3), but work only if
771PCRE support is enabled.
772.PP
773The fundamental building blocks are the regular expressions
774that match a single character.
775Most characters, including all letters and digits,
776are regular expressions that match themselves.
777Any meta-character with special meaning
778may be quoted by preceding it with a backslash.
779.PP
780The period
781.B .\&
782matches any single character.
783It is unspecified whether it matches an encoding error.
784.SS "Character Classes and Bracket Expressions"
785A
786.I "bracket expression"
787is a list of characters enclosed by
788.B [
789and
790.BR ] .
791It matches any single
792character in that list.
793If the first character of the list
794is the caret
795.B ^
796then it matches any character
797.I not
798in the list; it is unspecified whether it matches an encoding error.
799For example, the regular expression
800.B [0123456789]
801matches any single digit.
802.PP
803Within a bracket expression, a
804.I "range expression"
805consists of two characters separated by a hyphen.
806It matches any single character that sorts between the two characters,
807inclusive, using the locale's collating sequence and character set.
808For example, in the default C locale,
809.B [a\-d]
810is equivalent to
811.BR [abcd] .
812Many locales sort characters in dictionary order, and in these locales
813.B [a\-d]
814is typically not equivalent to
815.BR [abcd] ;
816it might be equivalent to
817.BR [aBbCcDd] ,
818for example.
819To obtain the traditional interpretation of bracket expressions,
820you can use the C locale by setting the
821.B LC_ALL
822environment variable to the value
823.BR C .
824.PP
825Finally, certain named classes of characters are predefined within
826bracket expressions, as follows.
827Their names are self explanatory, and they are
828.BR [:alnum:] ,
829.BR [:alpha:] ,
830.BR [:blank:] ,
831.BR [:cntrl:] ,
832.BR [:digit:] ,
833.BR [:graph:] ,
834.BR [:lower:] ,
835.BR [:print:] ,
836.BR [:punct:] ,
837.BR [:space:] ,
838.BR [:upper:] ,
839and
840.BR [:xdigit:] .
841For example,
842.B [[:alnum:]]
843means the character class of numbers and
844letters in the current locale.
845In the C locale and ASCII
846character set encoding, this is the same as
847.BR [0\-9A\-Za\-z] .
848(Note that the brackets in these class names are part of the symbolic
849names, and must be included in addition to the brackets delimiting
850the bracket expression.)
851Most meta-characters lose their special meaning inside bracket expressions.
852To include a literal
853.B ]
854place it first in the list.
855Similarly, to include a literal
856.B ^
857place it anywhere but first.
858Finally, to include a literal
859.B \-
860place it last.
861.SS Anchoring
862The caret
863.B ^
864and the dollar sign
865.B $
866are meta-characters that respectively match the empty string at the
867beginning and end of a line.
868.SS "The Backslash Character and Special Expressions"
869The symbols
870.B \e<
871and
872.B \e>
873respectively match the empty string at the beginning and end of a word.
874The symbol
875.B \eb
876matches the empty string at the edge of a word,
877and
878.B \eB
879matches the empty string provided it's
880.I not
881at the edge of a word.
882The symbol
883.B \ew
884is a synonym for
885.B [_[:alnum:]]
886and
887.B \eW
888is a synonym for
889.BR [^_[:alnum:]] .
890.SS Repetition
891A regular expression may be followed by one of several repetition operators:
892.PD 0
893.TP
894.B ?
895The preceding item is optional and matched at most once.
896.TP
897.B *
898The preceding item will be matched zero or more times.
899.TP
900.B +
901The preceding item will be matched one or more times.
902.TP
903.BI { n }
904The preceding item is matched exactly
905.I n
906times.
907.TP
908.BI { n ,}
909The preceding item is matched
910.I n
911or more times.
912.TP
913.BI {, m }
914The preceding item is matched at most
915.I m
916times.
917This is a GNU extension.
918.TP
919.BI { n , m }
920The preceding item is matched at least
921.I n
922times, but not more than
923.I m
924times.
925.PD
926.SS Concatenation
927Two regular expressions may be concatenated; the resulting
928regular expression matches any string formed by concatenating
929two substrings that respectively match the concatenated
930expressions.
931.SS Alternation
932Two regular expressions may be joined by the infix operator
933.BR | ;
934the resulting regular expression matches any string matching
935either alternate expression.
936.SS Precedence
937Repetition takes precedence over concatenation, which in turn
938takes precedence over alternation.
939A whole expression may be enclosed in parentheses
940to override these precedence rules and form a subexpression.
941.SS "Back-references and Subexpressions"
942The back-reference
943.BI \e n\c
944\&, where
945.I n
946is a single digit, matches the substring
947previously matched by the
948.IR n th
949parenthesized subexpression of the regular expression.
950.SS "Basic vs Extended Regular Expressions"
951In basic regular expressions the meta-characters
952.BR ? ,
953.BR + ,
954.BR { ,
955.BR | ,
956.BR ( ,
957and
958.BR )
959lose their special meaning; instead use the backslashed
960versions
961.BR \e? ,
962.BR \e+ ,
963.BR \e{ ,
964.BR \e| ,
965.BR \e( ,
966and
967.BR \e) .
968.
969.SH "EXIT STATUS"
970Normally the exit status is 0 if a line is selected, 1 if no lines
971were selected, and 2 if an error occurred. However, if the
972.B \-q
973or
974.B \-\^\-quiet
975or
976.B \-\^\-silent
977is used and a line is selected, the exit status is 0 even if an error
978occurred.
979.
980.SH ENVIRONMENT
981The behavior of
982.B grep
983is affected by the following environment variables.
984.PP
985The locale for category
986.BI LC_ foo
987is specified by examining the three environment variables
988.BR LC_ALL ,
989.BR LC_\fIfoo\fP ,
990.BR LANG ,
991in that order.
992The first of these variables that is set specifies the locale.
993For example, if
994.B LC_ALL
995is not set, but
996.B LC_MESSAGES
997is set to
998.BR pt_BR ,
999then the Brazilian Portuguese locale is used for the
1000.B LC_MESSAGES
1001category.
1002The C locale is used if none of these environment variables are set,
1003if the locale catalog is not installed, or if
1004.B grep
1005was not compiled with national language support (NLS).
1006The shell command
1007.B "locale \-a"
1008lists locales that are currently available.
1009.TP
1010.B GREP_COLOR
1011This variable specifies the color used to highlight matched (non-empty) text.
1012It is deprecated in favor of
1013.BR GREP_COLORS ,
1014but still supported.
1015The
1016.BR mt ,
1017.BR ms ,
1018and
1019.B mc
1020capabilities of
1021.B GREP_COLORS
1022have priority over it.
1023It can only specify the color used to highlight
1024the matching non-empty text in any matching line
1025(a selected line when the
1026.B \-v
1027command-line option is omitted,
1028or a context line when
1029.B \-v
1030is specified).
1031The default is
1032.BR 01;31 ,
1033which means a bold red foreground text on the terminal's default background.
1034.TP
1035.B GREP_COLORS
1036Specifies the colors and other attributes
1037used to highlight various parts of the output.
1038Its value is a colon-separated list of capabilities
1039that defaults to
1040.B ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36
1041with the
1042.B rv
1043and
1044.B ne
1045boolean capabilities omitted (i.e., false).
1046Supported capabilities are as follows.
1047.RS
1048.TP
1049.B sl=
1050SGR substring for whole selected lines
1051(i.e.,
1052matching lines when the
1053.B \-v
1054command-line option is omitted,
1055or non-matching lines when
1056.B \-v
1057is specified).
1058If however the boolean
1059.B rv
1060capability
1061and the
1062.B \-v
1063command-line option are both specified,
1064it applies to context matching lines instead.
1065The default is empty (i.e., the terminal's default color pair).
1066.TP
1067.B cx=
1068SGR substring for whole context lines
1069(i.e.,
1070non-matching lines when the
1071.B \-v
1072command-line option is omitted,
1073or matching lines when
1074.B \-v
1075is specified).
1076If however the boolean
1077.B rv
1078capability
1079and the
1080.B \-v
1081command-line option are both specified,
1082it applies to selected non-matching lines instead.
1083The default is empty (i.e., the terminal's default color pair).
1084.TP
1085.B rv
1086Boolean value that reverses (swaps) the meanings of
1087the
1088.B sl=
1089and
1090.B cx=
1091capabilities
1092when the
1093.B \-v
1094command-line option is specified.
1095The default is false (i.e., the capability is omitted).
1096.TP
1097.B mt=01;31
1098SGR substring for matching non-empty text in any matching line
1099(i.e.,
1100a selected line when the
1101.B \-v
1102command-line option is omitted,
1103or a context line when
1104.B \-v
1105is specified).
1106Setting this is equivalent to setting both
1107.B ms=
1108and
1109.B mc=
1110at once to the same value.
1111The default is a bold red text foreground over the current line background.
1112.TP
1113.B ms=01;31
1114SGR substring for matching non-empty text in a selected line.
1115(This is only used when the
1116.B \-v
1117command-line option is omitted.)
1118The effect of the
1119.B sl=
1120(or
1121.B cx=
1122if
1123.BR rv )
1124capability remains active when this kicks in.
1125The default is a bold red text foreground over the current line background.
1126.TP
1127.B mc=01;31
1128SGR substring for matching non-empty text in a context line.
1129(This is only used when the
1130.B \-v
1131command-line option is specified.)
1132The effect of the
1133.B cx=
1134(or
1135.B sl=
1136if
1137.BR rv )
1138capability remains active when this kicks in.
1139The default is a bold red text foreground over the current line background.
1140.TP
1141.B fn=35
1142SGR substring for file names prefixing any content line.
1143The default is a magenta text foreground over the terminal's default background.
1144.TP
1145.B ln=32
1146SGR substring for line numbers prefixing any content line.
1147The default is a green text foreground over the terminal's default background.
1148.TP
1149.B bn=32
1150SGR substring for byte offsets prefixing any content line.
1151The default is a green text foreground over the terminal's default background.
1152.TP
1153.B se=36
1154SGR substring for separators that are inserted
1155between selected line fields
1156.RB ( : ),
1157between context line fields,
1158.RB ( \- ),
1159and between groups of adjacent lines when nonzero context is specified
1160.RB ( \-\^\- ).
1161The default is a cyan text foreground over the terminal's default background.
1162.TP
1163.B ne
1164Boolean value that prevents clearing to the end of line
1165using Erase in Line (EL) to Right
1166.RB ( \e33[K )
1167each time a colorized item ends.
1168This is needed on terminals on which EL is not supported.
1169It is otherwise useful on terminals
1170for which the
1171.B back_color_erase
1172.RB ( bce )
1173boolean terminfo capability does not apply,
1174when the chosen highlight colors do not affect the background,
1175or when EL is too slow or causes too much flicker.
1176The default is false (i.e., the capability is omitted).
1177.PP
1178Note that boolean capabilities have no
1179.BR = .\|.\|.\&
1180part.
1181They are omitted (i.e., false) by default and become true when specified.
1182.PP
1183See the Select Graphic Rendition (SGR) section
1184in the documentation of the text terminal that is used
1185for permitted values and their meaning as character attributes.
1186These substring values are integers in decimal representation
1187and can be concatenated with semicolons.
1188.B grep
1189takes care of assembling the result
1190into a complete SGR sequence
1191.RB ( \e33[ .\|.\|. m ).
1192Common values to concatenate include
1193.B 1
1194for bold,
1195.B 4
1196for underline,
1197.B 5
1198for blink,
1199.B 7
1200for inverse,
1201.B 39
1202for default foreground color,
1203.B 30
1204to
1205.B 37
1206for foreground colors,
1207.B 90
1208to
1209.B 97
1210for 16-color mode foreground colors,
1211.B 38;5;0
1212to
1213.B 38;5;255
1214for 88-color and 256-color modes foreground colors,
1215.B 49
1216for default background color,
1217.B 40
1218to
1219.B 47
1220for background colors,
1221.B 100
1222to
1223.B 107
1224for 16-color mode background colors, and
1225.B 48;5;0
1226to
1227.B 48;5;255
1228for 88-color and 256-color modes background colors.
1229.RE
1230.TP
1231\fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fP
1232These variables specify the locale for the
1233.B LC_COLLATE
1234category,
1235which determines the collating sequence
1236used to interpret range expressions like
1237.BR [a\-z] .
1238.TP
1239\fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
1240These variables specify the locale for the
1241.B LC_CTYPE
1242category,
1243which determines the type of characters,
1244e.g., which characters are whitespace.
1245This category also determines the character encoding, that is, whether
1246text is encoded in UTF-8, ASCII, or some other encoding. In the C or
1247POSIX locale, all characters are encoded as a single byte and every
1248byte is a valid character.
1249.TP
1250\fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
1251These variables specify the locale for the
1252.B LC_MESSAGES
1253category,
1254which determines the language that
1255.B grep
1256uses for messages.
1257The default C locale uses American English messages.
1258.TP
1259.B POSIXLY_CORRECT
1260If set,
1261.B grep
1262behaves as POSIX requires; otherwise,
1263.B grep
1264behaves more like other GNU programs.
1265POSIX requires that options that follow file names must be
1266treated as file names; by default, such options are permuted to the
1267front of the operand list and are treated as options.
1268Also, POSIX requires that unrecognized options be diagnosed as
1269\*(lqillegal\*(rq, but since they are not really against the law the default
1270is to diagnose them as \*(lqinvalid\*(rq.
1271.B POSIXLY_CORRECT
1272also disables \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP,
1273described below.
1274.TP
1275\fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP
1276(Here
1277.I N
1278is
1279.BR grep 's
1280numeric process ID.) If the
1281.IR i th
1282character of this environment variable's value is
1283.BR 1 ,
1284do not consider the
1285.IR i th
1286operand of
1287.B grep
1288to be an option, even if it appears to be one.
1289A shell can put this variable in the environment for each command it runs,
1290specifying which operands are the results of file name wildcard
1291expansion and therefore should not be treated as options.
1292This behavior is available only with the GNU C library, and only
1293when
1294.B POSIXLY_CORRECT
1295is not set.
1296.
1297.SH NOTES
1298This man page is maintained only fitfully;
1299the full documentation is often more up-to-date.
1300.
1301.SH COPYRIGHT
1302Copyright 1998-2000, 2002, 2005-2021 Free Software Foundation, Inc.
1303.PP
1304This is free software;
1305see the source for copying conditions.
1306There is NO warranty;
1307not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1308.
1309.SH BUGS
1310.SS "Reporting Bugs"
1311Email bug reports to
1312.MT bug-grep@gnu.org
1313the bug-reporting address
1314.ME .
1315An
1316.UR https://lists.gnu.org/mailman/listinfo/bug-grep
1317email archive
1318.UE
1319and a
1320.UR https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep
1321bug tracker
1322.UE
1323are available.
1324.SS "Known Bugs"
1325Large repetition counts in the
1326.BI { n , m }
1327construct may cause
1328.B grep
1329to use lots of memory.
1330In addition,
1331certain other obscure regular expressions require exponential time
1332and space, and may cause
1333.B grep
1334to run out of memory.
1335.PP
1336Back-references are very slow, and may require exponential time.
1337.
1338.SH EXAMPLE
1339The following example outputs the location and contents of any line
1340containing \*(lqf\*(rq and ending in \*(lq.c\*(rq,
1341within all files in the current directory whose names
1342contain \*(lqg\*(rq and end in \*(lq.h\*(rq.
1343The
1344.B \-n
1345option outputs line numbers, the
1346.B \-\-
1347argument treats expansions of \*(lq*g*.h\*(rq starting with \*(lq\-\*(rq
1348as file names not options,
1349and the empty file /dev/null causes file names to be output
1350even if only one file name happens to be of the form \*(lq*g*.h\*(rq.
1351.PP
1352.in +2n
1353.EX
1354$ \fBgrep\fP \-n \-\- 'f.*\e.c$' *g*.h /dev/null
1355argmatch.h:1:/* definitions and prototypes for argmatch.c
1356.EE
1357.in
1358.PP
1359The only line that matches is line 1 of argmatch.h.
1360Note that the regular expression syntax used in the pattern differs
1361from the globbing syntax that the shell uses to match file names.
1362.
1363.SH "SEE ALSO"
1364.SS "Regular Manual Pages"
1365.BR awk (1),
1366.BR cmp (1),
1367.BR diff (1),
1368.BR find (1),
1369.BR perl (1),
1370.BR sed (1),
1371.BR sort (1),
1372.BR xargs (1),
1373.BR read (2),
1374.BR pcre (3),
1375.BR pcresyntax (3),
1376.BR pcrepattern (3),
1377.BR terminfo (5),
1378.BR glob (7),
1379.BR regex (7)
1380.SS "Full Documentation"
1381A
1382.UR https://www.gnu.org/software/grep/manual/
1383complete manual
1384.UE
1385is available.
1386If the
1387.B info
1388and
1389.B grep
1390programs are properly installed at your site, the command
1391.IP
1392.B info grep
1393.PP
1394should give you access to the complete manual.
1395.
1396.\" Work around problems with some troff -man implementations.
1397.br
1398.
1399.\" Format for Emacs-maintained Dt string defined at this file's start.
1400.\" Local variables:
1401.\" time-stamp-format: "%:y-%02m-%02d"
1402.\" End:
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette