2 .\" $FreeBSD: src/gnu/usr.bin/grep/grep.1,v 1.16.2.3 2001/11/27 08:25:45 ru Exp $
3 .\" $DragonFly: src/gnu/usr.bin/grep/grep.1,v 1.2 2003/06/17 04:25:45 dillon Exp $
7 . if \w'\(lq' .ds lq "\(lq
11 . if \w'\(rq' .ds rq "\(rq
17 .Id $Id: grep.1,v 1.9 2000/01/26 03:42:16 alainm Exp $
18 .TH GREP 1 \*(Dt "GNU Project"
20 grep, egrep, fgrep, zgrep \- print lines matching a pattern
37 searches the named input
39 (or standard input if no files are named, or
43 for lines containing a match to the given
47 prints the matching lines.
49 In addition, two variant programs
65 .BI \-A " NUM" "\fR,\fP \-\^\-after-context=" NUM
68 lines of trailing context after matching lines.
70 .BR \-a ", " \-\^\-text
71 Process a binary file as if it were text; this is equivalent to the
72 .B \-\^\-binary-files=text
75 .BI \-B " NUM" "\fR,\fP \-\^\-before-context=" NUM
78 lines of leading context before matching lines.
80 \fB\-C\fP [\fINUM\fP], \fB\-\fP\fINUM\fP, \fB\-\^\-context\fP[\fB=\fP\fINUM\fP]
83 lines (default 2) of output context.
85 .BR \-b ", " \-\^\-byte-offset
86 Print the byte offset within the input file before
89 .BI \-\^\-binary-files= TYPE
90 If the first few bytes of a file indicate that the file contains binary
91 data, assume that the file is of type
99 normally outputs either
100 a one-line message saying that a binary file matches, or no message if
107 assumes that a binary file does not match; this is equivalent to the
115 processes a binary file as if it were text; this is equivalent to the
119 .B "grep \-\^\-binary-files=text"
120 might output binary garbage,
121 which can have nasty side effects if the output is a terminal and if the
122 terminal driver interprets some of it as commands.
124 .BR \-c ", " \-\^\-count
125 Suppress normal output; instead print a count of
126 matching lines for each input file.
128 .BR \-v ", " \-\^\-invert-match
129 option (see below), count non-matching lines.
131 .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
132 If an input file is a directory, use
134 to process it. By default,
138 which means that directories are read just as if they were ordinary files.
143 directories are silently skipped.
149 grep reads all files under each directory, recursively;
150 this is equivalent to the
154 .BR \-E ", " \-\^\-extended-regexp
157 as an extended regular expression (see below).
159 .BI \-e " PATTERN" "\fR,\fP \-\^\-regexp=" PATTERN
162 as the pattern; useful to protect patterns beginning with
165 .BR \-F ", " \-\^\-fixed-strings
168 as a list of fixed strings, separated by newlines,
169 any of which is to be matched.
171 .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
175 The empty file contains zero patterns, and therfore matches nothing.
177 .BR \-G ", " \-\^\-basic-regexp
180 as a basic regular expression (see below). This is the default.
182 .BR \-H ", " \-\^\-with-filename
183 Print the filename for each match.
185 .BR \-h ", " \-\^\-no-filename
186 Suppress the prefixing of filenames on output
187 when multiple files are searched.
190 Output a brief help message.
193 Process a binary file as if it did not contain matching data; this is
195 .B \-\^\-binary-files=without-match
198 .BR \-i ", " \-\^\-ignore-case
199 Ignore case distinctions in both the
203 .BR \-L ", " \-\^\-files-without-match
204 Suppress normal output; instead print the name
205 of each input file from which no output would
206 normally have been printed. The scanning will stop
209 .BR \-l ", " \-\^\-files-with-matches
210 Suppress normal output; instead print
211 the name of each input file from which output
212 would normally have been printed. The scanning will
213 stop on the first match.
218 system call to read input, instead of
221 system call. In some situations,
223 yields better performance. However,
225 can cause undefined behavior (including core dumps)
226 if an input file shrinks while
228 is operating, or if an I/O error occurs.
230 .BR \-n ", " \-\^\-line-number
231 Prefix each line of output with the line number
232 within its input file.
234 .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
235 Quiet; suppress normal output. The scanning will stop
243 .BR \-r ", " \-\^\-recursive
244 Read all files under each directory, recursively;
245 this is equivalent to the
249 .BR \-s ", " \-\^\-no-messages
250 Suppress error messages about nonexistent or unreadable files.
251 Portability note: unlike \s-1GNU\s0
255 did not conform to \s-1POSIX.2\s0, because traditional
261 option behaved like \s-1GNU\s0
265 Shell scripts intended to be portable to traditional
271 and should redirect output to /dev/null instead.
273 .BR \-U ", " \-\^\-binary
274 Treat the file(s) as binary. By default, under MS-DOS and MS-Windows,
276 guesses the file type by looking at the contents of the first 32KB
277 read from the file. If
279 decides the file is a text file, it strips the CR characters from the
280 original file contents (to make regular expressions with
284 work correctly). Specifying
286 overrules this guesswork, causing all files to be read and passed to the
287 matching mechanism verbatim; if the file is a text file with CR/LF
288 pairs at the end of each line, this will cause some regular
290 This option has no effect on platforms other than MS-DOS and
293 .BR \-u ", " \-\^\-unix-byte-offsets
294 Report Unix-style byte offsets. This switch causes
296 to report byte offsets as if the file were Unix-style text file, i.e. with
297 CR characters stripped off. This will produce results identical to running
299 on a Unix machine. This option has no effect unless
302 it has no effect on platforms other than MS-DOS and MS-Windows.
304 .BR \-V ", " \-\^\-version
305 Print the version number of
307 to standard error. This version number should
308 be included in all bug reports (see below).
310 .BR \-v ", " \-\^\-invert-match
311 Invert the sense of matching, to select non-matching lines.
313 .BR \-w ", " \-\^\-word-regexp
314 Select only those lines containing matches that form whole words.
315 The test is that the matching substring must either be at the
316 beginning of the line, or preceded by a non-word constituent
317 character. Similarly, it must be either at the end of the line
318 or followed by a non-word constituent character. Word-constituent
319 characters are letters, digits, and the underscore.
321 .BR \-x ", " \-\^\-line-regexp
322 Select only those matches that exactly match the whole line.
329 Output a zero byte (the \s-1ASCII\s0
331 character) instead of the character that normally follows a file name.
333 .B "grep \-l \-\^\-null"
334 outputs a zero byte after each file name instead of the usual newline.
335 This option makes the output unambiguous, even in the presence of file
336 names containing unusual characters like newlines. This option can be
337 used with commands like
338 .BR "find \-print0" ,
343 to process arbitrary file names,
344 even those that contain newline characters.
346 .BR \-Z ", " \-\^\-decompress
347 Decompress the input data before searching.
348 This option is only available if compiled with zlib(3) library.
349 .SH "REGULAR EXPRESSIONS"
350 A regular expression is a pattern that describes a set of strings.
351 Regular expressions are constructed analogously to arithmetic
352 expressions, by using various operators to combine smaller expressions.
355 understands two different versions of regular expression syntax:
356 \*(lqbasic\*(rq and \*(lqextended.\*(rq In
357 .RB "\s-1GNU\s0\ " grep ,
358 there is no difference in available functionality using either syntax.
359 In other implementations, basic regular expressions are less powerful.
360 The following description applies to extended regular expressions;
361 differences for basic regular expressions are summarized afterwards.
363 The fundamental building blocks are the regular expressions that match
364 a single character. Most characters, including all letters and digits,
365 are regular expressions that match themselves. Any metacharacter with
366 special meaning may be quoted by preceding it with a backslash.
368 A list of characters enclosed by
373 character in that list; if the first character of the list
376 then it matches any character
379 For example, the regular expression
381 matches any single digit. A range of characters
382 may be specified by giving the first and last characters, separated
384 Finally, certain named classes of characters are predefined.
385 Their names are self explanatory, and they are
402 except the latter form depends upon the \s-1POSIX\s0 locale and the
403 \s-1ASCII\s0 character encoding, whereas the former is independent
404 of locale and character set.
405 (Note that the brackets in these class names are part of the symbolic
406 names, and must be included in addition to the brackets delimiting
407 the bracket list.) Most metacharacters lose their special meaning
408 inside lists. To include a literal
410 place it first in the list. Similarly, to include a literal
412 place it anywhere but first. Finally, to include a literal
418 matches any single character.
432 are metacharacters that respectively match the empty string at the
433 beginning and end of a line.
438 respectively match the empty string at the beginning and end of a word.
441 matches the empty string at the edge of a word,
444 matches the empty string provided it's
446 at the edge of a word.
448 A regular expression may be followed by one of several repetition operators:
452 The preceding item is optional and matched at most once.
455 The preceding item will be matched zero or more times.
458 The preceding item will be matched one or more times.
461 The preceding item is matched exactly
466 The preceding item is matched
471 The preceding item is matched at least
473 times, but not more than
478 Two regular expressions may be concatenated; the resulting
479 regular expression matches any string formed by concatenating
480 two substrings that respectively match the concatenated
483 Two regular expressions may be joined by the infix operator
485 the resulting regular expression matches any string matching
486 either subexpression.
488 Repetition takes precedence over concatenation, which in turn
489 takes precedence over alternation. A whole subexpression may be
490 enclosed in parentheses to override these precedence rules.
496 is a single digit, matches the substring
497 previously matched by the
499 parenthesized subexpression of the regular expression.
501 In basic regular expressions the metacharacters
509 lose their special meaning; instead use the backslashed
523 metacharacter, and some
525 implementations support
527 instead, so portable scripts should avoid
531 patterns and should use
538 attempts to support traditional usage by assuming that
540 is not special if it would be the start of an invalid interval
541 specification. For example, the shell command
543 searches for the two-character string
545 instead of reporting a syntax error in the regular expression.
546 \s-1POSIX.2\s0 allows this behavior as an extension, but portable scripts
548 .SH "ENVIRONMENT VARIABLES"
551 This variable specifies default options to be placed in front of any
552 explicit options. For example, if
555 .BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,
557 behaves as if the two options
558 .B \-\^\-binary-files=without-match
560 .B \-\^\-directories=skip
561 had been specified before any explicit options.
562 Option specifications are separated by whitespace.
563 A backslash escapes the next character,
564 so it can be used to specify an option containing whitespace or a backslash.
566 \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
567 These variables specify the
569 locale, which determines the language that
572 The locale is determined by the first of these variables that is set.
573 American English is used if none of these environment variables are set,
574 or if the message catalog is not installed, or if
576 was not compiled with national language support (\s-1NLS\s0).
578 \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
579 These variables specify the
581 locale, which determines the type of characters, e.g., which
582 characters are whitespace.
583 The locale is determined by the first of these variables that is set.
584 The \s-1POSIX\s0 locale is used if none of these environment variables
585 are set, or if the locale catalog is not installed, or if
587 was not compiled with national language support (\s-1NLS\s0).
592 behaves as \s-1POSIX.2\s0 requires; otherwise,
594 behaves more like other \s-1GNU\s0 programs.
595 \s-1POSIX.2\s0 requires that options that follow file names must be
596 treated as file names; by default, such options are permuted to the
597 front of the operand list and are treated as options.
598 Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as
599 \*(lqillegal\*(rq, but since they are not really against the law the default
600 is to diagnose them as \*(lqinvalid\*(rq.
602 Normally, exit status is 0 if matches were found,
603 and 1 if no matches were found. (The
605 option inverts the sense of the exit status.)
606 Exit status is 2 if there were syntax errors
607 in the pattern, inaccessible input files, or
611 .BR bug-gnu-utils@gnu.org .
612 Be sure to include the word \*(lqgrep\*(rq somewhere in the
613 \*(lqSubject:\*(rq field.
615 Large repetition counts in the
617 construct may cause grep to use lots of memory.
619 certain other obscure regular expressions require exponential time
620 and space, and may cause
622 to run out of memory.
624 Backreferences are very slow, and may require exponential time.
625 .\" Work around problems with some troff -man implementations.