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