Complete upgrade of XZ 5.0.4 => 5.0.7
[dragonfly.git] / contrib / grep / doc / grep.texi
1 \input texinfo  @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename grep.info
4 @include version.texi
5 @settitle GNU Grep @value{VERSION}
6
7 @c Combine indices.
8 @syncodeindex ky cp
9 @syncodeindex pg cp
10 @syncodeindex tp cp
11 @defcodeindex op
12 @syncodeindex op cp
13 @syncodeindex vr cp
14 @c %**end of header
15
16 @copying
17 This manual is for @command{grep}, a pattern matching engine.
18
19 Copyright @copyright{} 1999-2002, 2005, 2008-2012 Free Software Foundation,
20 Inc.
21
22 @quotation
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.3 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
27 Texts.  A copy of the license is included in the section entitled
28 ``GNU Free Documentation License''.
29 @end quotation
30 @end copying
31
32 @dircategory Text creation and manipulation
33 @direntry
34 * grep: (grep).                 Print lines matching a pattern.
35 @end direntry
36
37 @titlepage
38 @title GNU Grep: Print lines matching a pattern
39 @subtitle version @value{VERSION}, @value{UPDATED}
40 @author Alain Magloire et al.
41 @page
42 @vskip 0pt plus 1filll
43 @insertcopying
44 @end titlepage
45
46 @contents
47
48
49 @ifnottex
50 @node Top
51 @top grep
52
53 @command{grep} prints lines that contain a match for a pattern.
54
55 This manual is for version @value{VERSION} of GNU Grep.
56
57 @insertcopying
58 @end ifnottex
59
60 @menu
61 * Introduction::                Introduction.
62 * Invoking::                    Command-line options, environment, exit status.
63 * Regular Expressions::         Regular Expressions.
64 * Usage::                       Examples.
65 * Reporting Bugs::              Reporting Bugs.
66 * Copying::                     License terms for this manual.
67 * Index::                       Combined index.
68 @end menu
69
70
71 @node Introduction
72 @chapter Introduction
73
74 @cindex searching for a pattern
75
76 @command{grep} searches input files
77 for lines containing a match to a given pattern list.
78 When it finds a match in a line,
79 it copies the line to standard output (by default),
80 or produces whatever other sort of output you have requested with options.
81
82 Though @command{grep} expects to do the matching on text,
83 it has no limits on input line length other than available memory,
84 and it can match arbitrary characters within a line.
85 If the final byte of an input file is not a newline,
86 @command{grep} silently supplies one.
87 Since newline is also a separator for the list of patterns,
88 there is no way to match newline characters in a text.
89
90
91 @node Invoking
92 @chapter Invoking @command{grep}
93
94 The general synopsis of the @command{grep} command line is
95
96 @example
97 grep @var{options} @var{pattern} @var{input_file_names}
98 @end example
99
100 @noindent
101 There can be zero or more @var{options}.
102 @var{pattern} will only be seen as such
103 (and not as an @var{input_file_name})
104 if it wasn't already specified within @var{options}
105 (by using the @samp{-e@ @var{pattern}}
106 or @samp{-f@ @var{file}} options).
107 There can be zero or more @var{input_file_names}.
108
109 @menu
110 * Command-line Options::        Short and long names, grouped by category.
111 * Environment Variables::       POSIX, GNU generic, and GNU grep specific.
112 * Exit Status::                 Exit status returned by @command{grep}.
113 * grep Programs::               @command{grep} programs.
114 @end menu
115
116 @node Command-line Options
117 @section Command-line Options
118
119 @command{grep} comes with a rich set of options:
120 some from POSIX and some being GNU extensions.
121 Long option names are always a GNU extension,
122 even for options that are from POSIX specifications.
123 Options that are specified by POSIX,
124 under their short names,
125 are explicitly marked as such
126 to facilitate POSIX-portable programming.
127 A few option names are provided
128 for compatibility with older or more exotic implementations.
129
130 @menu
131 * Generic Program Information::
132 * Matching Control::
133 * General Output Control::
134 * Output Line Prefix Control::
135 * Context Line Control::
136 * File and Directory Selection::
137 * Other Options::
138 @end menu
139
140 Several additional options control
141 which variant of the @command{grep} matching engine is used.
142 @xref{grep Programs}.
143
144 @node Generic Program Information
145 @subsection Generic Program Information
146
147 @table @option
148
149 @item --help
150 @opindex --help
151 @cindex usage summary, printing
152 Print a usage message briefly summarizing the command-line options
153 and the bug-reporting address, then exit.
154
155 @item -V
156 @itemx --version
157 @opindex -V
158 @opindex --version
159 @cindex version, printing
160 Print the version number of @command{grep} to the standard output stream.
161 This version number should be included in all bug reports.
162
163 @end table
164
165 @node Matching Control
166 @subsection Matching Control
167
168 @table @option
169
170 @item -e @var{pattern}
171 @itemx --regexp=@var{pattern}
172 @opindex -e
173 @opindex --regexp=@var{pattern}
174 @cindex pattern list
175 Use @var{pattern} as the pattern.
176 This can be used to specify multiple search patterns,
177 or to protect a pattern beginning with a @samp{-}.
178 (@option{-e} is specified by POSIX.)
179
180 @item -f @var{file}
181 @itemx --file=@var{file}
182 @opindex -f
183 @opindex --file
184 @cindex pattern from file
185 Obtain patterns from @var{file}, one per line.
186 The empty file contains zero patterns, and therefore matches nothing.
187 (@option{-f} is specified by POSIX.)
188
189 @item -i
190 @itemx -y
191 @itemx --ignore-case
192 @opindex -i
193 @opindex -y
194 @opindex --ignore-case
195 @cindex case insensitive search
196 Ignore case distinctions in both the pattern and the input files.
197 @option{-y} is an obsolete synonym that is provided for compatibility.
198 (@option{-i} is specified by POSIX.)
199
200 @item -v
201 @itemx --invert-match
202 @opindex -v
203 @opindex --invert-match
204 @cindex invert matching
205 @cindex print non-matching lines
206 Invert the sense of matching, to select non-matching lines.
207 (@option{-v} is specified by POSIX.)
208
209 @item -w
210 @itemx --word-regexp
211 @opindex -w
212 @opindex --word-regexp
213 @cindex matching whole words
214 Select only those lines containing matches that form whole words.
215 The test is that the matching substring must either
216 be at the beginning of the line,
217 or preceded by a non-word constituent character.
218 Similarly,
219 it must be either at the end of the line
220 or followed by a non-word constituent character.
221 Word-constituent characters are letters, digits, and the underscore.
222
223 @item -x
224 @itemx --line-regexp
225 @opindex -x
226 @opindex --line-regexp
227 @cindex match the whole line
228 Select only those matches that exactly match the whole line.
229 (@option{-x} is specified by POSIX.)
230
231 @end table
232
233 @node General Output Control
234 @subsection General Output Control
235
236 @table @option
237
238 @item -c
239 @itemx --count
240 @opindex -c
241 @opindex --count
242 @cindex counting lines
243 Suppress normal output;
244 instead print a count of matching lines for each input file.
245 With the @option{-v} (@option{--invert-match}) option,
246 count non-matching lines.
247 (@option{-c} is specified by POSIX.)
248
249 @item --color[=@var{WHEN}]
250 @itemx --colour[=@var{WHEN}]
251 @opindex --color
252 @opindex --colour
253 @cindex highlight, color, colour
254 Surround the matched (non-empty) strings, matching lines, context lines,
255 file names, line numbers, byte offsets, and separators (for fields and
256 groups of context lines) with escape sequences to display them in color
257 on the terminal.
258 The colors are defined by the environment variable @env{GREP_COLORS}
259 and default to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36}
260 for bold red matched text, magenta file names, green line numbers,
261 green byte offsets, cyan separators, and default terminal colors otherwise.
262 The deprecated environment variable @env{GREP_COLOR} is still supported,
263 but its setting does not have priority;
264 it defaults to @samp{01;31} (bold red)
265 which only covers the color for matched text.
266 @var{WHEN} is @samp{never}, @samp{always}, or @samp{auto}.
267
268 @item -L
269 @itemx --files-without-match
270 @opindex -L
271 @opindex --files-without-match
272 @cindex files which don't match
273 Suppress normal output;
274 instead print the name of each input file from which
275 no output would normally have been printed.
276 The scanning of each file stops on the first match.
277
278 @item -l
279 @itemx --files-with-matches
280 @opindex -l
281 @opindex --files-with-matches
282 @cindex names of matching files
283 Suppress normal output;
284 instead print the name of each input file from which
285 output would normally have been printed.
286 The scanning of each file stops on the first match.
287 (@option{-l} is specified by POSIX.)
288
289 @item -m @var{num}
290 @itemx --max-count=@var{num}
291 @opindex -m
292 @opindex --max-count
293 @cindex max-count
294 Stop reading a file after @var{num} matching lines.
295 If the input is standard input from a regular file,
296 and @var{num} matching lines are output,
297 @command{grep} ensures that the standard input is positioned
298 just after the last matching line before exiting,
299 regardless of the presence of trailing context lines.
300 This enables a calling process to resume a search.
301 For example, the following shell script makes use of it:
302
303 @example
304 while grep -m 1 PATTERN
305 do
306   echo xxxx
307 done < FILE
308 @end example
309
310 But the following probably will not work because a pipe is not a regular
311 file:
312
313 @example
314 # This probably will not work.
315 cat FILE |
316 while grep -m 1 PATTERN
317 do
318   echo xxxx
319 done
320 @end example
321
322 When @command{grep} stops after @var{num} matching lines,
323 it outputs any trailing context lines.
324 Since context does not include matching lines,
325 @command{grep} will stop when it encounters another matching line.
326 When the @option{-c} or @option{--count} option is also used,
327 @command{grep} does not output a count greater than @var{num}.
328 When the @option{-v} or @option{--invert-match} option is also used,
329 @command{grep} stops after outputting @var{num} non-matching lines.
330
331 @item -o
332 @itemx --only-matching
333 @opindex -o
334 @opindex --only-matching
335 @cindex only matching
336 Print only the matched (non-empty) parts of matching lines,
337 with each such part on a separate output line.
338
339 @item -q
340 @itemx --quiet
341 @itemx --silent
342 @opindex -q
343 @opindex --quiet
344 @opindex --silent
345 @cindex quiet, silent
346 Quiet; do not write anything to standard output.
347 Exit immediately with zero status if any match is found,
348 even if an error was detected.
349 Also see the @option{-s} or @option{--no-messages} option.
350 (@option{-q} is specified by POSIX.)
351
352 @item -s
353 @itemx --no-messages
354 @opindex -s
355 @opindex --no-messages
356 @cindex suppress error messages
357 Suppress error messages about nonexistent or unreadable files.
358 Portability note:
359 unlike GNU @command{grep},
360 7th Edition Unix @command{grep} did not conform to POSIX,
361 because it lacked @option{-q}
362 and its @option{-s} option behaved like
363 GNU @command{grep}'s @option{-q} option.@footnote{Of course, 7th Edition
364 Unix predated POSIX by several years!}
365 USG-style @command{grep} also lacked @option{-q}
366 but its @option{-s} option behaved like GNU @command{grep}'s.
367 Portable shell scripts should avoid both
368 @option{-q} and @option{-s} and should redirect
369 standard and error output to @file{/dev/null} instead.
370 (@option{-s} is specified by POSIX.)
371
372 @end table
373
374 @node Output Line Prefix Control
375 @subsection Output Line Prefix Control
376
377 When several prefix fields are to be output,
378 the order is always file name, line number, and byte offset,
379 regardless of the order in which these options were specified.
380
381 @table @option
382
383 @item -b
384 @itemx --byte-offset
385 @opindex -b
386 @opindex --byte-offset
387 @cindex byte offset
388 Print the 0-based byte offset within the input file
389 before each line of output.
390 If @option{-o} (@option{--only-matching}) is specified,
391 print the offset of the matching part itself.
392 When @command{grep} runs on MS-DOS or MS-Windows,
393 the printed byte offsets depend on whether
394 the @option{-u} (@option{--unix-byte-offsets}) option is used;
395 see below.
396
397 @item -H
398 @itemx --with-filename
399 @opindex -H
400 @opindex --with-filename
401 @cindex with filename prefix
402 Print the file name for each match.
403 This is the default when there is more than one file to search.
404
405 @item -h
406 @itemx --no-filename
407 @opindex -h
408 @opindex --no-filename
409 @cindex no filename prefix
410 Suppress the prefixing of file names on output.
411 This is the default when there is only one file
412 (or only standard input) to search.
413
414 @item --label=@var{LABEL}
415 @opindex --label
416 @cindex changing name of standard input
417 Display input actually coming from standard input
418 as input coming from file @var{LABEL}.  This is
419 especially useful when implementing tools like
420 @command{zgrep}; e.g.:
421
422 @example
423 gzip -cd foo.gz | grep --label=foo -H something
424 @end example
425
426 @item -n
427 @itemx --line-number
428 @opindex -n
429 @opindex --line-number
430 @cindex line numbering
431 Prefix each line of output with the 1-based line number within its input file.
432 (@option{-n} is specified by POSIX.)
433
434 @item -T
435 @itemx --initial-tab
436 @opindex -T
437 @opindex --initial-tab
438 @cindex tab-aligned content lines
439 Make sure that the first character of actual line content lies on a tab stop,
440 so that the alignment of tabs looks normal.
441 This is useful with options that prefix their output to the actual content:
442 @option{-H}, @option{-n}, and @option{-b}.
443 In order to improve the probability that lines
444 from a single file will all start at the same column,
445 this also causes the line number and byte offset (if present)
446 to be printed in a minimum-size field width.
447
448 @item -u
449 @itemx --unix-byte-offsets
450 @opindex -u
451 @opindex --unix-byte-offsets
452 @cindex MS-DOS/MS-Windows byte offsets
453 @cindex byte offsets, on MS-DOS/MS-Windows
454 Report Unix-style byte offsets.
455 This option causes @command{grep} to report byte offsets
456 as if the file were a Unix-style text file,
457 i.e., the byte offsets ignore the @code{CR} characters that were stripped.
458 This will produce results identical
459 to running @command{grep} on a Unix machine.
460 This option has no effect unless the @option{-b} option is also used;
461 it has no effect on platforms other than MS-DOS and MS-Windows.
462
463 @item -Z
464 @itemx --null
465 @opindex -Z
466 @opindex --null
467 @cindex zero-terminated file names
468 Output a zero byte (the ASCII @code{NUL} character)
469 instead of the character that normally follows a file name.
470 For example,
471 @samp{grep -lZ} outputs a zero byte after each file name
472 instead of the usual newline.
473 This option makes the output unambiguous,
474 even in the presence of file names containing unusual characters like newlines.
475 This option can be used with commands like
476 @samp{find -print0}, @samp{perl -0}, @samp{sort -z}, and @samp{xargs -0}
477 to process arbitrary file names,
478 even those that contain newline characters.
479
480 @end table
481
482 @node Context Line Control
483 @subsection Context Line Control
484
485 Regardless of how these options are set,
486 @command{grep} will never print any given line more than once.
487 If the @option{-o} (@option{--only-matching}) option is specified,
488 these options have no effect and a warning is given upon their use.
489
490 @table @option
491
492 @item -A @var{num}
493 @itemx --after-context=@var{num}
494 @opindex -A
495 @opindex --after-context
496 @cindex after context
497 @cindex context lines, after match
498 Print @var{num} lines of trailing context after matching lines.
499
500 @item -B @var{num}
501 @itemx --before-context=@var{num}
502 @opindex -B
503 @opindex --before-context
504 @cindex before context
505 @cindex context lines, before match
506 Print @var{num} lines of leading context before matching lines.
507
508 @item -C @var{num}
509 @itemx -@var{num}
510 @itemx --context=@var{num}
511 @opindex -C
512 @opindex --context
513 @opindex -@var{num}
514 @cindex context
515 Print @var{num} lines of leading and trailing output context.
516
517 @item --group-separator=@var{string}
518 @opindex --group-separator
519 @cindex group separator
520 When @option{-A}, @option{-B} or @option{-C} are in use,
521 print @var{string} instead of @option{--} around disjoint groups
522 of lines.
523
524 @item --no-group-separator
525 @opindex --group-separator
526 @cindex group separator
527 When @option{-A}, @option{-B} or @option{-C} are in use,
528 print disjoint groups of lines adjacent to each other.
529
530 @end table
531
532 Here are some points about how @command{grep} chooses
533 the separator to print between prefix fields and line content:
534
535 @itemize @bullet
536 @item
537 Matching lines normally use @samp{:} as a separator
538 between prefix fields and actual line content.
539
540 @item
541 Context (i.e., non-matching) lines use @samp{-} instead.
542
543 @item
544 When no context is specified,
545 matching lines are simply output one right after another.
546
547 @item
548 When nonzero context is specified,
549 lines that are adjacent in the input form a group
550 and are output one right after another, while
551 a separator appears by default between disjoint groups on a line
552 of its own and without any prefix.
553
554 @item
555 The default separator
556 is @samp{--}, however whether to include it and its appearance
557 can be changed with the options above.
558
559 @item
560 Each group may contain
561 several matching lines when they are close enough to each other
562 that two otherwise adjacent but divided groups connect
563 and can just merge into a single contiguous one.
564 @end itemize
565
566 @node File and Directory Selection
567 @subsection File and Directory Selection
568
569 @table @option
570
571 @item -a
572 @itemx --text
573 @opindex -a
574 @opindex --text
575 @cindex suppress binary data
576 @cindex binary files
577 Process a binary file as if it were text;
578 this is equivalent to the @samp{--binary-files=text} option.
579
580 @item --binary-files=@var{type}
581 @opindex --binary-files
582 @cindex binary files
583 If a file's allocation metadata or its first few bytes
584 indicate that the file contains binary data,
585 assume that the file is of type @var{type}.
586 By default, @var{type} is @samp{binary},
587 and @command{grep} normally outputs either
588 a one-line message saying that a binary file matches,
589 or no message if there is no match.
590
591 If @var{type} is @samp{without-match},
592 @command{grep} assumes that a binary file does not match;
593 this is equivalent to the @option{-I} option.
594
595 If @var{type} is @samp{text},
596 @command{grep} processes a binary file as if it were text;
597 this is equivalent to the @option{-a} option.
598
599 @emph{Warning:} @samp{--binary-files=text} might output binary garbage,
600 which can have nasty side effects
601 if the output is a terminal and
602 if the terminal driver interprets some of it as commands.
603
604 @item -D @var{action}
605 @itemx --devices=@var{action}
606 @opindex -D
607 @opindex --devices
608 @cindex device search
609 If an input file is a device, FIFO, or socket, use @var{action} to process it.
610 If @var{action} is @samp{read},
611 all devices are read just as if they were ordinary files.
612 If @var{action} is @samp{skip},
613 devices, FIFOs, and sockets are silently skipped.
614 By default, devices are read if they are on the command line or if the
615 @option{-R} (@option{--dereference-recursive}) option is used, and are
616 skipped if they are encountered recursively and the @option{-r}
617 (@option{--recursive}) option is used.
618 This option has no effect on a file that is read via standard input.
619
620 @item -d @var{action}
621 @itemx --directories=@var{action}
622 @opindex -d
623 @opindex --directories
624 @cindex directory search
625 @cindex symbolic links
626 If an input file is a directory, use @var{action} to process it.
627 By default, @var{action} is @samp{read},
628 which means that directories are read just as if they were ordinary files
629 (some operating systems and file systems disallow this,
630 and will cause @command{grep}
631 to print error messages for every directory or silently skip them).
632 If @var{action} is @samp{skip}, directories are silently skipped.
633 If @var{action} is @samp{recurse},
634 @command{grep} reads all files under each directory, recursively,
635 following command-line symbolic links and skipping other symlinks;
636 this is equivalent to the @option{-r} option.
637
638 @item --exclude=@var{glob}
639 @opindex --exclude
640 @cindex exclude files
641 @cindex searching directory trees
642 Skip files whose base name matches @var{glob}
643 (using wildcard matching).
644 A file-name glob can use
645 @samp{*}, @samp{?}, and @samp{[}...@samp{]} as wildcards,
646 and @code{\} to quote a wildcard or backslash character literally.
647
648 @item --exclude-from=@var{file}
649 @opindex --exclude-from
650 @cindex exclude files
651 @cindex searching directory trees
652 Skip files whose base name matches any of the file-name globs
653 read from @var{file} (using wildcard matching as described
654 under @option{--exclude}).
655
656 @item --exclude-dir=@var{dir}
657 @opindex --exclude-dir
658 @cindex exclude directories
659 Exclude directories matching the pattern @var{dir} from recursive
660 directory searches.
661
662 @item -I
663 Process a binary file as if it did not contain matching data;
664 this is equivalent to the @samp{--binary-files=without-match} option.
665
666 @item --include=@var{glob}
667 @opindex --include
668 @cindex include files
669 @cindex searching directory trees
670 Search only files whose base name matches @var{glob}
671 (using wildcard matching as described under @option{--exclude}).
672
673 @item -r
674 @itemx --recursive
675 @opindex -r
676 @opindex --recursive
677 @cindex recursive search
678 @cindex searching directory trees
679 @cindex symbolic links
680 For each directory operand,
681 read and process all files in that directory, recursively.
682 Follow symbolic links on the command line, but skip symlinks
683 that are encountered recursively.
684 This is the same as the @samp{--directories=recurse} option.
685
686 @item -R
687 @itemx --dereference-recursive
688 @opindex -R
689 @opindex --dereference-recursive
690 @cindex recursive search
691 @cindex searching directory trees
692 @cindex symbolic links
693 For each directory operand, read and process all files in that
694 directory, recursively, following all symbolic links.
695
696 @end table
697
698 @node Other Options
699 @subsection Other Options
700
701 @table @option
702
703 @item --line-buffered
704 @opindex --line-buffered
705 @cindex line buffering
706 Use line buffering on output.
707 This can cause a performance penalty.
708
709 @item --mmap
710 @opindex --mmap
711 @cindex memory mapped input
712 This option is deprecated and now elicits a warning, but is otherwise a no-op.
713 It used to make @command{grep} read
714 input with the @code{mmap} system call, instead of the default @code{read}
715 system call.  On modern systems, @code{mmap} would rarely if ever yield
716 better performance.
717
718 @item -U
719 @itemx --binary
720 @opindex -U
721 @opindex --binary
722 @cindex MS-DOS/MS-Windows binary files
723 @cindex binary files, MS-DOS/MS-Windows
724 Treat the file(s) as binary.
725 By default, under MS-DOS and MS-Windows,
726 @command{grep} guesses whether a file is text or binary
727 as described for the @option{--binary-files} option.
728 If @command{grep} decides the file is a text file,
729 it strips the @code{CR} characters from the original file contents
730 (to make regular expressions with @code{^} and @code{$} work correctly).
731 Specifying @option{-U} overrules this guesswork,
732 causing all files to be read and passed to the matching mechanism verbatim;
733 if the file is a text file with @code{CR/LF} pairs at the end of each line,
734 this will cause some regular expressions to fail.
735 This option has no effect
736 on platforms other than MS-DOS and MS-Windows.
737
738 @item -z
739 @itemx --null-data
740 @opindex -z
741 @opindex --null-data
742 @cindex zero-terminated lines
743 Treat the input as a set of lines, each terminated by a zero byte (the
744 ASCII @code{NUL} character) instead of a newline.
745 Like the @option{-Z} or @option{--null} option,
746 this option can be used with commands like
747 @samp{sort -z} to process arbitrary file names.
748
749 @end table
750
751 @node Environment Variables
752 @section Environment Variables
753
754 The behavior of @command{grep} is affected
755 by the following environment variables.
756
757 The locale for category @w{@code{LC_@var{foo}}}
758 is specified by examining the three environment variables
759 @env{LC_ALL}, @w{@env{LC_@var{foo}}}, and @env{LANG},
760 in that order.
761 The first of these variables that is set specifies the locale.
762 For example, if @env{LC_ALL} is not set,
763 but @env{LC_MESSAGES} is set to @samp{pt_BR},
764 then the Brazilian Portuguese locale is used
765 for the @code{LC_MESSAGES} category.
766 The @samp{C} locale is used if none of these environment variables are set,
767 if the locale catalog is not installed,
768 or if @command{grep} was not compiled
769 with national language support (NLS).
770
771 Many of the environment variables in the following list let you
772 control highlighting using
773 Select Graphic Rendition (SGR)
774 commands interpreted by the terminal or terminal emulator.
775 (See the
776 section
777 in the documentation of your text terminal
778 for permitted values and their meanings as character attributes.)
779 These substring values are integers in decimal representation
780 and can be concatenated with semicolons.
781 @command{grep} takes care of assembling the result
782 into a complete SGR sequence (@samp{\33[}...@samp{m}).
783 Common values to concatenate include
784 @samp{1} for bold,
785 @samp{4} for underline,
786 @samp{5} for blink,
787 @samp{7} for inverse,
788 @samp{39} for default foreground color,
789 @samp{30} to @samp{37} for foreground colors,
790 @samp{90} to @samp{97} for 16-color mode foreground colors,
791 @samp{38;5;0} to @samp{38;5;255}
792 for 88-color and 256-color modes foreground colors,
793 @samp{49} for default background color,
794 @samp{40} to @samp{47} for background colors,
795 @samp{100} to @samp{107} for 16-color mode background colors,
796 and @samp{48;5;0} to @samp{48;5;255}
797 for 88-color and 256-color modes background colors.
798
799 The two-letter names used in the @env{GREP_COLORS} environment variable
800 (and some of the others) refer to terminal ``capabilities,'' the ability
801 of a terminal to highlight text, or change its color, and so on.
802 These capabilities are stored in an online database and accessed by
803 the @code{terminfo} library.
804
805 @cindex environment variables
806
807 @table @env
808
809 @item GREP_OPTIONS
810 @vindex GREP_OPTIONS @r{environment variable}
811 @cindex default options environment variable
812 This variable specifies default options to be placed in front of any
813 explicit options.
814 For example, if @code{GREP_OPTIONS} is
815 @samp{--binary-files=without-match --directories=skip}, @command{grep}
816 behaves as if the two options @samp{--binary-files=without-match} and
817 @samp{--directories=skip} had been specified before
818 any explicit options.
819 Option specifications are separated by
820 whitespace.
821 A backslash escapes the next character, so it can be used to
822 specify an option containing whitespace or a backslash.
823
824 The @code{GREP_OPTIONS} value does not affect whether @command{grep}
825 without file operands searches standard input or the working
826 directory; that is affected only by command-line options.  For
827 example, the command @samp{grep PAT} searches standard input and the
828 command @samp{grep -r PAT} searches the working directory, regardless
829 of whether @code{GREP_OPTIONS} contains @option{-r}.
830
831 @item GREP_COLOR
832 @vindex GREP_COLOR @r{environment variable}
833 @cindex highlight markers
834 This variable specifies the color used to highlight matched (non-empty) text.
835 It is deprecated in favor of @env{GREP_COLORS}, but still supported.
836 The @samp{mt}, @samp{ms}, and @samp{mc} capabilities of @env{GREP_COLORS}
837 have priority over it.
838 It can only specify the color used to highlight
839 the matching non-empty text in any matching line
840 (a selected line when the @option{-v} command-line option is omitted,
841 or a context line when @option{-v} is specified).
842 The default is @samp{01;31},
843 which means a bold red foreground text on the terminal's default background.
844
845 @item GREP_COLORS
846 @vindex GREP_COLORS @r{environment variable}
847 @cindex highlight markers
848 This variable specifies the colors and other attributes
849 used to highlight various parts of the output.
850 Its value is a colon-separated list of @code{terminfo} capabilities
851 that defaults to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36}
852 with the @samp{rv} and @samp{ne} boolean capabilities omitted (i.e., false).
853 Supported capabilities are as follows.
854
855 @table @code
856 @item sl=
857 @vindex sl GREP_COLORS @r{capability}
858 SGR substring for whole selected lines
859 (i.e.,
860 matching lines when the @option{-v} command-line option is omitted,
861 or non-matching lines when @option{-v} is specified).
862 If however the boolean @samp{rv} capability
863 and the @option{-v} command-line option are both specified,
864 it applies to context matching lines instead.
865 The default is empty (i.e., the terminal's default color pair).
866
867 @item cx=
868 @vindex cx GREP_COLORS @r{capability}
869 SGR substring for whole context lines
870 (i.e.,
871 non-matching lines when the @option{-v} command-line option is omitted,
872 or matching lines when @option{-v} is specified).
873 If however the boolean @samp{rv} capability
874 and the @option{-v} command-line option are both specified,
875 it applies to selected non-matching lines instead.
876 The default is empty (i.e., the terminal's default color pair).
877
878 @item rv
879 @vindex rv GREP_COLORS @r{capability}
880 Boolean value that reverses (swaps) the meanings of
881 the @samp{sl=} and @samp{cx=} capabilities
882 when the @option{-v} command-line option is specified.
883 The default is false (i.e., the capability is omitted).
884
885 @item mt=01;31
886 @vindex mt GREP_COLORS @r{capability}
887 SGR substring for matching non-empty text in any matching line
888 (i.e.,
889 a selected line when the @option{-v} command-line option is omitted,
890 or a context line when @option{-v} is specified).
891 Setting this is equivalent to setting both @samp{ms=} and @samp{mc=}
892 at once to the same value.
893 The default is a bold red text foreground over the current line background.
894
895 @item ms=01;31
896 @vindex ms GREP_COLORS @r{capability}
897 SGR substring for matching non-empty text in a selected line.
898 (This is used only when the @option{-v} command-line option is omitted.)
899 The effect of the @samp{sl=} (or @samp{cx=} if @samp{rv}) capability
900 remains active when this takes effect.
901 The default is a bold red text foreground over the current line background.
902
903 @item mc=01;31
904 @vindex mc GREP_COLORS @r{capability}
905 SGR substring for matching non-empty text in a context line.
906 (This is used only when the @option{-v} command-line option is specified.)
907 The effect of the @samp{cx=} (or @samp{sl=} if @samp{rv}) capability
908 remains active when this takes effect.
909 The default is a bold red text foreground over the current line background.
910
911 @item fn=35
912 @vindex fn GREP_COLORS @r{capability}
913 SGR substring for file names prefixing any content line.
914 The default is a magenta text foreground over the terminal's default background.
915
916 @item ln=32
917 @vindex ln GREP_COLORS @r{capability}
918 SGR substring for line numbers prefixing any content line.
919 The default is a green text foreground over the terminal's default background.
920
921 @item bn=32
922 @vindex bn GREP_COLORS @r{capability}
923 SGR substring for byte offsets prefixing any content line.
924 The default is a green text foreground over the terminal's default background.
925
926 @item se=36
927 @vindex fn GREP_COLORS @r{capability}
928 SGR substring for separators that are inserted
929 between selected line fields (@samp{:}),
930 between context line fields (@samp{-}),
931 and between groups of adjacent lines
932 when nonzero context is specified (@samp{--}).
933 The default is a cyan text foreground over the terminal's default background.
934
935 @item ne
936 @vindex ne GREP_COLORS @r{capability}
937 Boolean value that prevents clearing to the end of line
938 using Erase in Line (EL) to Right (@samp{\33[K})
939 each time a colorized item ends.
940 This is needed on terminals on which EL is not supported.
941 It is otherwise useful on terminals
942 for which the @code{back_color_erase}
943 (@code{bce}) boolean @code{terminfo} capability does not apply,
944 when the chosen highlight colors do not affect the background,
945 or when EL is too slow or causes too much flicker.
946 The default is false (i.e., the capability is omitted).
947 @end table
948
949 Note that boolean capabilities have no @samp{=}... part.
950 They are omitted (i.e., false) by default and become true when specified.
951
952
953 @item LC_ALL
954 @itemx LC_COLLATE
955 @itemx LANG
956 @vindex LC_ALL @r{environment variable}
957 @vindex LC_COLLATE @r{environment variable}
958 @vindex LANG @r{environment variable}
959 @cindex character type
960 @cindex national language support
961 @cindex NLS
962 These variables specify the locale for the @code{LC_COLLATE} category,
963 which determines the collating sequence
964 used to interpret range expressions like @samp{[a-z]}.
965
966 @item LC_ALL
967 @itemx LC_CTYPE
968 @itemx LANG
969 @vindex LC_ALL @r{environment variable}
970 @vindex LC_CTYPE @r{environment variable}
971 @vindex LANG @r{environment variable}
972 These variables specify the locale for the @code{LC_CTYPE} category,
973 which determines the type of characters,
974 e.g., which characters are whitespace.
975
976 @item LC_ALL
977 @itemx LC_MESSAGES
978 @itemx LANG
979 @vindex LC_ALL @r{environment variable}
980 @vindex LC_MESSAGES @r{environment variable}
981 @vindex LANG @r{environment variable}
982 @cindex language of messages
983 @cindex message language
984 @cindex national language support
985 @cindex translation of message language
986 These variables specify the locale for the @code{LC_MESSAGES} category,
987 which determines the language that @command{grep} uses for messages.
988 The default @samp{C} locale uses American English messages.
989
990 @item POSIXLY_CORRECT
991 @vindex POSIXLY_CORRECT @r{environment variable}
992 If set, @command{grep} behaves as POSIX requires; otherwise,
993 @command{grep} behaves more like other GNU programs.
994 POSIX
995 requires that options that
996 follow file names must be treated as file names;
997 by default,
998 such options are permuted to the front of the operand list
999 and are treated as options.
1000 Also, @code{POSIXLY_CORRECT} disables special handling of an
1001 invalid bracket expression.  @xref{invalid-bracket-expr}.
1002
1003 @item _@var{N}_GNU_nonoption_argv_flags_
1004 @vindex _@var{N}_GNU_nonoption_argv_flags_ @r{environment variable}
1005 (Here @code{@var{N}} is @command{grep}'s numeric process ID.)
1006 If the @var{i}th character of this environment variable's value is @samp{1},
1007 do not consider the @var{i}th operand of @command{grep} to be an option,
1008 even if it appears to be one.
1009 A shell can put this variable in the environment for each command it runs,
1010 specifying which operands are the results of file name wildcard expansion
1011 and therefore should not be treated as options.
1012 This behavior is available only with the GNU C library,
1013 and only when @code{POSIXLY_CORRECT} is not set.
1014
1015 @end table
1016
1017
1018 @node Exit Status
1019 @section Exit Status
1020 @cindex exit status
1021 @cindex return status
1022
1023 Normally, the exit status is 0 if selected lines are found and 1 otherwise.
1024 But the exit status is 2 if an error occurred, unless the @option{-q} or
1025 @option{--quiet} or @option{--silent} option is used and a selected line
1026 is found.
1027 Note, however, that POSIX only mandates,
1028 for programs such as @command{grep}, @command{cmp}, and @command{diff},
1029 that the exit status in case of error be greater than 1;
1030 it is therefore advisable, for the sake of portability,
1031 to use logic that tests for this general condition
1032 instead of strict equality with@ 2.
1033
1034
1035 @node grep Programs
1036 @section @command{grep} Programs
1037 @cindex @command{grep} programs
1038 @cindex variants of @command{grep}
1039
1040 @command{grep} searches the named input files
1041 for lines containing a match to the given pattern.
1042 By default, @command{grep} prints the matching lines.
1043 A file named @file{-} stands for standard input.
1044 If no input is specified, @command{grep} searches the working
1045 directory @file{.} if given a command-line option specifying
1046 recursion; otherwise, @command{grep} searches standard input.
1047 There are four major variants of @command{grep},
1048 controlled by the following options.
1049
1050 @table @option
1051
1052 @item -G
1053 @itemx --basic-regexp
1054 @opindex -G
1055 @opindex --basic-regexp
1056 @cindex matching basic regular expressions
1057 Interpret the pattern as a basic regular expression (BRE).
1058 This is the default.
1059
1060 @item -E
1061 @itemx --extended-regexp
1062 @opindex -E
1063 @opindex --extended-regexp
1064 @cindex matching extended regular expressions
1065 Interpret the pattern as an extended regular expression (ERE).
1066 (@option{-E} is specified by POSIX.)
1067
1068 @item -F
1069 @itemx --fixed-strings
1070 @opindex -F
1071 @opindex --fixed-strings
1072 @cindex matching fixed strings
1073 Interpret the pattern as a list of fixed strings, separated
1074 by newlines, any of which is to be matched.
1075 (@option{-F} is specified by POSIX.)
1076
1077 @item -P
1078 @itemx --perl-regexp
1079 @opindex -P
1080 @opindex --perl-regexp
1081 @cindex matching Perl regular expressions
1082 Interpret the pattern as a Perl regular expression.
1083 This is highly experimental and
1084 @samp{grep@ -P} may warn of unimplemented features.
1085
1086 @end table
1087
1088 In addition,
1089 two variant programs @command{egrep} and @command{fgrep} are available.
1090 @command{egrep} is the same as @samp{grep@ -E}.
1091 @command{fgrep} is the same as @samp{grep@ -F}.
1092 Direct invocation as either
1093 @command{egrep} or @command{fgrep} is deprecated,
1094 but is provided to allow historical applications
1095 that rely on them to run unmodified.
1096
1097
1098 @node Regular Expressions
1099 @chapter Regular Expressions
1100 @cindex regular expressions
1101
1102 A @dfn{regular expression} is a pattern that describes a set of strings.
1103 Regular expressions are constructed analogously to arithmetic expressions,
1104 by using various operators to combine smaller expressions.
1105 @command{grep} understands
1106 three different versions of regular expression syntax:
1107 ``basic,'' (BRE) ``extended'' (ERE) and ``perl''.
1108 In GNU @command{grep},
1109 there is no difference in available functionality between the basic and
1110 extended syntaxes.
1111 In other implementations, basic regular expressions are less powerful.
1112 The following description applies to extended regular expressions;
1113 differences for basic regular expressions are summarized afterwards.
1114 Perl regular expressions give additional functionality, and are
1115 documented in the @i{pcresyntax}(3) and @i{pcrepattern}(3) manual pages,
1116 but may not be available on every system.
1117
1118 @menu
1119 * Fundamental Structure::
1120 * Character Classes and Bracket Expressions::
1121 * The Backslash Character and Special Expressions::
1122 * Anchoring::
1123 * Back-references and Subexpressions::
1124 * Basic vs Extended::
1125 @end menu
1126
1127 @node Fundamental Structure
1128 @section Fundamental Structure
1129
1130 The fundamental building blocks are the regular expressions that match
1131 a single character.
1132 Most characters, including all letters and digits,
1133 are regular expressions that match themselves.
1134 Any meta-character
1135 with special meaning may be quoted by preceding it with a backslash.
1136
1137 A regular expression may be followed by one of several
1138 repetition operators:
1139
1140 @table @samp
1141
1142 @item .
1143 @opindex .
1144 @cindex dot
1145 @cindex period
1146 The period @samp{.} matches any single character.
1147
1148 @item ?
1149 @opindex ?
1150 @cindex question mark
1151 @cindex match expression at most once
1152 The preceding item is optional and will be matched at most once.
1153
1154 @item *
1155 @opindex *
1156 @cindex asterisk
1157 @cindex match expression zero or more times
1158 The preceding item will be matched zero or more times.
1159
1160 @item +
1161 @opindex +
1162 @cindex plus sign
1163 @cindex match expression one or more times
1164 The preceding item will be matched one or more times.
1165
1166 @item @{@var{n}@}
1167 @opindex @{@var{n}@}
1168 @cindex braces, one argument
1169 @cindex match expression @var{n} times
1170 The preceding item is matched exactly @var{n} times.
1171
1172 @item @{@var{n},@}
1173 @opindex @{@var{n},@}
1174 @cindex braces, second argument omitted
1175 @cindex match expression @var{n} or more times
1176 The preceding item is matched @var{n} or more times.
1177
1178 @item @{,@var{m}@}
1179 @opindex @{,@var{m}@}
1180 @cindex braces, first argument omitted
1181 @cindex match expression at most @var{m} times
1182 The preceding item is matched at most @var{m} times.
1183
1184 @item @{@var{n},@var{m}@}
1185 @opindex @{@var{n},@var{m}@}
1186 @cindex braces, two arguments
1187 @cindex match expression from @var{n} to @var{m} times
1188 The preceding item is matched at least @var{n} times, but not more than
1189 @var{m} times.
1190
1191 @end table
1192
1193 The empty regular expression matches the empty string.
1194 Two regular expressions may be concatenated;
1195 the resulting regular expression
1196 matches any string formed by concatenating two substrings
1197 that respectively match the concatenated expressions.
1198
1199 Two regular expressions may be joined by the infix operator @samp{|};
1200 the resulting regular expression
1201 matches any string matching either alternate expression.
1202
1203 Repetition takes precedence over concatenation,
1204 which in turn takes precedence over alternation.
1205 A whole expression may be enclosed in parentheses
1206 to override these precedence rules and form a subexpression.
1207
1208 @node Character Classes and Bracket Expressions
1209 @section Character Classes and Bracket Expressions
1210
1211 @cindex bracket expression
1212 @cindex character class
1213 A @dfn{bracket expression} is a list of characters enclosed by @samp{[} and
1214 @samp{]}.
1215 It matches any single character in that list;
1216 if the first character of the list is the caret @samp{^},
1217 then it matches any character @strong{not} in the list.
1218 For example, the regular expression
1219 @samp{[0123456789]} matches any single digit.
1220
1221 @cindex range expression
1222 Within a bracket expression, a @dfn{range expression} consists of two
1223 characters separated by a hyphen.
1224 It matches any single character that
1225 sorts between the two characters, inclusive, using the locale's
1226 collating sequence and character set.
1227 For example, in the default C
1228 locale, @samp{[a-d]} is equivalent to @samp{[abcd]}.
1229 Many locales sort
1230 characters in dictionary order, and in these locales @samp{[a-d]} is
1231 typically not equivalent to @samp{[abcd]};
1232 it might be equivalent to @samp{[aBbCcDd]}, for example.
1233 To obtain the traditional interpretation
1234 of bracket expressions, you can use the @samp{C} locale by setting the
1235 @env{LC_ALL} environment variable to the value @samp{C}.
1236
1237 Finally, certain named classes of characters are predefined within
1238 bracket expressions, as follows.
1239 Their interpretation depends on the @code{LC_CTYPE} locale;
1240 for example, @samp{[[:alnum:]]} means the character class of numbers and letters
1241 in the current locale.
1242
1243 @cindex classes of characters
1244 @cindex character classes
1245 @table @samp
1246
1247 @item [:alnum:]
1248 @opindex alnum @r{character class}
1249 @cindex alphanumeric characters
1250 Alphanumeric characters:
1251 @samp{[:alpha:]} and @samp{[:digit:]}; in the @samp{C} locale and ASCII character encoding, this is the same as @samp{[0-9A-Za-z]}.
1252
1253 @item [:alpha:]
1254 @opindex alpha @r{character class}
1255 @cindex alphabetic characters
1256 Alphabetic characters:
1257 @samp{[:lower:]} and @samp{[:upper:]}; in the @samp{C} locale and ASCII character encoding, this is the same as @samp{[A-Za-z]}.
1258
1259 @item [:blank:]
1260 @opindex blank @r{character class}
1261 @cindex blank characters
1262 Blank characters:
1263 space and tab.
1264
1265 @item [:cntrl:]
1266 @opindex cntrl @r{character class}
1267 @cindex control characters
1268 Control characters.
1269 In ASCII, these characters have octal codes 000
1270 through 037, and 177 (@code{DEL}).
1271 In other character sets, these are
1272 the equivalent characters, if any.
1273
1274 @item [:digit:]
1275 @opindex digit @r{character class}
1276 @cindex digit characters
1277 @cindex numeric characters
1278 Digits: @code{0 1 2 3 4 5 6 7 8 9}.
1279
1280 @item [:graph:]
1281 @opindex graph @r{character class}
1282 @cindex graphic characters
1283 Graphical characters:
1284 @samp{[:alnum:]} and @samp{[:punct:]}.
1285
1286 @item [:lower:]
1287 @opindex lower @r{character class}
1288 @cindex lower-case letters
1289 Lower-case letters; in the @samp{C} locale and ASCII character
1290 encoding, this is
1291 @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}.
1292
1293 @item [:print:]
1294 @opindex print @r{character class}
1295 @cindex printable characters
1296 Printable characters:
1297 @samp{[:alnum:]}, @samp{[:punct:]}, and space.
1298
1299 @item [:punct:]
1300 @opindex punct @r{character class}
1301 @cindex punctuation characters
1302 Punctuation characters; in the @samp{C} locale and ASCII character
1303 encoding, this is
1304 @code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}.
1305
1306 @item [:space:]
1307 @opindex space @r{character class}
1308 @cindex space characters
1309 @cindex whitespace characters
1310 Space characters: in the @samp{C} locale, this is
1311 tab, newline, vertical tab, form feed, carriage return, and space.
1312 @xref{Usage}, for more discussion of matching newlines.
1313
1314 @item [:upper:]
1315 @opindex upper @r{character class}
1316 @cindex upper-case letters
1317 Upper-case letters: in the @samp{C} locale and ASCII character
1318 encoding, this is
1319 @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}.
1320
1321 @item [:xdigit:]
1322 @opindex xdigit @r{character class}
1323 @cindex xdigit class
1324 @cindex hexadecimal digits
1325 Hexadecimal digits:
1326 @code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}.
1327
1328 @end table
1329 Note that the brackets in these class names are
1330 part of the symbolic names, and must be included in addition to
1331 the brackets delimiting the bracket expression.
1332
1333 @anchor{invalid-bracket-expr}
1334 If you mistakenly omit the outer brackets, and search for say, @samp{[:upper:]},
1335 GNU @command{grep} prints a diagnostic and exits with status 2, on
1336 the assumption that you did not intend to search for the nominally
1337 equivalent regular expression: @samp{[:epru]}.
1338 Set the @code{POSIXLY_CORRECT} environment variable to disable this feature.
1339
1340 Most meta-characters lose their special meaning inside bracket expressions.
1341
1342 @table @samp
1343 @item ]
1344 ends the bracket expression if it's not the first list item.
1345 So, if you want to make the @samp{]} character a list item,
1346 you must put it first.
1347
1348 @item [.
1349 represents the open collating symbol.
1350
1351 @item .]
1352 represents the close collating symbol.
1353
1354 @item [=
1355 represents the open equivalence class.
1356
1357 @item =]
1358 represents the close equivalence class.
1359
1360 @item [:
1361 represents the open character class symbol, and should be followed by a valid character class name.
1362
1363 @item :]
1364 represents the close character class symbol.
1365
1366 @item -
1367 represents the range if it's not first or last in a list or the ending point
1368 of a range.
1369
1370 @item ^
1371 represents the characters not in the list.
1372 If you want to make the @samp{^}
1373 character a list item, place it anywhere but first.
1374
1375 @end table
1376
1377 @node The Backslash Character and Special Expressions
1378 @section The Backslash Character and Special Expressions
1379 @cindex backslash
1380
1381 The @samp{\} character,
1382 when followed by certain ordinary characters,
1383 takes a special meaning:
1384
1385 @table @samp
1386
1387 @item \b
1388 Match the empty string at the edge of a word.
1389
1390 @item \B
1391 Match the empty string provided it's not at the edge of a word.
1392
1393 @item \<
1394 Match the empty string at the beginning of word.
1395
1396 @item \>
1397 Match the empty string at the end of word.
1398
1399 @item \w
1400 Match word constituent, it is a synonym for @samp{[_[:alnum:]]}.
1401
1402 @item \W
1403 Match non-word constituent, it is a synonym for @samp{[^_[:alnum:]]}.
1404
1405 @item \s
1406 Match whitespace, it is a synonym for @samp{[[:space:]]}.
1407
1408 @item \S
1409 Match non-whitespace, it is a synonym for @samp{[^[:space:]]}.
1410
1411 @end table
1412
1413 For example, @samp{\brat\b} matches the separate word @samp{rat},
1414 @samp{\Brat\B} matches @samp{crate} but not @samp{furry rat}.
1415
1416 @node Anchoring
1417 @section Anchoring
1418 @cindex anchoring
1419
1420 The caret @samp{^} and the dollar sign @samp{$} are meta-characters that
1421 respectively match the empty string at the beginning and end of a line.
1422 They are termed @dfn{anchors}, since they force the match to be ``anchored''
1423 to beginning or end of a line, respectively.
1424
1425 @node Back-references and Subexpressions
1426 @section Back-references and Subexpressions
1427 @cindex subexpression
1428 @cindex back-reference
1429
1430 The back-reference @samp{\@var{n}}, where @var{n} is a single digit, matches
1431 the substring previously matched by the @var{n}th parenthesized subexpression
1432 of the regular expression.
1433 For example, @samp{(a)\1} matches @samp{aa}.
1434 When used with alternation, if the group does not participate in the match then
1435 the back-reference makes the whole match fail.
1436 For example, @samp{a(.)|b\1}
1437 will not match @samp{ba}.
1438 When multiple regular expressions are given with
1439 @option{-e} or from a file (@samp{-f @var{file}}),
1440 back-references are local to each expression.
1441
1442 @node Basic vs Extended
1443 @section Basic vs Extended Regular Expressions
1444 @cindex basic regular expressions
1445
1446 In basic regular expressions the meta-characters @samp{?}, @samp{+},
1447 @samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning;
1448 instead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{},
1449 @samp{\|}, @samp{\(}, and @samp{\)}.
1450
1451 @cindex interval specifications
1452 Traditional @command{egrep} did not support the @samp{@{} meta-character,
1453 and some @command{egrep} implementations support @samp{\@{} instead, so
1454 portable scripts should avoid @samp{@{} in @samp{grep@ -E} patterns and
1455 should use @samp{[@{]} to match a literal @samp{@{}.
1456
1457 GNU @command{grep@ -E} attempts to support traditional usage by
1458 assuming that @samp{@{} is not special if it would be the start of an
1459 invalid interval specification.
1460 For example, the command
1461 @samp{grep@ -E@ '@{1'} searches for the two-character string @samp{@{1}
1462 instead of reporting a syntax error in the regular expression.
1463 POSIX allows this behavior as an extension, but portable scripts
1464 should avoid it.
1465
1466
1467 @node Usage
1468 @chapter Usage
1469
1470 @cindex usage, examples
1471 Here is an example command that invokes GNU @command{grep}:
1472
1473 @example
1474 grep -i 'hello.*world' menu.h main.c
1475 @end example
1476
1477 @noindent
1478 This lists all lines in the files @file{menu.h} and @file{main.c} that
1479 contain the string @samp{hello} followed by the string @samp{world};
1480 this is because @samp{.*} matches zero or more characters within a line.
1481 @xref{Regular Expressions}.
1482 The @option{-i} option causes @command{grep}
1483 to ignore case, causing it to match the line @samp{Hello, world!}, which
1484 it would not otherwise match.
1485 @xref{Invoking}, for more details about
1486 how to invoke @command{grep}.
1487
1488 @cindex using @command{grep}, Q&A
1489 @cindex FAQ about @command{grep} usage
1490 Here are some common questions and answers about @command{grep} usage.
1491
1492 @enumerate
1493
1494 @item
1495 How can I list just the names of matching files?
1496
1497 @example
1498 grep -l 'main' *.c
1499 @end example
1500
1501 @noindent
1502 lists the names of all C files in the current directory whose contents
1503 mention @samp{main}.
1504
1505 @item
1506 How do I search directories recursively?
1507
1508 @example
1509 grep -r 'hello' /home/gigi
1510 @end example
1511
1512 @noindent
1513 searches for @samp{hello} in all files
1514 under the @file{/home/gigi} directory.
1515 For more control over which files are searched,
1516 use @command{find}, @command{grep}, and @command{xargs}.
1517 For example, the following command searches only C files:
1518
1519 @example
1520 find /home/gigi -name '*.c' -print0 | xargs -0r grep -H 'hello'
1521 @end example
1522
1523 This differs from the command:
1524
1525 @example
1526 grep -H 'hello' *.c
1527 @end example
1528
1529 which merely looks for @samp{hello} in all files in the current
1530 directory whose names end in @samp{.c}.
1531 The @samp{find ...} command line above is more similar to the command:
1532
1533 @example
1534 grep -rH --include='*.c' 'hello' /home/gigi
1535 @end example
1536
1537 @item
1538 What if a pattern has a leading @samp{-}?
1539
1540 @example
1541 grep -e '--cut here--' *
1542 @end example
1543
1544 @noindent
1545 searches for all lines matching @samp{--cut here--}.
1546 Without @option{-e},
1547 @command{grep} would attempt to parse @samp{--cut here--} as a list of
1548 options.
1549
1550 @item
1551 Suppose I want to search for a whole word, not a part of a word?
1552
1553 @example
1554 grep -w 'hello' *
1555 @end example
1556
1557 @noindent
1558 searches only for instances of @samp{hello} that are entire words;
1559 it does not match @samp{Othello}.
1560 For more control, use @samp{\<} and
1561 @samp{\>} to match the start and end of words.
1562 For example:
1563
1564 @example
1565 grep 'hello\>' *
1566 @end example
1567
1568 @noindent
1569 searches only for words ending in @samp{hello}, so it matches the word
1570 @samp{Othello}.
1571
1572 @item
1573 How do I output context around the matching lines?
1574
1575 @example
1576 grep -C 2 'hello' *
1577 @end example
1578
1579 @noindent
1580 prints two lines of context around each matching line.
1581
1582 @item
1583 How do I force @command{grep} to print the name of the file?
1584
1585 Append @file{/dev/null}:
1586
1587 @example
1588 grep 'eli' /etc/passwd /dev/null
1589 @end example
1590
1591 gets you:
1592
1593 @example
1594 /etc/passwd:eli:x:2098:1000:Eli Smith:/home/eli:/bin/bash
1595 @end example
1596
1597 Alternatively, use @option{-H}, which is a GNU extension:
1598
1599 @example
1600 grep -H 'eli' /etc/passwd
1601 @end example
1602
1603 @item
1604 Why do people use strange regular expressions on @command{ps} output?
1605
1606 @example
1607 ps -ef | grep '[c]ron'
1608 @end example
1609
1610 If the pattern had been written without the square brackets, it would
1611 have matched not only the @command{ps} output line for @command{cron},
1612 but also the @command{ps} output line for @command{grep}.
1613 Note that on some platforms,
1614 @command{ps} limits the output to the width of the screen;
1615 @command{grep} does not have any limit on the length of a line
1616 except the available memory.
1617
1618 @item
1619 Why does @command{grep} report ``Binary file matches''?
1620
1621 If @command{grep} listed all matching ``lines'' from a binary file, it
1622 would probably generate output that is not useful, and it might even
1623 muck up your display.
1624 So GNU @command{grep} suppresses output from
1625 files that appear to be binary files.
1626 To force GNU @command{grep}
1627 to output lines even from files that appear to be binary, use the
1628 @option{-a} or @samp{--binary-files=text} option.
1629 To eliminate the
1630 ``Binary file matches'' messages, use the @option{-I} or
1631 @samp{--binary-files=without-match} option.
1632
1633 @item
1634 Why doesn't @samp{grep -lv} print non-matching file names?
1635
1636 @samp{grep -lv} lists the names of all files containing one or more
1637 lines that do not match.
1638 To list the names of all files that contain no
1639 matching lines, use the @option{-L} or @option{--files-without-match}
1640 option.
1641
1642 @item
1643 I can do ``OR'' with @samp{|}, but what about ``AND''?
1644
1645 @example
1646 grep 'paul' /etc/motd | grep 'franc,ois'
1647 @end example
1648
1649 @noindent
1650 finds all lines that contain both @samp{paul} and @samp{franc,ois}.
1651
1652 @item
1653 Why does the empty pattern match every input line?
1654
1655 The @command{grep} command searches for lines that contain strings
1656 that match a pattern.  Every line contains the empty string, so an
1657 empty pattern causes @command{grep} to find a match on each line.  It
1658 is not the only such pattern: @samp{^}, @samp{$}, @samp{.*}, and many
1659 other patterns cause @command{grep} to match every line.
1660
1661 To match empty lines, use the pattern @samp{^$}.  To match blank
1662 lines, use the pattern @samp{^[[:blank:]]*$}.  To match no lines at
1663 all, use the command @samp{grep -f /dev/null}.
1664
1665 @item
1666 How can I search in both standard input and in files?
1667
1668 Use the special file name @samp{-}:
1669
1670 @example
1671 cat /etc/passwd | grep 'alain' - /etc/motd
1672 @end example
1673
1674 @item
1675 @cindex palindromes
1676 How to express palindromes in a regular expression?
1677
1678 It can be done by using back-references;
1679 for example,
1680 a palindrome of 4 characters can be written with a BRE:
1681
1682 @example
1683 grep -w -e '\(.\)\(.\).\2\1' file
1684 @end example
1685
1686 It matches the word ``radar'' or ``civic.''
1687
1688 Guglielmo Bondioni proposed a single RE
1689 that finds all palindromes up to 19 characters long
1690 using @w{9 subexpressions} and @w{9 back-references}:
1691
1692 @smallexample
1693 grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file
1694 @end smallexample
1695
1696 Note this is done by using GNU ERE extensions;
1697 it might not be portable to other implementations of @command{grep}.
1698
1699 @item
1700 Why is this back-reference failing?
1701
1702 @example
1703 echo 'ba' | grep -E '(a)\1|b\1'
1704 @end example
1705
1706 This gives no output, because the first alternate @samp{(a)\1} does not match,
1707 as there is no @samp{aa} in the input, so the @samp{\1} in the second alternate
1708 has nothing to refer back to, meaning it will never match anything.
1709 (The second alternate in this example can only match
1710 if the first alternate has matched---making the second one superfluous.)
1711
1712 @item
1713 How can I match across lines?
1714
1715 Standard grep cannot do this, as it is fundamentally line-based.
1716 Therefore, merely using the @code{[:space:]} character class does not
1717 match newlines in the way you might expect.  However, if your grep is
1718 compiled with Perl patterns enabled, the Perl @samp{s}
1719 modifier (which makes @code{.} match newlines) can be used:
1720
1721 @example
1722 printf 'foo\nbar\n' | grep -P '(?s)foo.*?bar'
1723 @end example
1724
1725 With the GNU @command{grep} option @code{-z} (@pxref{File and
1726 Directory Selection}), the input is terminated by null bytes.  Thus,
1727 you can match newlines in the input, but the output will be the whole
1728 file, so this is really only useful to determine if the pattern is
1729 present:
1730
1731 @example
1732 printf 'foo\nbar\n' | grep -z -q 'foo[[:space:]]\+bar'
1733 @end example
1734
1735 Failing either of those options, you need to transform the input
1736 before giving it to @command{grep}, or turn to @command{awk},
1737 @command{sed}, @command{perl}, or many other utilities that are
1738 designed to operate across lines.
1739
1740 @item
1741 What do @command{grep}, @command{fgrep}, and @command{egrep} stand for?
1742
1743 The name @command{grep} comes from the way line editing was done on Unix.
1744 For example,
1745 @command{ed} uses the following syntax
1746 to print a list of matching lines on the screen:
1747
1748 @example
1749 global/regular expression/print
1750 g/re/p
1751 @end example
1752
1753 @command{fgrep} stands for Fixed @command{grep};
1754 @command{egrep} stands for Extended @command{grep}.
1755
1756 @end enumerate
1757
1758
1759 @node Reporting Bugs
1760 @chapter Reporting bugs
1761
1762 @cindex bugs, reporting
1763 Email bug reports to @email{bug-grep@@gnu.org},
1764 a mailing list whose web page is
1765 @url{http://lists.gnu.org/mailman/listinfo/bug-grep}.
1766 The Savannah bug tracker for @command{grep} is located at
1767 @url{http://savannah.gnu.org/bugs/?group=grep}.
1768
1769 @section Known Bugs
1770 @cindex Bugs, known
1771
1772 Large repetition counts in the @samp{@{n,m@}} construct may cause
1773 @command{grep} to use lots of memory.
1774 In addition, certain other
1775 obscure regular expressions require exponential time and
1776 space, and may cause @command{grep} to run out of memory.
1777
1778 Back-references are very slow, and may require exponential time.
1779
1780
1781 @node Copying
1782 @chapter Copying
1783 @cindex copying
1784
1785 GNU @command{grep} is licensed under the GNU GPL, which makes it @dfn{free
1786 software}.
1787
1788 The ``free'' in ``free software'' refers to liberty, not price. As
1789 some GNU project advocates like to point out, think of ``free speech''
1790 rather than ``free beer''.  In short, you have the right (freedom) to
1791 run and change @command{grep} and distribute it to other people, and---if you
1792 want---charge money for doing either.  The important restriction is
1793 that you have to grant your recipients the same rights and impose the
1794 same restrictions.
1795
1796 This general method of licensing software is sometimes called
1797 @dfn{open source}.  The GNU project prefers the term ``free software''
1798 for reasons outlined at
1799 @url{http://www.gnu.org/philosophy/open-source-misses-the-point.html}.
1800
1801 This manual is free documentation in the same sense.  The
1802 documentation license is included below.  The license for the program
1803 is available with the source code, or at
1804 @url{http://www.gnu.org/licenses/gpl.html}.
1805
1806 @menu
1807 * GNU Free Documentation License::
1808 @end menu
1809
1810 @node GNU Free Documentation License
1811 @section GNU Free Documentation License
1812
1813 @include fdl.texi
1814
1815
1816 @node Index
1817 @unnumbered Index
1818
1819 @printindex cp
1820
1821 @bye