Grep: Add DragonFly README files
[dragonfly.git] / gnu / usr.bin / grep / doc / grep.texi
1 @c $DragonFly: src/gnu/usr.bin/grep/doc/grep.texi,v 1.2 2005/10/08 11:28:23 corecode Exp $
2
3 \input texinfo  @c -*-texinfo-*-
4 @c %**start of header
5 @setfilename grep.info
6 @settitle grep, print lines matching a pattern
7 @c %**end of header
8
9 @c This file has the new style title page commands.
10 @c Run `makeinfo' rather than `texinfo-format-buffer'.
11
12 @c smallbook
13
14 @c tex
15 @c \overfullrule=0pt
16 @c end tex
17
18 @include version.texi
19
20 @c Combine indices.
21 @syncodeindex ky cp
22 @syncodeindex pg cp
23 @syncodeindex tp cp
24
25 @defcodeindex op
26 @syncodeindex op fn
27 @syncodeindex vr fn
28
29 @ifinfo
30 @direntry
31 * grep: (grep).                   print lines matching a pattern.
32 @end direntry
33 This file documents @command{grep}, a pattern matching engine.
34
35
36 Published by the Free Software Foundation,
37 59 Temple Place - Suite 330
38 Boston, MA 02111-1307, USA
39
40 Copyright 1999 Free Software Foundation, Inc.
41
42 Permission is granted to make and distribute verbatim copies of
43 this manual provided the copyright notice and this permission notice
44 are preserved on all copies.
45
46 @ignore
47 Permission is granted to process this file through TeX and print the
48 results, provided the printed document carries copying permission
49 notice identical to this one except for the removal of this paragraph
50 (this paragraph not being relevant to the printed manual).
51
52 @end ignore
53 Permission is granted to copy and distribute modified versions of this
54 manual under the conditions for verbatim copying, provided that the entire
55 resulting derived work is distributed under the terms of a permission
56 notice identical to this one.
57
58 Permission is granted to copy and distribute translations of this manual
59 into another language, under the above conditions for modified versions,
60 except that this permission notice may be stated in a translation approved
61 by the Foundation.
62 @end ifinfo
63
64 @setchapternewpage off
65
66 @titlepage
67 @title grep, searching for a pattern
68 @subtitle version @value{VERSION}, @value{UPDATED}
69 @author Alain Magloire et al.
70
71 @page
72 @vskip 0pt plus 1filll
73 Copyright @copyright{} 1999 Free Software Foundation, Inc.
74
75 @sp 2
76 Published by the Free Software Foundation, @*
77 59 Temple Place - Suite 330, @*
78 Boston, MA 02111-1307, USA
79
80 Permission is granted to make and distribute verbatim copies of
81 this manual provided the copyright notice and this permission notice
82 are preserved on all copies.
83
84 Permission is granted to copy and distribute modified versions of this
85 manual under the conditions for verbatim copying, provided that the entire
86 resulting derived work is distributed under the terms of a permission
87 notice identical to this one.
88
89 Permission is granted to copy and distribute translations of this manual
90 into another language, under the above conditions for modified versions,
91 except that this permission notice may be stated in a translation approved
92 by the Foundation.
93
94 @end titlepage
95 @page
96
97
98 @ifnottex
99 @node Top
100 @top Grep
101
102 @command{grep} searches for lines matching a pattern.
103
104 This document was produced for version @value{VERSION} of @sc{gnu}
105 @command{grep}.
106 @end ifnottex
107
108 @menu
109 * Introduction::                Introduction.
110 * Invoking::                    Invoking @command{grep}; description of options.
111 * Diagnostics::                 Exit status returned by @command{grep}.
112 * Grep Programs::               @command{grep} programs.
113 * Regular Expressions::         Regular Expressions.
114 * Usage::                       Examples.
115 * Reporting Bugs::              Reporting Bugs.
116 * Concept Index::               A menu with all the topics in this manual.
117 * Index::                       A menu with all @command{grep} commands
118                                  and command-line options.
119 @end menu
120
121
122 @node Introduction
123 @chapter Introduction
124
125 @cindex Searching for a pattern.
126
127 @command{grep} searches the input files
128 for lines containing a match to a given
129 pattern list.  When it finds a match in a line, it copies the line to standard
130 output (by default), or does whatever other sort of output you have requested
131 with options.
132
133 Though @command{grep} expects to do the matching on text,
134 it has no limits on input line length other than available memory,
135 and it can match arbitrary characters within a line.
136 If the final byte of an input file is not a newline,
137 @command{grep} silently supplies one.
138 Since newline is also a separator for the list of patterns, there
139 is no way to match newline characters in a text.
140
141 @node Invoking
142 @chapter Invoking @command{grep}
143
144 @command{grep} comes with a rich set of options from @sc{posix.2} and @sc{gnu}
145 extensions.
146
147 @table @samp
148
149 @item -c
150 @itemx --count
151 @opindex -c
152 @opindex -count
153 @cindex counting lines
154 Suppress normal output; instead print a count of matching
155 lines for each input file.  With the @samp{-v}, @samp{--invert-match} option,
156 count non-matching lines.
157
158 @item -e @var{pattern}
159 @itemx --regexp=@var{pattern}
160 @opindex -e
161 @opindex --regexp=@var{pattern}
162 @cindex pattern list
163 Use @var{pattern} as the pattern; useful to protect patterns
164 beginning with a @samp{-}.
165
166 @item -f @var{file}
167 @itemx --file=@var{file}
168 @opindex -f
169 @opindex --file
170 @cindex pattern from file
171 Obtain patterns from @var{file}, one per line.  The empty
172 file contains zero patterns, and therefore matches nothing.
173
174 @item -i
175 @itemx --ignore-case
176 @opindex -i
177 @opindex --ignore-case
178 @cindex case insensitive search
179 Ignore case distinctions in both the pattern and the input files.
180
181 @item -l
182 @itemx --files-with-matches
183 @opindex -l
184 @opindex --files-with-matches
185 @cindex names of matching files
186 Suppress normal output; instead print the name of each input
187 file from which output would normally have been printed.
188 The scanning of every file will stop on the first match.
189
190 @item -n
191 @itemx --line-number
192 @opindex -n
193 @opindex --line-number
194 @cindex line numbering
195 Prefix each line of output with the line number within its input file.
196
197 @item -q
198 @itemx --quiet
199 @itemx --silent
200 @opindex -q
201 @opindex --quiet
202 @opindex --silent
203 @cindex quiet, silent
204 Quiet; suppress normal output.  The scanning of every file will stop on
205 the first match.  Also see the @samp{-s} or @samp{--no-messages} option.
206
207 @item -s
208 @itemx --no-messages
209 @opindex -s
210 @opindex --no-messages
211 @cindex suppress error messages
212 Suppress error messages about nonexistent or unreadable files.
213 Portability note: unlike @sc{gnu} @command{grep}, traditional
214 @command{grep} did not conform to @sc{posix.2}, because traditional
215 @command{grep} lacked a @samp{-q} option and its @samp{-s} option behaved
216 like @sc{gnu} @command{grep}'s @samp{-q} option.  Shell scripts intended
217 to be portable to traditional @command{grep} should avoid both
218 @samp{-q} and @samp{-s} and should redirect
219 output to @file{/dev/null} instead.
220
221 @item -v
222 @itemx --invert-match
223 @opindex -v
224 @opindex --invert-match
225 @cindex invert matching
226 @cindex print non-matching lines
227 Invert the sense of matching, to select non-matching lines.
228
229 @item -x
230 @itemx --line-regexp
231 @opindex -x
232 @opindex --line-regexp
233 @cindex match the whole line
234 Select only those matches that exactly match the whole line.
235
236 @end table
237
238 @section (@sc{gnu}) Extensions
239
240 @table @samp
241
242 @item -A @var{num}
243 @itemx --after-context=@var{num}
244 @opindex -A
245 @opindex --after-context
246 @cindex after context
247 @cindex context lines, after match
248 Print @var{num} lines of trailing context after matching lines.
249
250 @item -B @var{num}
251 @itemx --before-context=@var{num}
252 @opindex -B
253 @opindex --before-context
254 @cindex before context
255 @cindex context lines, before match
256 Print @var{num} lines of leading context before matching lines.
257
258 @item -C @var{num}
259 @itemx --context=[@var{num}]
260 @opindex -C
261 @opindex --context
262 @cindex context
263 Print @var{num} lines (default 2) of output context.
264
265
266 @item -@var{num}
267 @opindex -NUM
268 Same as @samp{--context=@var{num}} lines of leading and trailing
269 context.  However, grep will never print any given line more than once.
270
271
272 @item -V
273 @itemx --version
274 @opindex -V
275 @opindex --version
276 @cindex Version, printing
277 Print the version number of @command{grep} to the standard output stream.
278 This version number should be included in all bug reports.
279
280 @item --help
281 @opindex --help
282 @cindex Usage summary, printing
283 Print a usage message briefly summarizing these command-line options
284 and the bug-reporting address, then exit.
285
286 @itemx --binary-files=@var{type}
287 @opindex --binary-files
288 @cindex binary files
289 If the first few bytes of a file indicate that the file contains binary
290 data, assume that the file is of type @var{type}.  By default,
291 @var{type} is @samp{binary}, and @command{grep} normally outputs either
292 a one-line message saying that a binary file matches, or no message if
293 there is no match.  If @var{type} is @samp{without-match},
294 @command{grep} assumes that a binary file does not match;
295 this is equivalent to the @samp{-I} option.  If @var{type}
296 is @samp{text}, @command{grep} processes a binary file as if it were
297 text; this is equivalent to the @samp{-a} option.
298 @emph{Warning:} @samp{--binary-files=text} might output binary garbage,
299 which can have nasty side effects if the output is a terminal and if the
300 terminal driver interprets some of it as commands.
301
302 @item -b
303 @itemx --byte-offset
304 @opindex -b
305 @opindex --byte-offset
306 @cindex byte offset
307 Print the byte offset within the input file before each line of output.
308 When @command{grep} runs on @sc{ms-dos} or MS-Windows, the printed
309 byte offsets
310 depend on whether the @samp{-u} (@samp{--unix-byte-offsets}) option is
311 used; see below.
312
313 @item -d @var{action}
314 @itemx --directories=@var{action}
315 @opindex -d
316 @opindex --directories
317 @cindex directory search
318 If an input file is a directory, use @var{action} to process it.
319 By default, @var{action} is @samp{read}, which means that directories are
320 read just as if they were ordinary files (some operating systems
321 and filesystems disallow this, and will cause @command{grep} to print error
322 messages for every directory).  If @var{action} is @samp{skip},
323 directories are silently skipped.  If @var{action} is @samp{recurse},
324 @command{grep} reads all files under each directory, recursively; this is
325 equivalent to the @samp{-r} option.
326
327 @item -H
328 @itemx --with-filename
329 @opindex -H
330 @opindex --With-filename
331 @cindex with filename prefix
332 Print the filename for each match.
333
334 @item -h
335 @itemx --no-filename
336 @opindex -h
337 @opindex --no-filename
338 @cindex no filename prefix
339 Suppress the prefixing of filenames on output when multiple files are searched.
340
341 @item -L
342 @itemx --files-without-match
343 @opindex -L
344 @opindex --files-without-match
345 @cindex files which don't match
346 Suppress normal output; instead print the name of each input
347 file from which no output would normally have been printed.
348 The scanning of every file will stop on the first match.
349
350 @item -a
351 @itemx --text
352 @opindex -a
353 @opindex --text
354 @cindex suppress binary data
355 @cindex binary files
356 Process a binary file as if it were text; this is equivalent to the
357 @samp{--binary-files=text} option.
358
359 @item -I
360 Process a binary file as if it did not contain matching data; this is
361 equivalent to the @samp{--binary-files=without-match} option.
362
363 @item -w
364 @itemx --word-regexp
365 @opindex -w
366 @opindex --word-regexp
367 @cindex matching whole words
368 Select only those lines containing matches that form
369 whole words.  The test is that the matching substring
370 must either be at the beginning of the line, or preceded
371 by a non-word constituent character.  Similarly,
372 it must be either at the end of the line or followed by
373 a non-word constituent character.  Word-constituent
374 characters are letters, digits, and the underscore.
375
376 @item -r
377 @itemx --recursive
378 @opindex -r
379 @opindex --recursive
380 @cindex recursive search
381 @cindex searching directory trees
382 For each directory mentioned in the command line, read and process all
383 files in that directory, recursively.  This is the same as the @samp{-d
384 recurse} option.
385
386 @item -O
387 @itemx --only-files
388 @opindex -O
389 @opindex --only-files
390 @cindex ignoring special files
391 @cindex ignoring symlinked directories
392 Ignore all special files, except for symlinks.
393 When recursing into directories, ignore symlinked directories as well.
394
395 @item -y
396 @opindex -y
397 @cindex case insensitive search, obsolete option
398 Obsolete synonym for @samp{-i}.
399
400 @item -U
401 @itemx --binary
402 @opindex -U
403 @opindex --binary
404 @cindex DOS/Windows binary files
405 @cindex binary files, DOS/Windows
406 Treat the file(s) as binary.  By default, under @sc{ms-dos}
407 and MS-Windows, @command{grep} guesses the file type by looking
408 at the contents of the first 32kB read from the file.
409 If @command{grep} decides the file is a text file, it strips the
410 @code{CR} characters from the original file contents (to make
411 regular expressions with @code{^} and @code{$} work correctly).
412 Specifying @samp{-U} overrules this guesswork, causing all
413 files to be read and passed to the matching mechanism
414 verbatim; if the file is a text file with @code{CR/LF} pairs
415 at the end of each line, this will cause some regular
416 expressions to fail.  This option has no effect on platforms other than
417 @sc{ms-dos} and MS-Windows.
418
419 @item -u
420 @itemx --unix-byte-offsets
421 @opindex -u
422 @opindex --unix-byte-offsets
423 @cindex DOS byte offsets
424 @cindex byte offsets, on DOS/Windows
425 Report Unix-style byte offsets.  This switch causes
426 @command{grep} to report byte offsets as if the file were Unix style
427 text file, i.e., the byte offsets ignore the @code{CR} characters which were
428 stripped.  This will produce results identical to running @command{grep} on
429 a Unix machine.  This option has no effect unless @samp{-b}
430 option is also used; it has no effect on platforms other than @sc{ms-dos} and
431 MS-Windows.
432
433 @item --mmap
434 @opindex --mmap
435 @cindex memory mapped input
436 If possible, use the @code{mmap} system call to read input, instead of
437 the default @code{read} system call.  In some situations, @samp{--mmap}
438 yields better performance.  However, @samp{--mmap} can cause undefined
439 behavior (including core dumps) if an input file shrinks while
440 @command{grep} is operating, or if an I/O error occurs.
441
442 @item -Z
443 @itemx --null
444 @opindex -Z
445 @opindex --null
446 @cindex zero-terminated file names
447 Output a zero byte (the @sc{ascii} @code{NUL} character) instead of the
448 character that normally follows a file name.  For example, @samp{grep
449 -lZ} outputs a zero byte after each file name instead of the usual
450 newline.  This option makes the output unambiguous, even in the presence
451 of file names containing unusual characters like newlines.  This option
452 can be used with commands like @samp{find -print0}, @samp{perl -0},
453 @samp{sort -z}, and @samp{xargs -0} to process arbitrary file names,
454 even those that contain newline characters.
455
456 @item -z
457 @itemx --null-data
458 @opindex -z
459 @opindex --null-data
460 @cindex zero-terminated lines
461 Treat the input as a set of lines, each terminated by a zero byte (the
462 @sc{ascii} @code{NUL} character) instead of a newline.  Like the @samp{-Z}
463 or @samp{--null} option, this option can be used with commands like
464 @samp{sort -z} to process arbitrary file names.
465
466 @end table
467
468 Several additional options control which variant of the @command{grep}
469 matching engine is used.  @xref{Grep Programs}.
470
471 @section Environment Variables
472
473 Grep's behavior is affected by the following environment variables.
474 @cindex environment variables
475
476 @table @code
477
478 @item GREP_OPTIONS
479 @vindex GREP_OPTIONS
480 @cindex default options environment variable
481 This variable specifies default options to be placed in front of any
482 explicit options.  For example, if @code{GREP_OPTIONS} is
483 @samp{--binary-files=without-match --directories=skip}, @command{grep}
484 behaves as if the two options @samp{--binary-files=without-match} and
485 @samp{--directories=skip} had been specified before
486 any explicit options.  Option specifications are separated by
487 whitespace.  A backslash escapes the next character, so it can be used to
488 specify an option containing whitespace or a backslash.
489
490 @item LC_ALL
491 @itemx LC_MESSAGES
492 @itemx LANG
493 @vindex LC_ALL
494 @vindex LC_MESSAGES
495 @vindex LANG
496 @cindex language of messages
497 @cindex message language
498 @cindex national language support
499 @cindex NLS
500 @cindex translation of message language
501 These variables specify the @code{LC_MESSAGES} locale, which determines
502 the language that @command{grep} uses for messages.  The locale is determined
503 by the first of these variables that is set.  American English is used
504 if none of these environment variables are set, or if the message
505 catalog is not installed, or if @command{grep} was not compiled with national
506 language support (@sc{nls}).
507
508 @item LC_ALL
509 @itemx LC_CTYPE
510 @itemx LANG
511 @vindex LC_ALL
512 @vindex LC_CTYPE
513 @vindex LANG
514 @cindex character type
515 @cindex national language support
516 @cindex NLS
517 These variables specify the @code{LC_CTYPE} locale, which determines the
518 type of characters, e.g., which characters are whitespace.  The locale is
519 determined by the first of these variables that is set.  The @sc{posix}
520 locale is used if none of these environment variables are set, or if the
521 locale catalog is not installed, or if @command{grep} was not compiled with
522 national language support (@sc{nls}).
523
524 @item POSIXLY_CORRECT
525 @vindex POSIXLY_CORRECT
526 If set, @command{grep} behaves as @sc{posix.2} requires; otherwise,
527 @command{grep} behaves more like other @sc{gnu} programs.  @sc{posix.2}
528 requires that options that
529 follow file names must be treated as file names; by default, such
530 options are permuted to the front of the operand list and are treated as
531 options.  Also, @sc{posix.2} requires that unrecognized options be
532 diagnosed as
533 ``illegal'', but since they are not really against the law the default
534 is to diagnose them as ``invalid''.  @code{POSIXLY_CORRECT} also
535 disables @code{_@var{N}_GNU_nonoption_argv_flags_}, described below.
536
537 @item _@var{N}_GNU_nonoption_argv_flags_
538 @vindex _@var{N}_GNU_nonoption_argv_flags_
539 (Here @code{@var{N}} is @command{grep}'s numeric process ID.)  If the
540 @var{i}th character of this environment variable's value is @samp{1}, do
541 not consider the @var{i}th operand of @command{grep} to be an option, even if
542 it appears to be one.  A shell can put this variable in the environment
543 for each command it runs, specifying which operands are the results of
544 file name wildcard expansion and therefore should not be treated as
545 options.  This behavior is available only with the @sc{gnu} C library, and
546 only when @code{POSIXLY_CORRECT} is not set.
547
548 @end table
549
550 @node Diagnostics
551 @chapter Diagnostics
552
553 Normally, exit status is 0 if matches were found, and 1 if no matches
554 were found (the @samp{-v} option inverts the sense of the exit status).
555 Exit status is 2 if there were syntax errors in the pattern,
556 inaccessible input files, or other system errors.
557
558 @node Grep Programs
559 @chapter @command{grep} programs
560
561 @command{grep} searches the named input files (or standard input if no
562 files are named, or the file name @file{-} is given) for lines containing
563 a match to the given pattern.  By default, @command{grep} prints the
564 matching lines.  There are three major variants of @command{grep},
565 controlled by the following options.
566
567 @table @samp
568
569 @item -G
570 @itemx --basic-regexp
571 @opindex -G
572 @opindex --basic-regexp
573 @cindex matching basic regular expressions
574 Interpret pattern as a basic regular expression.  This is the default.
575
576 @item -E
577 @itemx --extended-regexp
578 @opindex -E
579 @opindex --extended-regexp
580 @cindex matching extended regular expressions
581 Interpret pattern as an extended regular expression.
582
583
584 @item -F
585 @itemx --fixed-strings
586 @opindex -F
587 @opindex --fixed-strings
588 @cindex matching fixed strings
589 Interpret pattern as a list of fixed strings, separated
590 by newlines, any of which is to be matched.
591
592 @end table
593
594 In addition, two variant programs @sc{egrep} and @sc{fgrep} are available.
595 @sc{egrep} is the same as @samp{grep -E}.  @sc{fgrep} is the
596 same as @samp{grep -F}.
597
598 @node Regular Expressions
599 @chapter Regular Expressions
600 @cindex regular expressions
601
602 A @dfn{regular expression} is a pattern that describes a set of strings.
603 Regular expressions are constructed analogously to arithmetic expressions,
604 by using various operators to combine smaller expressions.
605 @command{grep} understands two different versions of regular expression
606 syntax: ``basic'' and ``extended''.  In @sc{gnu} @command{grep}, there is no
607 difference in available functionality using either syntax.
608 In other implementations, basic regular expressions are less powerful.
609 The following description applies to extended regular expressions;
610 differences for basic regular expressions are summarized afterwards.
611
612 The fundamental building blocks are the regular expressions that match
613 a single character.  Most characters, including all letters and digits,
614 are regular expressions that match themselves.  Any metacharacter
615 with special meaning may be quoted by preceding it with a backslash.
616 A list of characters enclosed by @samp{[} and @samp{]} matches any
617 single character in that list; if the first character of the list is the
618 caret @samp{^}, then it
619 matches any character @strong{not} in the list.  For example, the regular
620 expression @samp{[0123456789]} matches any single digit.
621 A range of characters may be specified by giving the first
622 and last characters, separated by a hyphen.
623
624 Finally, certain named classes of characters are predefined, as follows.
625 Their interpretation depends on the @code{LC_CTYPE} locale; the
626 interpretation below is that of the @sc{posix} locale, which is the default
627 if no @code{LC_CTYPE} locale is specified.
628
629 @cindex classes of characters
630 @cindex character classes
631 @table @samp
632
633 @item [:alnum:]
634 @opindex alnum
635 @cindex alphanumeric characters
636 Alphanumeric characters:
637 @samp{[:alpha:]} and @samp{[:digit:]}.
638
639 @item [:alpha:]
640 @opindex alpha
641 @cindex alphabetic characters
642 Alphabetic characters:
643 @samp{[:lower:]} and @samp{[:upper:]}.
644
645 @item [:blank:]
646 @opindex blank
647 @cindex blank characters
648 Blank characters:
649 space and tab.
650
651 @item [:cntrl:]
652 @opindex cntrl
653 @cindex control characters
654 Control characters.  In @sc{ascii}, these characters have octal codes 000
655 through 037, and 177 (@code{DEL}).  In other character sets, these are
656 the equivalent characters, if any.
657
658 @item [:digit:]
659 @opindex digit
660 @cindex digit characters
661 @cindex numeric characters
662 Digits: @code{0 1 2 3 4 5 6 7 8 9}.
663
664 @item [:graph:]
665 @opindex graph
666 @cindex graphic characters
667 Graphical characters:
668 @samp{[:alnum:]} and @samp{[:punct:]}.
669
670 @item [:lower:]
671 @opindex lower
672 @cindex lower-case letters
673 Lower-case letters:
674 @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}.
675
676 @item [:print:]
677 @opindex print
678 @cindex printable characters
679 Printable characters:
680 @samp{[:alnum:]}, @samp{[:punct:]}, and space.
681
682 @item [:punct:]
683 @opindex punct
684 @cindex punctuation characters
685 Punctuation characters:
686 @code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}.
687
688 @item [:space:]
689 @opindex space
690 @cindex space characters
691 @cindex whitespace characters
692 Space characters:
693 tab, newline, vertical tab, form feed, carriage return, and space.
694
695 @item [:upper:]
696 @opindex upper
697 @cindex upper-case letters
698 Upper-case letters:
699 @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}.
700
701 @item [:xdigit:]
702 @opindex xdigit
703 @cindex xdigit class
704 @cindex hexadecimal digits
705 Hexadecimal digits:
706 @code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}.
707
708 @end table
709 For example, @samp{[[:alnum:]]} means @samp{[0-9A-Za-z]}, except the latter
710 depends upon the @sc{posix} locale and the @sc{ascii} character
711 encoding, whereas the former is independent of locale and character set.
712 (Note that the brackets in these class names are
713 part of the symbolic names, and must be included in addition to
714 the brackets delimiting the bracket list.)  Most metacharacters lose
715 their special meaning inside lists.  To include a literal @samp{]}, place it
716 first in the list.  Similarly, to include a literal @samp{^}, place it anywhere
717 but first.  Finally, to include a literal @samp{-}, place it last.
718
719 The period @samp{.} matches any single character.  The symbol @samp{\w}
720 is a synonym for @samp{[[:alnum:]]} and @samp{\W} is a synonym for
721 @samp{[^[:alnum]]}.
722
723 The caret @samp{^} and the dollar sign @samp{$} are metacharacters that
724 respectively match the empty string at the beginning and end
725 of a line.  The symbols @samp{\<} and @samp{\>} respectively match the
726 empty string at the beginning and end of a word.  The symbol
727 @samp{\b} matches the empty string at the edge of a word, and @samp{\B}
728 matches the empty string provided it's not at the edge of a word.
729
730 A regular expression may be followed by one of several
731 repetition operators:
732
733
734 @table @samp
735
736 @item ?
737 @opindex ?
738 @cindex question mark
739 @cindex match sub-expression at most once
740 The preceding item is optional and will be matched at most once.
741
742 @item *
743 @opindex *
744 @cindex asterisk
745 @cindex match sub-expression zero or more times
746 The preceding item will be matched zero or more times.
747
748 @item +
749 @opindex +
750 @cindex plus sign
751 The preceding item will be matched one or more times.
752
753 @item @{@var{n}@}
754 @opindex @{n@}
755 @cindex braces, one argument
756 @cindex match sub-expression n times
757 The preceding item is matched exactly @var{n} times.
758
759 @item @{@var{n},@}
760 @opindex @{n,@}
761 @cindex braces, second argument omitted
762 @cindex match sub-expression n or more times
763 The preceding item is matched n or more times.
764
765 @item @{@var{n},@var{m}@}
766 @opindex @{n,m@}
767 @cindex braces, two arguments
768 The preceding item is matched at least @var{n} times, but not more than
769 @var{m} times.
770
771 @end table
772
773 Two regular expressions may be concatenated; the resulting regular
774 expression matches any string formed by concatenating two substrings
775 that respectively match the concatenated subexpressions.
776
777 Two regular expressions may be joined by the infix operator @samp{|}; the
778 resulting regular expression matches any string matching either
779 subexpression.
780
781 Repetition takes precedence over concatenation, which in turn
782 takes precedence over alternation.  A whole subexpression may be
783 enclosed in parentheses to override these precedence rules.
784
785 The backreference @samp{\@var{n}}, where @var{n} is a single digit, matches the
786 substring previously matched by the @var{n}th parenthesized subexpression
787 of the regular expression.
788
789 @cindex basic regular expressions
790 In basic regular expressions the metacharacters @samp{?}, @samp{+},
791 @samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning;
792 instead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{},
793 @samp{\|}, @samp{\(}, and @samp{\)}.
794
795 @cindex interval specifications
796 Traditional @command{egrep} did not support the @samp{@{} metacharacter,
797 and some @command{egrep} implementations support @samp{\@{} instead, so
798 portable scripts should avoid @samp{@{} in @samp{egrep} patterns and
799 should use @samp{[@{]} to match a literal @samp{@{}.
800
801 @sc{gnu} @command{egrep} attempts to support traditional usage by
802 assuming that @samp{@{} is not special if it would be the start of an
803 invalid interval specification.  For example, the shell command
804 @samp{egrep '@{1'} searches for the two-character string @samp{@{1}
805 instead of reporting a syntax error in the regular expression.
806 @sc{posix.2} allows this behavior as an extension, but portable scripts
807 should avoid it.
808
809 @node Usage
810 @chapter Usage
811
812 @cindex Usage, examples
813 Here is an example shell command that invokes @sc{gnu} @command{grep}:
814
815 @example
816 grep -i 'hello.*world' menu.h main.c
817 @end example
818
819 @noindent
820 This lists all lines in the files @file{menu.h} and @file{main.c} that
821 contain the string @samp{hello} followed by the string @samp{world};
822 this is because @samp{.*} matches zero or more characters within a line.
823 @xref{Regular Expressions}.  The @samp{-i} option causes @command{grep}
824 to ignore case, causing it to match the line @samp{Hello, world!}, which
825 it would not otherwise match.  @xref{Invoking}, for more details about
826 how to invoke @command{grep}.
827
828 @cindex Using @command{grep}, Q&A
829 @cindex FAQ about @command{grep} usage
830 Here are some common questions and answers about @command{grep} usage.
831
832 @enumerate
833
834 @item
835 How can I list just the names of matching files?
836
837 @example
838 grep -l 'main' *.c
839 @end example 
840
841 @noindent
842 lists the names of all C files in the current directory whose contents
843 mention @samp{main}.
844
845 @item
846 How do I search directories recursively?
847
848 @example
849 grep -r 'hello' /home/gigi
850 @end example
851
852 @noindent
853 searches for @samp{hello} in all files under the directory
854 @file{/home/gigi}.  For more control of which files are searched, use
855 @command{find}, @command{grep} and @command{xargs}.  For example,
856 the following command searches only C files:
857
858 @smallexample
859 find /home/gigi -name '*.c' -print | xargs grep 'hello' /dev/null
860 @end smallexample
861
862 @item
863 What if a pattern has a leading @samp{-}?
864
865 @example
866 grep -e '--cut here--' *
867 @end example 
868
869 @noindent
870 searches for all lines matching @samp{--cut here--}.  Without @samp{-e},
871 @command{grep} would attempt to parse @samp{--cut here--} as a list of
872 options.
873
874 @item
875 Suppose I want to search for a whole word, not a part of a word?
876
877 @example
878 grep -w 'hello' *
879 @end example
880
881 @noindent
882 searches only for instances of @samp{hello} that are entire words; it
883 does not match @samp{Othello}.  For more control, use @samp{\<} and
884 @samp{\>} to match the start and end of words.  For example:
885
886 @example
887 grep 'hello\>' *
888 @end example
889
890 @noindent
891 searches only for words ending in @samp{hello}, so it matches the word
892 @samp{Othello}.
893
894 @item
895 How do I output context around the matching lines?
896
897 @example
898 grep -C 2 'hello' *
899 @end example
900
901 @noindent
902 prints two lines of context around each matching line.
903
904 @item
905 How do I force grep to print the name of the file?
906
907 Append @file{/dev/null}:
908
909 @example
910 grep 'eli' /etc/passwd /dev/null
911 @end example
912
913 @item
914 Why do people use strange regular expressions on @command{ps} output?
915
916 @example
917 ps -ef | grep '[c]ron'
918 @end example
919
920 If the pattern had been written without the square brackets, it would
921 have matched not only the @command{ps} output line for @command{cron},
922 but also the @command{ps} output line for @command{grep}.
923
924 @item
925 Why does @command{grep} report ``Binary file matches''?
926
927 If @command{grep} listed all matching ``lines'' from a binary file, it
928 would probably generate output that is not useful, and it might even
929 muck up your display.  So @sc{gnu} @command{grep} suppresses output from
930 files that appear to be binary files.  To force @sc{gnu} @command{grep}
931 to output lines even from files that appear to be binary, use the
932 @samp{-a} or @samp{--binary-files=text} option.  To eliminate the
933 ``Binary file matches'' messages, use the @samp{-I} or
934 @samp{--binary-files=without-match} option.
935
936 @item
937 Why doesn't @samp{grep -lv} print nonmatching file names?
938
939 @samp{grep -lv} lists the names of all files containing one or more
940 lines that do not match.  To list the names of all files that contain no
941 matching lines, use the @samp{-L} or @samp{--files-without-match}
942 option.
943
944 @item
945 I can do @sc{or} with @samp{|}, but what about @sc{and}?
946
947 @example
948 grep 'paul' /etc/motd | grep 'franc,ois'
949 @end example
950
951 @noindent
952 finds all lines that contain both @samp{paul} and @samp{franc,ois}.
953
954 @item
955 How can I search in both standard input and in files?
956
957 Use the special file name @samp{-}:
958
959 @example
960 cat /etc/passwd | grep 'alain' - /etc/motd
961 @end example
962 @end enumerate
963
964 @node Reporting Bugs
965 @chapter Reporting bugs
966
967 @cindex Bugs, reporting
968 Email bug reports to @email{bug-gnu-utils@@gnu.org}.
969 Be sure to include the word ``grep'' somewhere in the ``Subject:'' field.
970
971 Large repetition counts in the @samp{@{m,n@}} construct may cause
972 @command{grep} to use lots of memory.  In addition, certain other
973 obscure regular expressions require exponential time and
974 space, and may cause grep to run out of memory.
975 Backreferences are very slow, and may require exponential time.
976
977 @page
978 @node Concept Index
979 @unnumbered Concept Index
980
981 This is a general index of all issues discussed in this manual, with the
982 exception of the @command{grep} commands and command-line options.
983
984 @printindex cp
985
986 @page
987 @node Index
988 @unnumbered Index
989
990 This is an alphabetical list of all @command{grep} commands, command-line
991 options, and environment variables.
992
993 @printindex fn
994
995 @contents
996 @bye