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