grep.texi revision 126432
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 2000 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{} 2000 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. 130 131Though @command{grep} expects to do the matching on text, 132it has no limits on input line length other than available memory, 133and it can match arbitrary characters within a line. 134If the final byte of an input file is not a newline, 135@command{grep} silently supplies one. 136Since newline is also a separator for the list of patterns, there 137is 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} 143extensions. 144 145@table @samp 146 147@item -c 148@itemx --count 149@opindex -c 150@opindex -count 151@cindex counting lines 152Suppress normal output; instead print a count of matching 153lines for each input file. With the @samp{-v}, @samp{--invert-match} option, 154count 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 161Use @var{pattern} as the pattern; useful to protect patterns 162beginning 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 169Obtain patterns from @var{file}, one per line. The empty 170file 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 177Ignore 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 184Suppress normal output; instead print the name of each input 185file from which output would normally have been printed. 186The 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 193Prefix 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 202Quiet; suppress normal output. The scanning of every file will stop on 203the 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 210Suppress error messages about nonexistent or unreadable files. 211Portability 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 214like @sc{gnu} @command{grep}'s @samp{-q} option. Shell scripts intended 215to be portable to traditional @command{grep} should avoid both 216@samp{-q} and @samp{-s} and should redirect 217output 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 225Invert 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 232Select 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 246Print @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 254Print @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 261Print @var{num} lines (default 2) of output context. 262 263 264@item -@var{num} 265@opindex -NUM 266Same as @samp{--context=@var{num}} lines of leading and trailing 267context. 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 275Print the version number of @command{grep} to the standard output stream. 276This version number should be included in all bug reports. 277 278@item --help 279@opindex --help 280@cindex Usage summary, printing 281Print a usage message briefly summarizing these command-line options 282and the bug-reporting address, then exit. 283 284@itemx --binary-files=@var{type} 285@opindex --binary-files 286@cindex binary files 287If the first few bytes of a file indicate that the file contains binary 288data, assume that the file is of type @var{type}. By default, 289@var{type} is @samp{binary}, and @command{grep} normally outputs either 290a one-line message saying that a binary file matches, or no message if 291there is no match. If @var{type} is @samp{without-match}, 292@command{grep} assumes that a binary file does not match; 293this is equivalent to the @samp{-I} option. If @var{type} 294is @samp{text}, @command{grep} processes a binary file as if it were 295text; this is equivalent to the @samp{-a} option. 296@emph{Warning:} @samp{--binary-files=text} might output binary garbage, 297which can have nasty side effects if the output is a terminal and if the 298terminal 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 305Print the byte offset within the input file before each line of output. 306When @command{grep} runs on @sc{ms-dos} or MS-Windows, the printed 307byte offsets 308depend on whether the @samp{-u} (@samp{--unix-byte-offsets}) option is 309used; see below. 310 311@item -d @var{action} 312@itemx --directories=@var{action} 313@opindex -d 314@opindex --directories 315@cindex directory search 316If an input file is a directory, use @var{action} to process it. 317By default, @var{action} is @samp{read}, which means that directories are 318read just as if they were ordinary files (some operating systems 319and filesystems disallow this, and will cause @command{grep} to print error 320messages for every directory). If @var{action} is @samp{skip}, 321directories are silently skipped. If @var{action} is @samp{recurse}, 322@command{grep} reads all files under each directory, recursively; this is 323equivalent 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 330Print 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 337Suppress 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 344Suppress normal output; instead print the name of each input 345file from which no output would normally have been printed. 346The 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 354Process a binary file as if it were text; this is equivalent to the 355@samp{--binary-files=text} option. 356 357@item -I 358Process a binary file as if it did not contain matching data; this is 359equivalent 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 366Select only those lines containing matches that form 367whole words. The test is that the matching substring 368must either be at the beginning of the line, or preceded 369by a non-word constituent character. Similarly, 370it must be either at the end of the line or followed by 371a non-word constituent character. Word-constituent 372characters 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 380For each directory mentioned in the command line, read and process all 381files in that directory, recursively. This is the same as the @samp{-d 382recurse} option. 383 384@item -y 385@opindex -y 386@cindex case insensitive search, obsolete option 387Obsolete 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 395Treat the file(s) as binary. By default, under @sc{ms-dos} 396and MS-Windows, @command{grep} guesses the file type by looking 397at the contents of the first 32kB read from the file. 398If @command{grep} decides the file is a text file, it strips the 399@code{CR} characters from the original file contents (to make 400regular expressions with @code{^} and @code{$} work correctly). 401Specifying @samp{-U} overrules this guesswork, causing all 402files to be read and passed to the matching mechanism 403verbatim; if the file is a text file with @code{CR/LF} pairs 404at the end of each line, this will cause some regular 405expressions 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 414Report Unix-style byte offsets. This switch causes 415@command{grep} to report byte offsets as if the file were Unix style 416text file, i.e., the byte offsets ignore the @code{CR} characters which were 417stripped. This will produce results identical to running @command{grep} on 418a Unix machine. This option has no effect unless @samp{-b} 419option is also used; it has no effect on platforms other than @sc{ms-dos} and 420MS-Windows. 421 422@item --mmap 423@opindex --mmap 424@cindex memory mapped input 425If possible, use the @code{mmap} system call to read input, instead of 426the default @code{read} system call. In some situations, @samp{--mmap} 427yields better performance. However, @samp{--mmap} can cause undefined 428behavior (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 436Output a zero byte (the @sc{ascii} @code{NUL} character) instead of the 437character that normally follows a file name. For example, @samp{grep 438-lZ} outputs a zero byte after each file name instead of the usual 439newline. This option makes the output unambiguous, even in the presence 440of file names containing unusual characters like newlines. This option 441can be used with commands like @samp{find -print0}, @samp{perl -0}, 442@samp{sort -z}, and @samp{xargs -0} to process arbitrary file names, 443even 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 450Treat 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} 452or @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 457Several additional options control which variant of the @command{grep} 458matching engine is used. @xref{Grep Programs}. 459 460@section Environment Variables 461 462Grep'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 470This variable specifies default options to be placed in front of any 471explicit options. For example, if @code{GREP_OPTIONS} is 472@samp{--binary-files=without-match --directories=skip}, @command{grep} 473behaves as if the two options @samp{--binary-files=without-match} and 474@samp{--directories=skip} had been specified before 475any explicit options. Option specifications are separated by 476whitespace. A backslash escapes the next character, so it can be used to 477specify 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 490These variables specify the @code{LC_MESSAGES} locale, which determines 491the language that @command{grep} uses for messages. The locale is determined 492by the first of these variables that is set. American English is used 493if none of these environment variables are set, or if the message 494catalog is not installed, or if @command{grep} was not compiled with national 495language 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 506These variables specify the @code{LC_CTYPE} locale, which determines the 507type of characters, e.g., which characters are whitespace. The locale is 508determined by the first of these variables that is set. The @sc{posix} 509locale is used if none of these environment variables are set, or if the 510locale catalog is not installed, or if @command{grep} was not compiled with 511national language support (@sc{nls}). 512 513@item POSIXLY_CORRECT 514@vindex POSIXLY_CORRECT 515If set, @command{grep} behaves as @sc{posix.2} requires; otherwise, 516@command{grep} behaves more like other @sc{gnu} programs. @sc{posix.2} 517requires that options that 518follow file names must be treated as file names; by default, such 519options are permuted to the front of the operand list and are treated as 520options. Also, @sc{posix.2} requires that unrecognized options be 521diagnosed as 522``illegal'', but since they are not really against the law the default 523is to diagnose them as ``invalid''. @code{POSIXLY_CORRECT} also 524disables @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 530not consider the @var{i}th operand of @command{grep} to be an option, even if 531it appears to be one. A shell can put this variable in the environment 532for each command it runs, specifying which operands are the results of 533file name wildcard expansion and therefore should not be treated as 534options. This behavior is available only with the @sc{gnu} C library, and 535only when @code{POSIXLY_CORRECT} is not set. 536 537@end table 538 539@node Diagnostics 540@chapter Diagnostics 541 542Normally, exit status is 0 if matches were found, and 1 if no matches 543were found (the @samp{-v} option inverts the sense of the exit status). 544Exit status is 2 if there were syntax errors in the pattern, 545inaccessible 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 551files are named, or the file name @file{-} is given) for lines containing 552a match to the given pattern. By default, @command{grep} prints the 553matching lines. There are three major variants of @command{grep}, 554controlled 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 563Interpret 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 570Interpret 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 578Interpret pattern as a list of fixed strings, separated 579by newlines, any of which is to be matched. 580 581@end table 582 583In 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 585same as @samp{grep -F}. 586 587@node Regular Expressions 588@chapter Regular Expressions 589@cindex regular expressions 590 591A @dfn{regular expression} is a pattern that describes a set of strings. 592Regular expressions are constructed analogously to arithmetic expressions, 593by using various operators to combine smaller expressions. 594@command{grep} understands two different versions of regular expression 595syntax: ``basic'' and ``extended''. In @sc{gnu} @command{grep}, there is no 596difference in available functionality using either syntax. 597In other implementations, basic regular expressions are less powerful. 598The following description applies to extended regular expressions; 599differences for basic regular expressions are summarized afterwards. 600 601The fundamental building blocks are the regular expressions that match 602a single character. Most characters, including all letters and digits, 603are regular expressions that match themselves. Any metacharacter 604with special meaning may be quoted by preceding it with a backslash. 605A list of characters enclosed by @samp{[} and @samp{]} matches any 606single character in that list; if the first character of the list is the 607caret @samp{^}, then it 608matches any character @strong{not} in the list. For example, the regular 609expression @samp{[0123456789]} matches any single digit. 610A range of characters may be specified by giving the first 611and last characters, separated by a hyphen. 612 613Finally, certain named classes of characters are predefined, as follows. 614Their interpretation depends on the @code{LC_CTYPE} locale; the 615interpretation below is that of the @sc{posix} locale, which is the default 616if 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 625Alphanumeric characters: 626@samp{[:alpha:]} and @samp{[:digit:]}. 627 628@item [:alpha:] 629@opindex alpha 630@cindex alphabetic characters 631Alphabetic characters: 632@samp{[:lower:]} and @samp{[:upper:]}. 633 634@item [:blank:] 635@opindex blank 636@cindex blank characters 637Blank characters: 638space and tab. 639 640@item [:cntrl:] 641@opindex cntrl 642@cindex control characters 643Control characters. In @sc{ascii}, these characters have octal codes 000 644through 037, and 177 (@code{DEL}). In other character sets, these are 645the equivalent characters, if any. 646 647@item [:digit:] 648@opindex digit 649@cindex digit characters 650@cindex numeric characters 651Digits: @code{0 1 2 3 4 5 6 7 8 9}. 652 653@item [:graph:] 654@opindex graph 655@cindex graphic characters 656Graphical characters: 657@samp{[:alnum:]} and @samp{[:punct:]}. 658 659@item [:lower:] 660@opindex lower 661@cindex lower-case letters 662Lower-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 668Printable characters: 669@samp{[:alnum:]}, @samp{[:punct:]}, and space. 670 671@item [:punct:] 672@opindex punct 673@cindex punctuation characters 674Punctuation characters: 675@code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}. 676 677@item [:space:] 678@opindex space 679@cindex space characters 680@cindex whitespace characters 681Space characters: 682tab, newline, vertical tab, form feed, carriage return, and space. 683 684@item [:upper:] 685@opindex upper 686@cindex upper-case letters 687Upper-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 694Hexadecimal 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 698For example, @samp{[[:alnum:]]} means @samp{[0-9A-Za-z]}, except the latter 699depends upon the @sc{posix} locale and the @sc{ascii} character 700encoding, whereas the former is independent of locale and character set. 701(Note that the brackets in these class names are 702part of the symbolic names, and must be included in addition to 703the brackets delimiting the bracket list.) Most metacharacters lose 704their special meaning inside lists. To include a literal @samp{]}, place it 705first in the list. Similarly, to include a literal @samp{^}, place it anywhere 706but first. Finally, to include a literal @samp{-}, place it last. 707 708The period @samp{.} matches any single character. The symbol @samp{\w} 709is a synonym for @samp{[[:alnum:]]} and @samp{\W} is a synonym for 710@samp{[^[:alnum]]}. 711 712The caret @samp{^} and the dollar sign @samp{$} are metacharacters that 713respectively match the empty string at the beginning and end 714of a line. The symbols @samp{\<} and @samp{\>} respectively match the 715empty 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} 717matches the empty string provided it's not at the edge of a word. 718 719A regular expression may be followed by one of several 720repetition operators: 721 722 723@table @samp 724 725@item ? 726@opindex ? 727@cindex question mark 728@cindex match sub-expression at most once 729The 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 735The preceding item will be matched zero or more times. 736 737@item + 738@opindex + 739@cindex plus sign 740The 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 746The 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 752The preceding item is matched n or more times. 753 754@item @{@var{n},@var{m}@} 755@opindex @{n,m@} 756@cindex braces, two arguments 757The preceding item is matched at least @var{n} times, but not more than 758@var{m} times. 759 760@end table 761 762Two regular expressions may be concatenated; the resulting regular 763expression matches any string formed by concatenating two substrings 764that respectively match the concatenated subexpressions. 765 766Two regular expressions may be joined by the infix operator @samp{|}; the 767resulting regular expression matches any string matching either 768subexpression. 769 770Repetition takes precedence over concatenation, which in turn 771takes precedence over alternation. A whole subexpression may be 772enclosed in parentheses to override these precedence rules. 773 774The backreference @samp{\@var{n}}, where @var{n} is a single digit, matches the 775substring previously matched by the @var{n}th parenthesized subexpression 776of the regular expression. 777 778@cindex basic regular expressions 779In basic regular expressions the metacharacters @samp{?}, @samp{+}, 780@samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning; 781instead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{}, 782@samp{\|}, @samp{\(}, and @samp{\)}. 783 784@cindex interval specifications 785Traditional @command{egrep} did not support the @samp{@{} metacharacter, 786and some @command{egrep} implementations support @samp{\@{} instead, so 787portable scripts should avoid @samp{@{} in @samp{egrep} patterns and 788should use @samp{[@{]} to match a literal @samp{@{}. 789 790@sc{gnu} @command{egrep} attempts to support traditional usage by 791assuming that @samp{@{} is not special if it would be the start of an 792invalid interval specification. For example, the shell command 793@samp{egrep '@{1'} searches for the two-character string @samp{@{1} 794instead of reporting a syntax error in the regular expression. 795@sc{posix.2} allows this behavior as an extension, but portable scripts 796should avoid it. 797 798@node Usage 799@chapter Usage 800 801@cindex Usage, examples 802Here is an example shell command that invokes @sc{gnu} @command{grep}: 803 804@example 805grep -i 'hello.*world' menu.h main.c 806@end example 807 808@noindent 809This lists all lines in the files @file{menu.h} and @file{main.c} that 810contain the string @samp{hello} followed by the string @samp{world}; 811this is because @samp{.*} matches zero or more characters within a line. 812@xref{Regular Expressions}. The @samp{-i} option causes @command{grep} 813to ignore case, causing it to match the line @samp{Hello, world!}, which 814it would not otherwise match. @xref{Invoking}, for more details about 815how to invoke @command{grep}. 816 817@cindex Using @command{grep}, Q&A 818@cindex FAQ about @command{grep} usage 819Here are some common questions and answers about @command{grep} usage. 820 821@enumerate 822 823@item 824How can I list just the names of matching files? 825 826@example 827grep -l 'main' *.c 828@end example 829 830@noindent 831lists the names of all C files in the current directory whose contents 832mention @samp{main}. 833 834@item 835How do I search directories recursively? 836 837@example 838grep -r 'hello' /home/gigi 839@end example 840 841@noindent 842searches 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, 845the following command searches only C files: 846 847@smallexample 848find /home/gigi -name '*.c' -print | xargs grep 'hello' /dev/null 849@end smallexample 850 851@item 852What if a pattern has a leading @samp{-}? 853 854@example 855grep -e '--cut here--' * 856@end example 857 858@noindent 859searches for all lines matching @samp{--cut here--}. Without @samp{-e}, 860@command{grep} would attempt to parse @samp{--cut here--} as a list of 861options. 862 863@item 864Suppose I want to search for a whole word, not a part of a word? 865 866@example 867grep -w 'hello' * 868@end example 869 870@noindent 871searches only for instances of @samp{hello} that are entire words; it 872does 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 876grep 'hello\>' * 877@end example 878 879@noindent 880searches only for words ending in @samp{hello}, so it matches the word 881@samp{Othello}. 882 883@item 884How do I output context around the matching lines? 885 886@example 887grep -C 2 'hello' * 888@end example 889 890@noindent 891prints two lines of context around each matching line. 892 893@item 894How do I force grep to print the name of the file? 895 896Append @file{/dev/null}: 897 898@example 899grep 'eli' /etc/passwd /dev/null 900@end example 901 902@item 903Why do people use strange regular expressions on @command{ps} output? 904 905@example 906ps -ef | grep '[c]ron' 907@end example 908 909If the pattern had been written without the square brackets, it would 910have matched not only the @command{ps} output line for @command{cron}, 911but also the @command{ps} output line for @command{grep}. 912 913@item 914Why does @command{grep} report ``Binary file matches''? 915 916If @command{grep} listed all matching ``lines'' from a binary file, it 917would probably generate output that is not useful, and it might even 918muck up your display. So @sc{gnu} @command{grep} suppresses output from 919files that appear to be binary files. To force @sc{gnu} @command{grep} 920to 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 926Why doesn't @samp{grep -lv} print nonmatching file names? 927 928@samp{grep -lv} lists the names of all files containing one or more 929lines that do not match. To list the names of all files that contain no 930matching lines, use the @samp{-L} or @samp{--files-without-match} 931option. 932 933@item 934I can do @sc{or} with @samp{|}, but what about @sc{and}? 935 936@example 937grep 'paul' /etc/motd | grep 'franc,ois' 938@end example 939 940@noindent 941finds all lines that contain both @samp{paul} and @samp{franc,ois}. 942 943@item 944How can I search in both standard input and in files? 945 946Use the special file name @samp{-}: 947 948@example 949cat /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 957Email bug reports to @email{bug-gnu-utils@@gnu.org}. 958Be sure to include the word ``grep'' somewhere in the ``Subject:'' field. 959 960Large repetition counts in the @samp{@{m,n@}} construct may cause 961@command{grep} to use lots of memory. In addition, certain other 962obscure regular expressions require exponential time and 963space, and may cause grep to run out of memory. 964Backreferences are very slow, and may require exponential time. 965 966@page 967@node Concept Index 968@unnumbered Concept Index 969 970This is a general index of all issues discussed in this manual, with the 971exception of the @command{grep} commands and command-line options. 972 973@printindex cp 974 975@page 976@node Index 977@unnumbered Index 978 979This is an alphabetical list of all @command{grep} commands, command-line 980options, and environment variables. 981 982@printindex fn 983 984@contents 985@bye 986