1\input texinfo @c -*-texinfo-*- 2@c %**start of header 3@setfilename grep.info 4@include version.texi 5@settitle GNU Grep @value{VERSION} 6 7@c Combine indices. 8@syncodeindex ky cp 9@syncodeindex pg cp 10@syncodeindex tp cp 11@defcodeindex op 12@syncodeindex op cp 13@syncodeindex vr cp 14@c %**end of header 15 16@copying 17This manual is for @command{grep}, a pattern matching engine. 18 19Copyright @copyright{} 1999-2002, 2005, 2008-2011 Free Software Foundation, Inc. 20 21@quotation 22Permission is granted to copy, distribute and/or modify this document 23under the terms of the GNU Free Documentation License, Version 1.3 or 24any later version published by the Free Software Foundation; with no 25Invariant Sections, with no Front-Cover Texts, and with no Back-Cover 26Texts. A copy of the license is included in the section entitled 27``GNU Free Documentation License''. 28@end quotation 29@end copying 30 31@dircategory Text creation and manipulation 32@direntry 33* grep: (grep). Print lines matching a pattern. 34@end direntry 35 36@titlepage 37@title GNU Grep: Print lines matching a pattern 38@subtitle version @value{VERSION}, @value{UPDATED} 39@author Alain Magloire et al. 40@page 41@vskip 0pt plus 1filll 42@insertcopying 43@end titlepage 44 45@contents 46 47 48@ifnottex 49@node Top 50@top grep 51 52@command{grep} prints lines that match a pattern. 53 54This manual is for version @value{VERSION} of GNU Grep. 55 56@insertcopying 57@end ifnottex 58 59@menu 60* Introduction:: Introduction. 61* Invoking:: Command-line options, environment, exit status. 62* Regular Expressions:: Regular Expressions. 63* Usage:: Examples. 64* Reporting Bugs:: Reporting Bugs. 65* Copying:: License terms for this manual. 66* Index:: Combined index. 67@end menu 68 69 70@node Introduction 71@chapter Introduction 72 73@cindex searching for a pattern 74 75@command{grep} searches the input files 76for lines containing a match to a given pattern list. 77When it finds a match in a line, 78it copies the line to standard output (by default), 79or produces whatever other sort of output you have requested with options. 80 81Though @command{grep} expects to do the matching on text, 82it has no limits on input line length other than available memory, 83and it can match arbitrary characters within a line. 84If the final byte of an input file is not a newline, 85@command{grep} silently supplies one. 86Since newline is also a separator for the list of patterns, 87there is no way to match newline characters in a text. 88 89 90@node Invoking 91@chapter Invoking @command{grep} 92 93The general synopsis of the @command{grep} command line is 94 95@example 96grep @var{options} @var{pattern} @var{input_file_names} 97@end example 98 99@noindent 100There can be zero or more @var{options}. 101@var{pattern} will only be seen as such 102(and not as an @var{input_file_name}) 103if it wasn't already specified within @var{options} 104(by using the @samp{-e@ @var{pattern}} 105or @samp{-f@ @var{file}} options). 106There can be zero or more @var{input_file_names}. 107 108@menu 109* Command-line Options:: Short and long names, grouped by category. 110* Environment Variables:: POSIX, GNU generic, and GNU grep specific. 111* Exit Status:: Exit status returned by @command{grep}. 112* grep Programs:: @command{grep} programs. 113@end menu 114 115@node Command-line Options 116@section Command-line Options 117 118@command{grep} comes with a rich set of options: 119some from @sc{posix.2} and some being @sc{gnu} extensions. 120Long option names are always a @sc{gnu} extension, 121even for options that are from @sc{posix} specifications. 122Options that are specified by @sc{posix}, 123under their short names, 124are explicitly marked as such 125to facilitate @sc{posix}-portable programming. 126A few option names are provided 127for compatibility with older or more exotic implementations. 128 129@menu 130* Generic Program Information:: 131* Matching Control:: 132* General Output Control:: 133* Output Line Prefix Control:: 134* Context Line Control:: 135* File and Directory Selection:: 136* Other Options:: 137@end menu 138 139Several additional options control 140which variant of the @command{grep} matching engine is used. 141@xref{grep Programs}. 142 143@node Generic Program Information 144@subsection Generic Program Information 145 146@table @samp 147 148@item --help 149@opindex --help 150@cindex usage summary, printing 151Print a usage message briefly summarizing the command-line options 152and the bug-reporting address, then exit. 153 154@item -V 155@itemx --version 156@opindex -V 157@opindex --version 158@cindex version, printing 159Print the version number of @command{grep} to the standard output stream. 160This version number should be included in all bug reports. 161 162@end table 163 164@node Matching Control 165@subsection Matching Control 166 167@table @samp 168 169@item -e @var{pattern} 170@itemx --regexp=@var{pattern} 171@opindex -e 172@opindex --regexp=@var{pattern} 173@cindex pattern list 174Use @var{pattern} as the pattern. 175This can be used to specify multiple search patterns, 176or to protect a pattern beginning with a @samp{-}. 177(@samp{-e} is specified by @sc{posix}.) 178 179@item -f @var{file} 180@itemx --file=@var{file} 181@opindex -f 182@opindex --file 183@cindex pattern from file 184Obtain patterns from @var{file}, one per line. 185The empty file contains zero patterns, and therefore matches nothing. 186(@samp{-f} is specified by @sc{posix}.) 187 188@item -i 189@itemx -y 190@itemx --ignore-case 191@opindex -i 192@opindex -y 193@opindex --ignore-case 194@cindex case insensitive search 195Ignore case distinctions in both the pattern and the input files. 196@samp{-y} is an obsolete synonym that is provided for compatibility. 197(@samp{-i} is specified by @sc{posix}.) 198 199@item -v 200@itemx --invert-match 201@opindex -v 202@opindex --invert-match 203@cindex invert matching 204@cindex print non-matching lines 205Invert the sense of matching, to select non-matching lines. 206(@samp{-v} is specified by @sc{posix}.) 207 208@item -w 209@itemx --word-regexp 210@opindex -w 211@opindex --word-regexp 212@cindex matching whole words 213Select only those lines containing matches that form whole words. 214The test is that the matching substring must either 215be at the beginning of the line, 216or preceded by a non-word constituent character. 217Similarly, 218it must be either at the end of the line 219or followed by a non-word constituent character. 220Word-constituent characters are letters, digits, and the underscore. 221 222@item -x 223@itemx --line-regexp 224@opindex -x 225@opindex --line-regexp 226@cindex match the whole line 227Select only those matches that exactly match the whole line. 228(@samp{-x} is specified by @sc{posix}.) 229 230@end table 231 232@node General Output Control 233@subsection General Output Control 234 235@table @samp 236 237@item -c 238@itemx --count 239@opindex -c 240@opindex --count 241@cindex counting lines 242Suppress normal output; 243instead print a count of matching lines for each input file. 244With the @samp{-v}, @samp{--invert-match} option, 245count non-matching lines. 246(@samp{-c} is specified by @sc{posix}.) 247 248@item --color[=@var{WHEN}] 249@itemx --colour[=@var{WHEN}] 250@opindex --color 251@opindex --colour 252@cindex highlight, color, colour 253Surround the matched (non-empty) strings, matching lines, context lines, 254file names, line numbers, byte offsets, and separators (for fields and 255groups of context lines) with escape sequences to display them in color 256on the terminal. 257The colors are defined by the environment variable @var{GREP_COLORS} 258and default to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36} 259for bold red matched text, magenta file names, green line numbers, 260green byte offsets, cyan separators, and default terminal colors otherwise. 261The deprecated environment variable @var{GREP_COLOR} is still supported, 262but its setting does not have priority; 263it defaults to `01;31' (bold red) 264which only covers the color for matched text. 265@var{WHEN} is @samp{never}, @samp{always}, or @samp{auto}. 266 267@item -L 268@itemx --files-without-match 269@opindex -L 270@opindex --files-without-match 271@cindex files which don't match 272Suppress normal output; 273instead print the name of each input file from which 274no output would normally have been printed. 275The scanning of every file will stop on the first match. 276 277@item -l 278@itemx --files-with-matches 279@opindex -l 280@opindex --files-with-matches 281@cindex names of matching files 282Suppress normal output; 283instead print the name of each input file from which 284output would normally have been printed. 285The scanning of every file will stop on the first match. 286(@samp{-l} is specified by @sc{posix}.) 287 288@item -m @var{num} 289@itemx --max-count=@var{num} 290@opindex -m 291@opindex --max-count 292@cindex max-count 293Stop reading a file after @var{num} matching lines. 294If the input is standard input from a regular file, 295and @var{num} matching lines are output, 296@command{grep} ensures that the standard input is positioned 297just after the last matching line before exiting, 298regardless of the presence of trailing context lines. 299This enables a calling process to resume a search. 300For example, the following shell script makes use of it: 301 302@example 303while grep -m 1 PATTERN 304do 305 echo xxxx 306done < FILE 307@end example 308 309But the following probably will not work because a pipe is not a regular 310file: 311 312@example 313# This probably will not work. 314cat FILE | 315while grep -m 1 PATTERN 316do 317 echo xxxx 318done 319@end example 320 321When @command{grep} stops after @var{num} matching lines, 322it outputs any trailing context lines. 323Since context does not include matching lines, 324@command{grep} will stop when it encounters another matching line. 325When the @samp{-c} or @samp{--count} option is also used, 326@command{grep} does not output a count greater than @var{num}. 327When the @samp{-v} or @samp{--invert-match} option is also used, 328@command{grep} stops after outputting @var{num} non-matching lines. 329 330@item -o 331@itemx --only-matching 332@opindex -o 333@opindex --only-matching 334@cindex only matching 335Print only the matched (non-empty) parts of matching lines, 336with each such part on a separate output line. 337 338@item -q 339@itemx --quiet 340@itemx --silent 341@opindex -q 342@opindex --quiet 343@opindex --silent 344@cindex quiet, silent 345Quiet; do not write anything to standard output. 346Exit immediately with zero status if any match is found, 347even if an error was detected. 348Also see the @samp{-s} or @samp{--no-messages} option. 349(@samp{-q} is specified by @sc{posix}.) 350 351@item -s 352@itemx --no-messages 353@opindex -s 354@opindex --no-messages 355@cindex suppress error messages 356Suppress error messages about nonexistent or unreadable files. 357Portability note: 358unlike @sc{gnu} @command{grep}, 3597th Edition Unix @command{grep} did not conform to @sc{posix}, 360because it lacked @samp{-q} 361and its @samp{-s} option behaved like 362@sc{gnu} @command{grep}'s @samp{-q} option. 363@sc{usg}-style @command{grep} also lacked @samp{-q} 364but its @samp{-s} option behaved like @sc{gnu} @command{grep}'s. 365Portable shell scripts should avoid both 366@samp{-q} and @samp{-s} and should redirect 367standard and error output to @file{/dev/null} instead. 368(@samp{-s} is specified by @sc{posix}.) 369 370@end table 371 372@node Output Line Prefix Control 373@subsection Output Line Prefix Control 374 375When several prefix fields are to be output, 376the order is always file name, line number, and byte offset, 377regardless of the order in which these options were specified. 378 379@table @samp 380 381@item -b 382@itemx --byte-offset 383@opindex -b 384@opindex --byte-offset 385@cindex byte offset 386Print the 0-based byte offset within the input file 387before each line of output. 388If @samp{-o} (@samp{--only-matching}) is specified, 389print the offset of the matching part itself. 390When @command{grep} runs on @sc{ms-dos} or @sc{ms}-Windows, 391the printed byte offsets depend on whether 392the @samp{-u} (@samp{--unix-byte-offsets}) option is used; 393see below. 394 395@item -H 396@itemx --with-filename 397@opindex -H 398@opindex --with-filename 399@cindex with filename prefix 400Print the file name for each match. 401This is the default when there is more than one file to search. 402 403@item -h 404@itemx --no-filename 405@opindex -h 406@opindex --no-filename 407@cindex no filename prefix 408Suppress the prefixing of file names on output. 409This is the default when there is only one file 410(or only standard input) to search. 411 412@item --label=@var{LABEL} 413@opindex --label 414@cindex changing name of standard input 415Display input actually coming from standard input 416as input coming from file @var{LABEL}. This is 417especially useful when implementing tools like 418@command{zgrep}; e.g.: 419 420@example 421gzip -cd foo.gz | grep --label=foo -H something 422@end example 423 424@item -n 425@itemx --line-number 426@opindex -n 427@opindex --line-number 428@cindex line numbering 429Prefix each line of output with the 1-based line number within its input file. 430(@samp{-n} is specified by @sc{posix}.) 431 432@item -T 433@itemx --initial-tab 434@opindex -T 435@opindex --initial-tab 436@cindex tab-aligned content lines 437Make sure that the first character of actual line content lies on a tab stop, 438so that the alignment of tabs looks normal. 439This is useful with options that prefix their output to the actual content: 440@samp{-H}, @samp{-n}, and @samp{-b}. 441In order to improve the probability that lines 442from a single file will all start at the same column, 443this also causes the line number and byte offset (if present) 444to be printed in a minimum-size field width. 445 446@item -u 447@itemx --unix-byte-offsets 448@opindex -u 449@opindex --unix-byte-offsets 450@cindex @sc{ms-dos}/@sc{ms}-Windows byte offsets 451@cindex byte offsets, on @sc{ms-dos}/@sc{ms}-Windows 452Report Unix-style byte offsets. 453This option causes @command{grep} to report byte offsets 454as if the file were a Unix-style text file, 455i.e., the byte offsets ignore the @code{CR} characters that were stripped. 456This will produce results identical 457to running @command{grep} on a Unix machine. 458This option has no effect unless the @samp{-b} option is also used; 459it has no effect on platforms other than @sc{ms-dos} and @sc{ms}-Windows. 460 461@item -Z 462@itemx --null 463@opindex -Z 464@opindex --null 465@cindex zero-terminated file names 466Output a zero byte (the @sc{ascii} @code{NUL} character) 467instead of the character that normally follows a file name. 468For example, 469@samp{grep -lZ} outputs a zero byte after each file name 470instead of the usual newline. 471This option makes the output unambiguous, 472even in the presence of file names containing unusual characters like newlines. 473This option can be used with commands like 474@samp{find -print0}, @samp{perl -0}, @samp{sort -z}, and @samp{xargs -0} 475to process arbitrary file names, 476even those that contain newline characters. 477 478@end table 479 480@node Context Line Control 481@subsection Context Line Control 482 483Regardless of how these options are set, 484@command{grep} will never print any given line more than once. 485If the @samp{-o} or @samp{--only-matching} option is specified, 486these options have no effect and a warning is given upon their use. 487 488@table @samp 489 490@item -A @var{num} 491@itemx --after-context=@var{num} 492@opindex -A 493@opindex --after-context 494@cindex after context 495@cindex context lines, after match 496Print @var{num} lines of trailing context after matching lines. 497 498@item -B @var{num} 499@itemx --before-context=@var{num} 500@opindex -B 501@opindex --before-context 502@cindex before context 503@cindex context lines, before match 504Print @var{num} lines of leading context before matching lines. 505 506@item -C @var{num} 507@itemx -@var{num} 508@itemx --context=@var{num} 509@opindex -C 510@opindex --context 511@opindex -@var{num} 512@cindex context 513Print @var{num} lines of leading and trailing output context. 514 515@item --group-separator=@var{string} 516@opindex --group-separator 517@cindex group separator 518When @option{-A}, @option{-B} or @option{-C} are in use, 519print @var{string} instead of @samp{--} around disjoint groups 520of lines. 521 522@item --no-group-separator 523@opindex --group-separator 524@cindex group separator 525When @option{-A}, @option{-B} or @option{-C} are in use, 526print disjoint groups of lines adjacent to each other. 527 528@end table 529 530Matching lines normally use @samp{:} as a separator 531between prefix fields and actual line content. 532Context (i.e., non-matching) lines use @samp{-} instead. 533When no context is specified, 534matching lines are simply output one right after another. 535When nonzero context is specified, 536lines that are adjacent in the input form a group 537and are output one right after another, while 538a separator appears by default between disjoint groups on a line 539of its own and without any prefix. The default separator 540is @samp{--}, however whether to include it and its appearance 541can be changed with the options above. Each group may contain 542several matching lines when they are close enough to each other 543that two otherwise adjacent but divided groups connect 544and can just merge into a single contiguous one. 545 546@node File and Directory Selection 547@subsection File and Directory Selection 548 549@table @samp 550 551@item -a 552@itemx --text 553@opindex -a 554@opindex --text 555@cindex suppress binary data 556@cindex binary files 557Process a binary file as if it were text; 558this is equivalent to the @samp{--binary-files=text} option. 559 560@itemx --binary-files=@var{type} 561@opindex --binary-files 562@cindex binary files 563If the first few bytes of a file indicate that the file contains binary data, 564assume that the file is of type @var{type}. 565By default, @var{type} is @samp{binary}, 566and @command{grep} normally outputs either 567a one-line message saying that a binary file matches, 568or no message if there is no match. 569If @var{type} is @samp{without-match}, 570@command{grep} assumes that a binary file does not match; 571this is equivalent to the @samp{-I} option. 572If @var{type} is @samp{text}, 573@command{grep} processes a binary file as if it were text; 574this is equivalent to the @samp{-a} option. 575@emph{Warning:} @samp{--binary-files=text} might output binary garbage, 576which can have nasty side effects 577if the output is a terminal and 578if the terminal driver interprets some of it as commands. 579 580@item -D @var{action} 581@itemx --devices=@var{action} 582@opindex -D 583@opindex --devices 584@cindex device search 585If an input file is a device, FIFO, or socket, use @var{action} to process it. 586By default, @var{action} is @samp{read}, 587which means that devices are read just as if they were ordinary files. 588If @var{action} is @samp{skip}, 589devices, FIFOs, and sockets are silently skipped. 590 591@item -d @var{action} 592@itemx --directories=@var{action} 593@opindex -d 594@opindex --directories 595@cindex directory search 596If an input file is a directory, use @var{action} to process it. 597By default, @var{action} is @samp{read}, 598which means that directories are read just as if they were ordinary files 599(some operating systems and file systems disallow this, 600and will cause @command{grep} 601to print error messages for every directory or silently skip them). 602If @var{action} is @samp{skip}, directories are silently skipped. 603If @var{action} is @samp{recurse}, 604@command{grep} reads all files under each directory, recursively; 605this is equivalent to the @samp{-r} option. 606 607@item --exclude=@var{glob} 608@opindex --exclude 609@cindex exclude files 610@cindex searching directory trees 611Skip files whose base name matches @var{glob} 612(using wildcard matching). 613A file-name glob can use 614@samp{*}, @samp{?}, and @samp{[}...@samp{]} as wildcards, 615and @code{\} to quote a wildcard or backslash character literally. 616 617@item --exclude-from=@var{file} 618@opindex --exclude-from 619@cindex exclude files 620@cindex searching directory trees 621Skip files whose base name matches any of the file-name globs 622read from @var{file} (using wildcard matching as described 623under @samp{--exclude}). 624 625@item --exclude-dir=@var{dir} 626@opindex --exclude-dir 627@cindex exclude directories 628Exclude directories matching the pattern @var{dir} from recursive 629directory searches. 630 631@item -I 632Process a binary file as if it did not contain matching data; 633this is equivalent to the @samp{--binary-files=without-match} option. 634 635@item --include=@var{glob} 636@opindex --include 637@cindex include files 638@cindex searching directory trees 639Search only files whose base name matches @var{glob} 640(using wildcard matching as described under @samp{--exclude}). 641 642@item -r 643@itemx -R 644@itemx --recursive 645@opindex -r 646@opindex --recursive 647@cindex recursive search 648@cindex searching directory trees 649For each directory mentioned on the command line, 650read and process all files in that directory, recursively. 651This is the same as the @samp{--directories=recurse} option. 652 653@item -O 654@itemx --only-files 655@opindex -O 656@opindex --only-files 657@cindex ignoring special files 658@cindex ignoring symlinked directories 659Ignore all special files, except for symlinks. 660When recursing into directories, ignore symlinked directories as well. 661 662@end table 663 664@node Other Options 665@subsection Other Options 666 667@table @samp 668 669@item --line-buffered 670@opindex --line-buffered 671@cindex line buffering 672Use line buffering on output. 673This can cause a performance penalty. 674 675@item --mmap 676@opindex --mmap 677@cindex memory mapped input 678This option is ignored for backwards compatibility. It used to read 679input with the @code{mmap} system call, instead of the default @code{read} 680system call. On modern systems, @code{mmap} would rarely if ever yield 681better performance. 682 683@item -U 684@itemx --binary 685@opindex -U 686@opindex --binary 687@cindex @sc{ms-dos}/@sc{ms}-Windows binary files 688@cindex binary files, @sc{ms-dos}/@sc{ms}-Windows 689Treat the file(s) as binary. 690By default, under @sc{ms-dos} and @sc{ms}-Windows, 691@command{grep} guesses the file type 692by looking at the contents of the first 32kB read from the file. 693If @command{grep} decides the file is a text file, 694it strips the @code{CR} characters from the original file contents 695(to make regular expressions with @code{^} and @code{$} work correctly). 696Specifying @samp{-U} overrules this guesswork, 697causing all files to be read and passed to the matching mechanism verbatim; 698if the file is a text file with @code{CR/LF} pairs at the end of each line, 699this will cause some regular expressions to fail. 700This option has no effect 701on platforms other than @sc{ms-dos} and @sc{ms}-Windows. 702 703@item -z 704@itemx --null-data 705@opindex -z 706@opindex --null-data 707@cindex zero-terminated lines 708Treat the input as a set of lines, each terminated by a zero byte (the 709@sc{ascii} @code{NUL} character) instead of a newline. 710Like the @samp{-Z} or @samp{--null} option, 711this option can be used with commands like 712@samp{sort -z} to process arbitrary file names. 713 714@end table 715 716@node Environment Variables 717@section Environment Variables 718 719The behavior of @command{grep} is affected 720by the following environment variables. 721 722The locale for category @w{@code{LC_@var{foo}}} 723is specified by examining the three environment variables 724@env{LC_ALL}, @w{@env{LC_@var{foo}}}, and @env{LANG}, 725in that order. 726The first of these variables that is set specifies the locale. 727For example, if @env{LC_ALL} is not set, 728but @env{LC_MESSAGES} is set to @samp{pt_BR}, 729then the Brazilian Portuguese locale is used 730for the @code{LC_MESSAGES} category. 731The @samp{C} locale is used if none of these environment variables are set, 732if the locale catalog is not installed, 733or if @command{grep} was not compiled 734with national language support (@sc{nls}). 735 736@cindex environment variables 737 738@table @env 739 740@item GREP_OPTIONS 741@vindex GREP_OPTIONS @r{environment variable} 742@cindex default options environment variable 743This variable specifies default options to be placed in front of any 744explicit options. 745For example, if @code{GREP_OPTIONS} is 746@samp{--binary-files=without-match --directories=skip}, @command{grep} 747behaves as if the two options @samp{--binary-files=without-match} and 748@samp{--directories=skip} had been specified before 749any explicit options. 750Option specifications are separated by 751whitespace. 752A backslash escapes the next character, so it can be used to 753specify an option containing whitespace or a backslash. 754 755@item GREP_COLOR 756@vindex GREP_COLOR @r{environment variable} 757@cindex highlight markers 758This variable specifies the color used to highlight matched (non-empty) text. 759It is deprecated in favor of @code{GREP_COLORS}, but still supported. 760The @samp{mt}, @samp{ms}, and @samp{mc} capabilities of @code{GREP_COLORS} 761have priority over it. 762It can only specify the color used to highlight 763the matching non-empty text in any matching line 764(a selected line when the @samp{-v} command-line option is omitted, 765or a context line when @samp{-v} is specified). 766The default is @samp{01;31}, 767which means a bold red foreground text on the terminal's default background. 768 769@item GREP_COLORS 770@vindex GREP_COLORS @r{environment variable} 771@cindex highlight markers 772This variable specifies the colors and other attributes 773used to highlight various parts of the output. 774Its value is a colon-separated list of capabilities 775that defaults to @samp{ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36} 776with the @samp{rv} and @samp{ne} boolean capabilities omitted (i.e., false). 777Supported capabilities are as follows. 778 779@table @code 780@item sl= 781@vindex sl GREP_COLORS @r{capability} 782SGR substring for whole selected lines 783(i.e., 784matching lines when the @samp{-v} command-line option is omitted, 785or non-matching lines when @samp{-v} is specified). 786If however the boolean @samp{rv} capability 787and the @samp{-v} command-line option are both specified, 788it applies to context matching lines instead. 789The default is empty (i.e., the terminal's default color pair). 790 791@item cx= 792@vindex cx GREP_COLORS @r{capability} 793SGR substring for whole context lines 794(i.e., 795non-matching lines when the @samp{-v} command-line option is omitted, 796or matching lines when @samp{-v} is specified). 797If however the boolean @samp{rv} capability 798and the @samp{-v} command-line option are both specified, 799it applies to selected non-matching lines instead. 800The default is empty (i.e., the terminal's default color pair). 801 802@item rv 803@vindex rv GREP_COLORS @r{capability} 804Boolean value that reverses (swaps) the meanings of 805the @samp{sl=} and @samp{cx=} capabilities 806when the @samp{-v} command-line option is specified. 807The default is false (i.e., the capability is omitted). 808 809@item mt=01;31 810@vindex mt GREP_COLORS @r{capability} 811SGR substring for matching non-empty text in any matching line 812(i.e., 813a selected line when the @samp{-v} command-line option is omitted, 814or a context line when @samp{-v} is specified). 815Setting this is equivalent to setting both @samp{ms=} and @samp{mc=} 816at once to the same value. 817The default is a bold red text foreground over the current line background. 818 819@item ms=01;31 820@vindex ms GREP_COLORS @r{capability} 821SGR substring for matching non-empty text in a selected line. 822(This is only used when the @samp{-v} command-line option is omitted.) 823The effect of the @samp{sl=} (or @samp{cx=} if @samp{rv}) capability 824remains active when this kicks in. 825The default is a bold red text foreground over the current line background. 826 827@item mc=01;31 828@vindex mc GREP_COLORS @r{capability} 829SGR substring for matching non-empty text in a context line. 830(This is only used when the @samp{-v} command-line option is specified.) 831The effect of the @samp{cx=} (or @samp{sl=} if @samp{rv}) capability 832remains active when this kicks in. 833The default is a bold red text foreground over the current line background. 834 835@item fn=35 836@vindex fn GREP_COLORS @r{capability} 837SGR substring for file names prefixing any content line. 838The default is a magenta text foreground over the terminal's default background. 839 840@item ln=32 841@vindex ln GREP_COLORS @r{capability} 842SGR substring for line numbers prefixing any content line. 843The default is a green text foreground over the terminal's default background. 844 845@item bn=32 846@vindex bn GREP_COLORS @r{capability} 847SGR substring for byte offsets prefixing any content line. 848The default is a green text foreground over the terminal's default background. 849 850@item se=36 851@vindex fn GREP_COLORS @r{capability} 852SGR substring for separators that are inserted 853between selected line fields (@samp{:}), 854between context line fields (@samp{-}), 855and between groups of adjacent lines 856when nonzero context is specified (@samp{--}). 857The default is a cyan text foreground over the terminal's default background. 858 859@item ne 860@vindex ne GREP_COLORS @r{capability} 861Boolean value that prevents clearing to the end of line 862using Erase in Line (EL) to Right (@samp{\33[K}) 863each time a colorized item ends. 864This is needed on terminals on which EL is not supported. 865It is otherwise useful on terminals 866for which the @code{back_color_erase} 867(@code{bce}) boolean terminfo capability does not apply, 868when the chosen highlight colors do not affect the background, 869or when EL is too slow or causes too much flicker. 870The default is false (i.e., the capability is omitted). 871@end table 872 873Note that boolean capabilities have no @samp{=}... part. 874They are omitted (i.e., false) by default and become true when specified. 875 876See the Select Graphic Rendition (SGR) section 877in the documentation of your text terminal 878for permitted values and their meaning as character attributes. 879These substring values are integers in decimal representation 880and can be concatenated with semicolons. 881@command{grep} takes care of assembling the result 882into a complete SGR sequence (@samp{\33[}...@samp{m}). 883Common values to concatenate include 884@samp{1} for bold, 885@samp{4} for underline, 886@samp{5} for blink, 887@samp{7} for inverse, 888@samp{39} for default foreground color, 889@samp{30} to @samp{37} for foreground colors, 890@samp{90} to @samp{97} for 16-color mode foreground colors, 891@samp{38;5;0} to @samp{38;5;255} 892for 88-color and 256-color modes foreground colors, 893@samp{49} for default background color, 894@samp{40} to @samp{47} for background colors, 895@samp{100} to @samp{107} for 16-color mode background colors, 896and @samp{48;5;0} to @samp{48;5;255} 897for 88-color and 256-color modes background colors. 898 899@item LC_ALL 900@itemx LC_COLLATE 901@itemx LANG 902@vindex LC_ALL @r{environment variable} 903@vindex LC_COLLATE @r{environment variable} 904@vindex LANG @r{environment variable} 905@cindex character type 906@cindex national language support 907@cindex NLS 908These variables specify the locale for the @code{LC_COLLATE} category, 909which determines the collating sequence 910used to interpret range expressions like @samp{[a-z]}. 911 912@item LC_ALL 913@itemx LC_CTYPE 914@itemx LANG 915@vindex LC_ALL @r{environment variable} 916@vindex LC_CTYPE @r{environment variable} 917@vindex LANG @r{environment variable} 918These variables specify the locale for the @code{LC_CTYPE} category, 919which determines the type of characters, 920e.g., which characters are whitespace. 921 922@item LC_ALL 923@itemx LC_MESSAGES 924@itemx LANG 925@vindex LC_ALL @r{environment variable} 926@vindex LC_MESSAGES @r{environment variable} 927@vindex LANG @r{environment variable} 928@cindex language of messages 929@cindex message language 930@cindex national language support 931@cindex translation of message language 932These variables specify the locale for the @code{LC_MESSAGES} category, 933which determines the language that @command{grep} uses for messages. 934The default @samp{C} locale uses American English messages. 935 936@item POSIXLY_CORRECT 937@vindex POSIXLY_CORRECT @r{environment variable} 938If set, @command{grep} behaves as @sc{posix.2} requires; otherwise, 939@command{grep} behaves more like other @sc{gnu} programs. 940@sc{posix.2} 941requires that options that 942follow file names must be treated as file names; 943by default, 944such options are permuted to the front of the operand list 945and are treated as options. 946Also, @code{POSIXLY_CORRECT} disables special handling of an 947invalid bracket expression. @xref{invalid-bracket-expr}. 948 949@item _@var{N}_GNU_nonoption_argv_flags_ 950@vindex _@var{N}_GNU_nonoption_argv_flags_ @r{environment variable} 951(Here @code{@var{N}} is @command{grep}'s numeric process ID.) 952If the @var{i}th character of this environment variable's value is @samp{1}, 953do not consider the @var{i}th operand of @command{grep} to be an option, 954even if it appears to be one. 955A shell can put this variable in the environment for each command it runs, 956specifying which operands are the results of file name wildcard expansion 957and therefore should not be treated as options. 958This behavior is available only with the @sc{gnu} C library, 959and only when @code{POSIXLY_CORRECT} is not set. 960 961@end table 962 963 964@node Exit Status 965@section Exit Status 966@cindex exit status 967@cindex return status 968 969Normally, the exit status is 0 if selected lines are found and 1 otherwise. 970But the exit status is 2 if an error occurred, unless the @option{-q} or 971@option{--quiet} or @option{--silent} option is used and a selected line 972is found. 973Note, however, that @sc{posix} only mandates, 974for programs such as @command{grep}, @command{cmp}, and @command{diff}, 975that the exit status in case of error be greater than 1; 976it is therefore advisable, for the sake of portability, 977to use logic that tests for this general condition 978instead of strict equality with@ 2. 979 980 981@node grep Programs 982@section @command{grep} Programs 983@cindex @command{grep} programs 984@cindex variants of @command{gerp} 985 986@command{grep} searches the named input files 987(or standard input if no files are named, 988or the file name @file{-} is given) 989for lines containing a match to the given pattern. 990By default, @command{grep} prints the matching lines. 991There are four major variants of @command{grep}, 992controlled by the following options. 993 994@table @samp 995 996@item -G 997@itemx --basic-regexp 998@opindex -G 999@opindex --basic-regexp 1000@cindex matching basic regular expressions 1001Interpret the pattern as a basic regular expression (BRE). 1002This is the default. 1003 1004@item -E 1005@itemx --extended-regexp 1006@opindex -E 1007@opindex --extended-regexp 1008@cindex matching extended regular expressions 1009Interpret the pattern as an extended regular expression (ERE). 1010(@samp{-E} is specified by @sc{posix}.) 1011 1012@item -F 1013@itemx --fixed-strings 1014@opindex -F 1015@opindex --fixed-strings 1016@cindex matching fixed strings 1017Interpret the pattern as a list of fixed strings, separated 1018by newlines, any of which is to be matched. 1019(@samp{-F} is specified by @sc{posix}.) 1020 1021@item -P 1022@itemx --perl-regexp 1023@opindex -P 1024@opindex --perl-regexp 1025@cindex matching Perl regular expressions 1026Interpret the pattern as a Perl regular expression. 1027This is highly experimental and 1028@samp{grep@ -P} may warn of unimplemented features. 1029 1030@end table 1031 1032In addition, 1033two variant programs @command{egrep} and @command{fgrep} are available. 1034@command{egrep} is the same as @samp{grep@ -E}. 1035@command{fgrep} is the same as @samp{grep@ -F}. 1036Direct invocation as either 1037@command{egrep} or @command{fgrep} is deprecated, 1038but is provided to allow historical applications 1039that rely on them to run unmodified. 1040 1041 1042@node Regular Expressions 1043@chapter Regular Expressions 1044@cindex regular expressions 1045 1046A @dfn{regular expression} is a pattern that describes a set of strings. 1047Regular expressions are constructed analogously to arithmetic expressions, 1048by using various operators to combine smaller expressions. 1049@command{grep} understands 1050three different versions of regular expression syntax: 1051``basic,'' (BRE) ``extended'' (ERE) and ``perl''. 1052In @sc{gnu} @command{grep}, 1053there is no difference in available functionality between basic and 1054extended syntaxes. 1055In other implementations, basic regular expressions are less powerful. 1056The following description applies to extended regular expressions; 1057differences for basic regular expressions are summarized afterwards. 1058Perl regular expressions give additional functionality, and are 1059documented in pcresyntax(3) and pcrepattern(3), but may not be 1060available on every system. 1061 1062@menu 1063* Fundamental Structure:: 1064* Character Classes and Bracket Expressions:: 1065* The Backslash Character and Special Expressions:: 1066* Anchoring:: 1067* Back-references and Subexpressions:: 1068* Basic vs Extended:: 1069@end menu 1070 1071@node Fundamental Structure 1072@section Fundamental Structure 1073 1074The fundamental building blocks are the regular expressions that match 1075a single character. 1076Most characters, including all letters and digits, 1077are regular expressions that match themselves. 1078Any meta-character 1079with special meaning may be quoted by preceding it with a backslash. 1080 1081A regular expression may be followed by one of several 1082repetition operators: 1083 1084@table @samp 1085 1086@item . 1087@opindex . 1088@cindex dot 1089@cindex period 1090The period @samp{.} matches any single character. 1091 1092@item ? 1093@opindex ? 1094@cindex question mark 1095@cindex match expression at most once 1096The preceding item is optional and will be matched at most once. 1097 1098@item * 1099@opindex * 1100@cindex asterisk 1101@cindex match expression zero or more times 1102The preceding item will be matched zero or more times. 1103 1104@item + 1105@opindex + 1106@cindex plus sign 1107@cindex match expression one or more times 1108The preceding item will be matched one or more times. 1109 1110@item @{@var{n}@} 1111@opindex @{@var{n}@} 1112@cindex braces, one argument 1113@cindex match expression @var{n} times 1114The preceding item is matched exactly @var{n} times. 1115 1116@item @{@var{n},@} 1117@opindex @{@var{n},@} 1118@cindex braces, second argument omitted 1119@cindex match expression @var{n} or more times 1120The preceding item is matched @var{n} or more times. 1121 1122@item @{,@var{m}@} 1123@opindex @{,@var{m}@} 1124@cindex braces, first argument omitted 1125@cindex match expression at most @var{m} times 1126The preceding item is matched at most @var{m} times. 1127 1128@item @{@var{n},@var{m}@} 1129@opindex @{@var{n},@var{m}@} 1130@cindex braces, two arguments 1131@cindex match expression from @var{n} to @var{m} times 1132The preceding item is matched at least @var{n} times, but not more than 1133@var{m} times. 1134 1135@end table 1136 1137Two regular expressions may be concatenated; 1138the resulting regular expression 1139matches any string formed by concatenating two substrings 1140that respectively match the concatenated expressions. 1141 1142Two regular expressions may be joined by the infix operator @samp{|}; 1143the resulting regular expression 1144matches any string matching either alternalte expression. 1145 1146Repetition takes precedence over concatenation, 1147which in turn takes precedence over alternation. 1148A whole expression may be enclosed in parentheses 1149to override these precedence rules and form a subexpression. 1150 1151@node Character Classes and Bracket Expressions 1152@section Character Classes and Bracket Expressions 1153 1154@cindex bracket expression 1155@cindex character class 1156A @dfn{bracket expression} is a list of characters enclosed by @samp{[} and 1157@samp{]}. 1158It matches any single character in that list; 1159if the first character of the list is the caret @samp{^}, 1160then it matches any character @strong{not} in the list. 1161For example, the regular expression 1162@samp{[0123456789]} matches any single digit. 1163 1164@cindex range expression 1165Within a bracket expression, a @dfn{range expression} consists of two 1166characters separated by a hyphen. 1167It matches any single character that 1168sorts between the two characters, inclusive, using the locale's 1169collating sequence and character set. 1170For example, in the default C 1171locale, @samp{[a-d]} is equivalent to @samp{[abcd]}. 1172Many locales sort 1173characters in dictionary order, and in these locales @samp{[a-d]} is 1174typically not equivalent to @samp{[abcd]}; 1175it might be equivalent to @samp{[aBbCcDd]}, for example. 1176To obtain the traditional interpretation 1177of bracket expressions, you can use the @samp{C} locale by setting the 1178@env{LC_ALL} environment variable to the value @samp{C}. 1179 1180Finally, certain named classes of characters are predefined within 1181bracket expressions, as follows. 1182Their interpretation depends on the @code{LC_CTYPE} locale; 1183the interpretation below is that of the @samp{C} locale, 1184which is the default if no @code{LC_CTYPE} locale is specified. 1185 1186@cindex classes of characters 1187@cindex character classes 1188@table @samp 1189 1190@item [:alnum:] 1191@opindex alnum @r{character class} 1192@cindex alphanumeric characters 1193Alphanumeric characters: 1194@samp{[:alpha:]} and @samp{[:digit:]}. 1195 1196@item [:alpha:] 1197@opindex alpha @r{character class} 1198@cindex alphabetic characters 1199Alphabetic characters: 1200@samp{[:lower:]} and @samp{[:upper:]}. 1201 1202@item [:blank:] 1203@opindex blank @r{character class} 1204@cindex blank characters 1205Blank characters: 1206space and tab. 1207 1208@item [:cntrl:] 1209@opindex cntrl @r{character class} 1210@cindex control characters 1211Control characters. 1212In @sc{ascii}, these characters have octal codes 000 1213through 037, and 177 (@code{DEL}). 1214In other character sets, these are 1215the equivalent characters, if any. 1216 1217@item [:digit:] 1218@opindex digit @r{character class} 1219@cindex digit characters 1220@cindex numeric characters 1221Digits: @code{0 1 2 3 4 5 6 7 8 9}. 1222 1223@item [:graph:] 1224@opindex graph @r{character class} 1225@cindex graphic characters 1226Graphical characters: 1227@samp{[:alnum:]} and @samp{[:punct:]}. 1228 1229@item [:lower:] 1230@opindex lower @r{character class} 1231@cindex lower-case letters 1232Lower-case letters: 1233@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}. 1234 1235@item [:print:] 1236@opindex print @r{character class} 1237@cindex printable characters 1238Printable characters: 1239@samp{[:alnum:]}, @samp{[:punct:]}, and space. 1240 1241@item [:punct:] 1242@opindex punct @r{character class} 1243@cindex punctuation characters 1244Punctuation characters: 1245@code{!@: " # $ % & ' ( ) * + , - .@: / : ; < = > ?@: @@ [ \ ] ^ _ ` @{ | @} ~}. 1246 1247@item [:space:] 1248@opindex space @r{character class} 1249@cindex space characters 1250@cindex whitespace characters 1251Space characters: 1252tab, newline, vertical tab, form feed, carriage return, and space. 1253@xref{Usage}, for more discussion of matching newlines. 1254 1255@item [:upper:] 1256@opindex upper @r{character class} 1257@cindex upper-case letters 1258Upper-case letters: 1259@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}. 1260 1261@item [:xdigit:] 1262@opindex xdigit @r{character class} 1263@cindex xdigit class 1264@cindex hexadecimal digits 1265Hexadecimal digits: 1266@code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}. 1267 1268@end table 1269For example, @samp{[[:alnum:]]} means @samp{[0-9A-Za-z]}, except the latter 1270depends upon the @samp{C} locale and the @sc{ascii} character 1271encoding, whereas the former is independent of locale and character set. 1272(Note that the brackets in these class names are 1273part of the symbolic names, and must be included in addition to 1274the brackets delimiting the bracket expression.) 1275 1276@anchor{invalid-bracket-expr} 1277If you mistakenly omit the outer brackets, and search for say, @samp{[:upper:]}, 1278GNU @command{grep} prints a diagnostic and exits with status 2, on 1279the assumption that you did not intend to search for the nominally 1280equivalent regular expression: @samp{[:epru]}. 1281Set the @code{POSIXLY_CORRECT} environment variable to disable this feature. 1282 1283Most meta-characters lose their special meaning inside bracket expressions. 1284 1285@table @samp 1286@item ] 1287ends the bracket expression if it's not the first list item. 1288So, if you want to make the @samp{]} character a list item, 1289you must put it first. 1290 1291@item [. 1292represents the open collating symbol. 1293 1294@item .] 1295represents the close collating symbol. 1296 1297@item [= 1298represents the open equivalence class. 1299 1300@item =] 1301represents the close equivalence class. 1302 1303@item [: 1304represents the open character class symbol, and should be followed by a valid character class name. 1305 1306@item :] 1307represents the close character class symbol. 1308 1309@item - 1310represents the range if it's not first or last in a list or the ending point 1311of a range. 1312 1313@item ^ 1314represents the characters not in the list. 1315If you want to make the @samp{^} 1316character a list item, place it anywhere but first. 1317 1318@end table 1319 1320@node The Backslash Character and Special Expressions 1321@section The Backslash Character and Special Expressions 1322@cindex backslash 1323 1324The @samp{\} character, 1325when followed by certain ordinary characters, 1326takes a special meaning: 1327 1328@table @samp 1329 1330@item @samp{\b} 1331Match the empty string at the edge of a word. 1332 1333@item @samp{\B} 1334Match the empty string provided it's not at the edge of a word. 1335 1336@item @samp{\<} 1337Match the empty string at the beginning of word. 1338 1339@item @samp{\>} 1340Match the empty string at the end of word. 1341 1342@item @samp{\w} 1343Match word constituent, it is a synonym for @samp{[[:alnum:]]}. 1344 1345@item @samp{\W} 1346Match non-word constituent, it is a synonym for @samp{[^[:alnum:]]}. 1347 1348@item @samp{\s} 1349Match whitespace, it is a synonym for @samp{[[:space:]]}. 1350 1351@item @samp{\S} 1352Match non-whitespace, it is a synonym for @samp{[^[:space:]]}. 1353 1354@end table 1355 1356For example, @samp{\brat\b} matches the separate word @samp{rat}, 1357@samp{\Brat\B} matches @samp{crate} but not @samp{furry rat}. 1358 1359@node Anchoring 1360@section Anchoring 1361@cindex anchoring 1362 1363The caret @samp{^} and the dollar sign @samp{$} are meta-characters that 1364respectively match the empty string at the beginning and end of a line. 1365 1366@node Back-references and Subexpressions 1367@section Back-references and Subexpressions 1368@cindex subexpression 1369@cindex back-reference 1370 1371The back-reference @samp{\@var{n}}, where @var{n} is a single digit, matches 1372the substring previously matched by the @var{n}th parenthesized subexpression 1373of the regular expression. 1374For example, @samp{(a)\1} matches @samp{aa}. 1375When used with alternation, if the group does not participate in the match then 1376the back-reference makes the whole match fail. 1377For example, @samp{a(.)|b\1} 1378will not match @samp{ba}. 1379When multiple regular expressions are given with 1380@samp{-e} or from a file (@samp{-f file}), 1381back-references are local to each expression. 1382 1383@node Basic vs Extended 1384@section Basic vs Extended Regular Expressions 1385@cindex basic regular expressions 1386 1387In basic regular expressions the meta-characters @samp{?}, @samp{+}, 1388@samp{@{}, @samp{|}, @samp{(}, and @samp{)} lose their special meaning; 1389instead use the backslashed versions @samp{\?}, @samp{\+}, @samp{\@{}, 1390@samp{\|}, @samp{\(}, and @samp{\)}. 1391 1392@cindex interval specifications 1393Traditional @command{egrep} did not support the @samp{@{} meta-character, 1394and some @command{egrep} implementations support @samp{\@{} instead, so 1395portable scripts should avoid @samp{@{} in @samp{grep@ -E} patterns and 1396should use @samp{[@{]} to match a literal @samp{@{}. 1397 1398@sc{gnu} @command{grep@ -E} attempts to support traditional usage by 1399assuming that @samp{@{} is not special if it would be the start of an 1400invalid interval specification. 1401For example, the command 1402@samp{grep@ -E@ '@{1'} searches for the two-character string @samp{@{1} 1403instead of reporting a syntax error in the regular expression. 1404@sc{posix.2} allows this behavior as an extension, but portable scripts 1405should avoid it. 1406 1407 1408@node Usage 1409@chapter Usage 1410 1411@cindex usage, examples 1412Here is an example command that invokes @sc{gnu} @command{grep}: 1413 1414@example 1415grep -i 'hello.*world' menu.h main.c 1416@end example 1417 1418@noindent 1419This lists all lines in the files @file{menu.h} and @file{main.c} that 1420contain the string @samp{hello} followed by the string @samp{world}; 1421this is because @samp{.*} matches zero or more characters within a line. 1422@xref{Regular Expressions}. 1423The @samp{-i} option causes @command{grep} 1424to ignore case, causing it to match the line @samp{Hello, world!}, which 1425it would not otherwise match. 1426@xref{Invoking}, for more details about 1427how to invoke @command{grep}. 1428 1429@cindex using @command{grep}, Q&A 1430@cindex FAQ about @command{grep} usage 1431Here are some common questions and answers about @command{grep} usage. 1432 1433@enumerate 1434 1435@item 1436How can I list just the names of matching files? 1437 1438@example 1439grep -l 'main' *.c 1440@end example 1441 1442@noindent 1443lists the names of all C files in the current directory whose contents 1444mention @samp{main}. 1445 1446@item 1447How do I search directories recursively? 1448 1449@example 1450grep -r 'hello' /home/gigi 1451@end example 1452 1453@noindent 1454searches for @samp{hello} in all files 1455under the @file{/home/gigi} directory. 1456For more control over which files are searched, 1457use @command{find}, @command{grep}, and @command{xargs}. 1458For example, the following command searches only C files: 1459 1460@example 1461find /home/gigi -name '*.c' -print0 | xargs -0r grep -H 'hello' 1462@end example 1463 1464This differs from the command: 1465 1466@example 1467grep -rH 'hello' *.c 1468@end example 1469 1470which merely looks for @samp{hello} in all files in the current 1471directory whose names end in @samp{.c}. 1472Here the @option{-r} is 1473probably unnecessary, as recursion occurs only in the unlikely event 1474that one of @samp{.c} files is a directory. 1475The @samp{find ...} command line above is more similar to the command: 1476 1477@example 1478grep -rH --include='*.c' 'hello' /home/gigi 1479@end example 1480 1481@item 1482What if a pattern has a leading @samp{-}? 1483 1484@example 1485grep -e '--cut here--' * 1486@end example 1487 1488@noindent 1489searches for all lines matching @samp{--cut here--}. 1490Without @samp{-e}, 1491@command{grep} would attempt to parse @samp{--cut here--} as a list of 1492options. 1493 1494@item 1495Suppose I want to search for a whole word, not a part of a word? 1496 1497@example 1498grep -w 'hello' * 1499@end example 1500 1501@noindent 1502searches only for instances of @samp{hello} that are entire words; 1503it does not match @samp{Othello}. 1504For more control, use @samp{\<} and 1505@samp{\>} to match the start and end of words. 1506For example: 1507 1508@example 1509grep 'hello\>' * 1510@end example 1511 1512@noindent 1513searches only for words ending in @samp{hello}, so it matches the word 1514@samp{Othello}. 1515 1516@item 1517How do I output context around the matching lines? 1518 1519@example 1520grep -C 2 'hello' * 1521@end example 1522 1523@noindent 1524prints two lines of context around each matching line. 1525 1526@item 1527How do I force @command{grep} to print the name of the file? 1528 1529Append @file{/dev/null}: 1530 1531@example 1532grep 'eli' /etc/passwd /dev/null 1533@end example 1534 1535gets you: 1536 1537@example 1538/etc/passwd:eli:x:2098:1000:Eli Smith:/home/eli:/bin/bash 1539@end example 1540 1541Alternatively, use @samp{-H}, which is a @sc{gnu} extension: 1542 1543@example 1544grep -H 'eli' /etc/passwd 1545@end example 1546 1547@item 1548Why do people use strange regular expressions on @command{ps} output? 1549 1550@example 1551ps -ef | grep '[c]ron' 1552@end example 1553 1554If the pattern had been written without the square brackets, it would 1555have matched not only the @command{ps} output line for @command{cron}, 1556but also the @command{ps} output line for @command{grep}. 1557Note that on some platforms, 1558@command{ps} limits the output to the width of the screen; 1559@command{grep} does not have any limit on the length of a line 1560except the available memory. 1561 1562@item 1563Why does @command{grep} report ``Binary file matches''? 1564 1565If @command{grep} listed all matching ``lines'' from a binary file, it 1566would probably generate output that is not useful, and it might even 1567muck up your display. 1568So @sc{gnu} @command{grep} suppresses output from 1569files that appear to be binary files. 1570To force @sc{gnu} @command{grep} 1571to output lines even from files that appear to be binary, use the 1572@samp{-a} or @samp{--binary-files=text} option. 1573To eliminate the 1574``Binary file matches'' messages, use the @samp{-I} or 1575@samp{--binary-files=without-match} option. 1576 1577@item 1578Why doesn't @samp{grep -lv} print non-matching file names? 1579 1580@samp{grep -lv} lists the names of all files containing one or more 1581lines that do not match. 1582To list the names of all files that contain no 1583matching lines, use the @samp{-L} or @samp{--files-without-match} 1584option. 1585 1586@item 1587I can do @sc{or} with @samp{|}, but what about @sc{and}? 1588 1589@example 1590grep 'paul' /etc/motd | grep 'franc,ois' 1591@end example 1592 1593@noindent 1594finds all lines that contain both @samp{paul} and @samp{franc,ois}. 1595 1596@item 1597How can I search in both standard input and in files? 1598 1599Use the special file name @samp{-}: 1600 1601@example 1602cat /etc/passwd | grep 'alain' - /etc/motd 1603@end example 1604 1605@item 1606@cindex palindromes 1607How to express palindromes in a regular expression? 1608 1609It can be done by using back-references; 1610for example, 1611a palindrome of 4 characters can be written with a BRE: 1612 1613@example 1614grep -w -e '\(.\)\(.\).\2\1' file 1615@end example 1616 1617It matches the word "radar" or "civic". 1618 1619Guglielmo Bondioni proposed a single RE 1620that finds all palindromes up to 19 characters long 1621using @w{9 subexpressions} and @w{9 back-references}: 1622 1623@smallexample 1624grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file 1625@end smallexample 1626 1627Note this is done by using @sc{gnu} ERE extensions; 1628it might not be portable to other implementations of @command{grep}. 1629 1630@item 1631Why is this back-reference failing? 1632 1633@example 1634echo 'ba' | grep -E '(a)\1|b\1' 1635@end example 1636 1637This gives no output, because the first alternate @samp{(a)\1} does not match, 1638as there is no @samp{aa} in the input, so the @samp{\1} in the second alternate 1639has nothing to refer back to, meaning it will never match anything. 1640(The second alternate in this example can only match 1641if the first alternate has matched---making the second one superfluous.) 1642 1643@item 1644How can I match across lines? 1645 1646Standard grep cannot do this, as it is fundamentally line-based. 1647Therefore, merely using the @code{[:space:]} character class does not 1648match newlines in the way you might expect. However, if your grep is 1649compiled with Perl patterns enabled, the Perl @samp{s} 1650modifier (which makes @code{.} match newlines) can be used: 1651 1652@example 1653printf 'foo\nbar\n' | grep -P '(?s)foo.*?bar' 1654@end example 1655 1656With the GNU @command{grep} option @code{-z} (@pxref{File and 1657Directory Selection}), the input is terminated by null bytes. Thus, 1658you can match newlines in the input, but the output will be the whole 1659file, so this is really only useful to determine if the pattern is 1660present: 1661 1662@example 1663printf 'foo\nbar\n' | grep -z -q 'foo[[:space:]]\+bar' 1664@end example 1665 1666Failing either of those options, you need to transform the input 1667before giving it to @command{grep}, or turn to @command{awk}, 1668@command{sed}, @command{perl}, or many other utilities that are 1669designed to operate across lines. 1670 1671@item 1672What do @command{grep}, @command{fgrep}, and @command{egrep} stand for? 1673 1674The name @command{grep} comes from the way line editing was done on Unix. 1675For example, 1676@command{ed} uses the following syntax 1677to print a list of matching lines on the screen: 1678 1679@example 1680global/regular expression/print 1681g/re/p 1682@end example 1683 1684@command{fgrep} stands for Fixed @command{grep}; 1685@command{egrep} stands for Extended @command{grep}. 1686 1687@end enumerate 1688 1689 1690@node Reporting Bugs 1691@chapter Reporting bugs 1692 1693@cindex bugs, reporting 1694Email bug reports to @email{bug-grep@@gnu.org}, 1695a mailing list whose web page is 1696@url{http://lists.gnu.org/mailman/listinfo/bug-grep}. 1697The Savannah bug tracker for @command{grep} is located at 1698@url{http://savannah.gnu.org/bugs/?group=grep}. 1699 1700@section Known Bugs 1701@cindex Bugs, known 1702 1703Large repetition counts in the @samp{@{n,m@}} construct may cause 1704@command{grep} to use lots of memory. 1705In addition, certain other 1706obscure regular expressions require exponential time and 1707space, and may cause @command{grep} to run out of memory. 1708 1709Back-references are very slow, and may require exponential time. 1710 1711 1712@node Copying 1713@chapter Copying 1714@cindex copying 1715 1716GNU grep is licensed under the GNU GPL, which makes it @dfn{free 1717software}. 1718 1719The ``free'' in ``free software'' refers to liberty, not price. As 1720some GNU project advocates like to point out, think of ``free speech'' 1721rather than ``free beer''. In short, you have the right (freedom) to 1722run and change grep and distribute it to other people, and---if you 1723want---charge money for doing either. The important restriction is 1724that you have to grant your recipients the same rights and impose the 1725same restrictions. 1726 1727This general method of licensing software is sometimes called 1728@dfn{open source}. The GNU project prefers the term ``free software'' 1729for reasons outlined at 1730@url{http://www.gnu.org/philosophy/open-source-misses-the-point.html}. 1731 1732This manual is free documentation in the same sense. The 1733documentation license is included below. The license for the program 1734is available with the source code, or at 1735@url{http://www.gnu.org/licenses/gpl.html}. 1736 1737@menu 1738* GNU Free Documentation License:: 1739@end menu 1740 1741@node GNU Free Documentation License 1742@section GNU Free Documentation License 1743 1744@include fdl.texi 1745 1746 1747@node Index 1748@unnumbered Index 1749 1750@printindex cp 1751 1752@bye 1753