Debian Codegraph @

upstream/opensuse-leap-16-1/man1/grep.1

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