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