Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / grep / grep.1
1 .\" grep man page
2 .\" $FreeBSD: src/gnu/usr.bin/grep/grep.1,v 1.16.2.3 2001/11/27 08:25:45 ru Exp $
3 .if !\n(.g \{\
4 .       if !\w|\*(lq| \{\
5 .               ds lq ``
6 .               if \w'\(lq' .ds lq "\(lq
7 .       \}
8 .       if !\w|\*(rq| \{\
9 .               ds rq ''
10 .               if \w'\(rq' .ds rq "\(rq
11 .       \}
12 .\}
13 .de Id
14 .ds Dt \\$4
15 ..
16 .Id $Id: grep.1,v 1.9 2000/01/26 03:42:16 alainm Exp $
17 .TH GREP 1 \*(Dt "GNU Project"
18 .SH NAME
19 grep, egrep, fgrep, zgrep \- print lines matching a pattern
20 .SH SYNOPSIS
21 .B grep
22 .RI [ options ]
23 .I PATTERN
24 .RI [ FILE .\|.\|.]
25 .br
26 .B grep
27 .RI [ options ]
28 .RB [ \-e
29 .I PATTERN
30 |
31 .B \-f
32 .IR FILE ]
33 .RI [ FILE .\|.\|.]
34 .SH DESCRIPTION
35 .B grep
36 searches the named input
37 .IR FILE s
38 (or standard input if no files are named, or
39 the file name
40 .B \-
41 is given)
42 for lines containing a match to the given
43 .IR PATTERN .
44 By default,
45 .B grep
46 prints the matching lines.
47 .PP
48 In addition, two variant programs
49 .B egrep
50 and
51 .B fgrep
52 are available.
53 .B egrep
54 is the same as
55 .BR "grep\ \-E" .
56 .B fgrep
57 is the same as
58 .BR "grep\ \-F" .
59 .B zgrep
60 is the same as
61 .BR "grep\ \-Z" .
62 .SH OPTIONS
63 .TP
64 .BI \-A " NUM" "\fR,\fP \-\^\-after-context=" NUM
65 Print
66 .I NUM
67 lines of trailing context after matching lines.
68 .TP
69 .BR \-a ", " \-\^\-text
70 Process a binary file as if it were text; this is equivalent to the
71 .B \-\^\-binary-files=text
72 option.
73 .TP
74 .BI \-B " NUM" "\fR,\fP \-\^\-before-context=" NUM
75 Print
76 .I NUM
77 lines of leading context before matching lines.
78 .TP
79 \fB\-C\fP [\fINUM\fP], \fB\-\fP\fINUM\fP, \fB\-\^\-context\fP[\fB=\fP\fINUM\fP]
80 Print
81 .I NUM
82 lines (default 2) of output context.
83 .TP
84 .BR \-b ", " \-\^\-byte-offset
85 Print the byte offset within the input file before
86 each line of output.
87 .TP
88 .BI \-\^\-binary-files= TYPE
89 If the first few bytes of a file indicate that the file contains binary
90 data, assume that the file is of type
91 .IR TYPE .
92 By default,
93 .I TYPE
94 is
95 .BR binary ,
96 and
97 .B grep
98 normally outputs either
99 a one-line message saying that a binary file matches, or no message if
100 there is no match.
101 If
102 .I TYPE
103 is
104 .BR without-match ,
105 .B grep
106 assumes that a binary file does not match; this is equivalent to the 
107 .B \-I
108 option.
109 If
110 .I TYPE
111 is
112 .BR text ,
113 .B grep
114 processes a binary file as if it were text; this is equivalent to the
115 .B \-a
116 option.
117 .I Warning:
118 .B "grep \-\^\-binary-files=text"
119 might output binary garbage,
120 which can have nasty side effects if the output is a terminal and if the
121 terminal driver interprets some of it as commands.
122 .TP
123 .BR \-c ", " \-\^\-count
124 Suppress normal output; instead print a count of
125 matching lines for each input file.
126 With the
127 .BR \-v ", " \-\^\-invert-match
128 option (see below), count non-matching lines.
129 .TP
130 .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
131 If an input file is a directory, use
132 .I ACTION
133 to process it.  By default,
134 .I ACTION
135 is
136 .BR read ,
137 which means that directories are read just as if they were ordinary files.
138 If
139 .I ACTION
140 is
141 .BR skip ,
142 directories are silently skipped.
143 If
144 .I ACTION
145 is
146 .BR recurse ,
147 .B
148 grep reads all files under each directory, recursively;
149 this is equivalent to the
150 .B \-r
151 option.
152 .TP
153 .BR \-E ", " \-\^\-extended-regexp
154 Interpret
155 .I PATTERN
156 as an extended regular expression (see below).
157 .TP
158 .BI \-e " PATTERN" "\fR,\fP \-\^\-regexp=" PATTERN
159 Use
160 .I PATTERN
161 as the pattern; useful to protect patterns beginning with
162 .BR \- .
163 .TP
164 .BR \-F ", " \-\^\-fixed-strings
165 Interpret
166 .I PATTERN
167 as a list of fixed strings, separated by newlines,
168 any of which is to be matched.
169 .TP
170 .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
171 Obtain patterns from
172 .IR FILE ,
173 one per line.
174 The empty file contains zero patterns, and therfore matches nothing.
175 .TP
176 .BR \-G ", " \-\^\-basic-regexp
177 Interpret
178 .I PATTERN
179 as a basic regular expression (see below).  This is the default.
180 .TP
181 .BR \-H ", " \-\^\-with-filename
182 Print the filename for each match.
183 .TP
184 .BR \-h ", " \-\^\-no-filename
185 Suppress the prefixing of filenames on output
186 when multiple files are searched.
187 .TP
188 .B \-\^\-help
189 Output a brief help message.
190 .TP
191 .BR \-I
192 Process a binary file as if it did not contain matching data; this is
193 equivalent to the
194 .B \-\^\-binary-files=without-match
195 option.
196 .TP
197 .BR \-i ", " \-\^\-ignore-case
198 Ignore case distinctions in both the
199 .I PATTERN
200 and the input files.
201 .TP
202 .BR \-L ", " \-\^\-files-without-match
203 Suppress normal output; instead print the name
204 of each input file from which no output would
205 normally have been printed.  The scanning will stop
206 on the first match.
207 .TP
208 .BR \-l ", " \-\^\-files-with-matches
209 Suppress normal output; instead print
210 the name of each input file from which output
211 would normally have been printed.  The scanning will
212 stop on the first match.
213 .TP
214 .B \-\^\-mmap
215 If possible, use the
216 .BR mmap (2)
217 system call to read input, instead of
218 the default
219 .BR read (2)
220 system call.  In some situations,
221 .B \-\^\-mmap
222 yields better performance.  However,
223 .B \-\^\-mmap
224 can cause undefined behavior (including core dumps)
225 if an input file shrinks while
226 .B grep
227 is operating, or if an I/O error occurs.
228 .TP
229 .BR \-n ", " \-\^\-line-number
230 Prefix each line of output with the line number
231 within its input file.
232 .TP
233 .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
234 Quiet; suppress normal output.  The scanning will stop
235 on the first match.
236 Also see the
237 .B \-s
238 or
239 .B \-\^\-no-messages
240 option below.
241 .TP
242 .BR \-r ", " \-\^\-recursive
243 Read all files under each directory, recursively;
244 this is equivalent to the
245 .B "\-d recurse"
246 option.
247 .TP
248 .BR \-s ", " \-\^\-no-messages
249 Suppress error messages about nonexistent or unreadable files.
250 Portability note: unlike \s-1GNU\s0
251 .BR grep ,
252 traditional
253 .B grep
254 did not conform to \s-1POSIX.2\s0, because traditional
255 .B grep
256 lacked a
257 .B \-q
258 option and its
259 .B \-s
260 option behaved like \s-1GNU\s0
261 .BR grep 's
262 .B \-q
263 option.
264 Shell scripts intended to be portable to traditional
265 .B grep
266 should avoid both
267 .B \-q
268 and
269 .B \-s
270 and should redirect output to /dev/null instead.
271 .TP
272 .BR \-U ", " \-\^\-binary
273 Treat the file(s) as binary.  By default, under MS-DOS and MS-Windows,
274 .BR grep
275 guesses the file type by looking at the contents of the first 32KB
276 read from the file.  If
277 .BR grep
278 decides the file is a text file, it strips the CR characters from the
279 original file contents (to make regular expressions with
280 .B ^
281 and
282 .B $
283 work correctly).  Specifying
284 .B \-U
285 overrules this guesswork, causing all files to be read and passed to the
286 matching mechanism verbatim; if the file is a text file with CR/LF
287 pairs at the end of each line, this will cause some regular
288 expressions to fail.
289 This option has no effect on platforms other than MS-DOS and
290 MS-Windows.
291 .TP
292 .BR \-u ", " \-\^\-unix-byte-offsets
293 Report Unix-style byte offsets.  This switch causes
294 .B grep
295 to report byte offsets as if the file were Unix-style text file, i.e. with
296 CR characters stripped off.  This will produce results identical to running
297 .B grep
298 on a Unix machine.  This option has no effect unless
299 .B \-b
300 option is also used;
301 it has no effect on platforms other than MS-DOS and MS-Windows.
302 .TP
303 .BR \-V ", " \-\^\-version
304 Print the version number of
305 .B grep
306 to standard error.  This version number should
307 be included in all bug reports (see below).
308 .TP
309 .BR \-v ", " \-\^\-invert-match
310 Invert the sense of matching, to select non-matching lines.
311 .TP
312 .BR \-w ", " \-\^\-word-regexp
313 Select only those lines containing matches that form whole words.
314 The test is that the matching substring must either be at the
315 beginning of the line, or preceded by a non-word constituent
316 character.  Similarly, it must be either at the end of the line
317 or followed by a non-word constituent character.  Word-constituent
318 characters are letters, digits, and the underscore.
319 .TP
320 .BR \-x ", " \-\^\-line-regexp
321 Select only those matches that exactly match the whole line.
322 .TP
323 .B \-y
324 Obsolete synonym for
325 .BR \-i .
326 .TP
327 .B \-\^\-null
328 Output a zero byte (the \s-1ASCII\s0
329 .B NUL
330 character) instead of the character that normally follows a file name.
331 For example,
332 .B "grep \-l \-\^\-null"
333 outputs a zero byte after each file name instead of the usual newline.
334 This option makes the output unambiguous, even in the presence of file
335 names containing unusual characters like newlines.  This option can be
336 used with commands like
337 .BR "find \-print0" ,
338 .BR "perl \-0" ,
339 .BR "sort \-z" ,
340 and
341 .B "xargs \-0"
342 to process arbitrary file names,
343 even those that contain newline characters.
344 .TP
345 .BR \-Z ", " \-\^\-decompress
346 Decompress the input data before searching.
347 This option is only available if compiled with zlib(3) library.
348 .SH "REGULAR EXPRESSIONS"
349 A regular expression is a pattern that describes a set of strings.
350 Regular expressions are constructed analogously to arithmetic
351 expressions, by using various operators to combine smaller expressions.
352 .PP
353 .B grep
354 understands two different versions of regular expression syntax:
355 \*(lqbasic\*(rq and \*(lqextended.\*(rq  In
356 .RB "\s-1GNU\s0\ " grep ,
357 there is no difference in available functionality using either syntax.
358 In other implementations, basic regular expressions are less powerful.
359 The following description applies to extended regular expressions;
360 differences for basic regular expressions are summarized afterwards.
361 .PP
362 The fundamental building blocks are the regular expressions that match
363 a single character.  Most characters, including all letters and digits,
364 are regular expressions that match themselves.  Any metacharacter with
365 special meaning may be quoted by preceding it with a backslash.
366 .PP
367 A list of characters enclosed by
368 .B [
369 and
370 .B ]
371 matches any single
372 character in that list; if the first character of the list
373 is the caret
374 .B ^
375 then it matches any character
376 .I not
377 in the list.
378 For example, the regular expression
379 .B [0123456789]
380 matches any single digit.  A range of characters
381 may be specified by giving the first and last characters, separated
382 by a hyphen.
383 Finally, certain named classes of characters are predefined.
384 Their names are self explanatory, and they are
385 .BR [:alnum:] ,
386 .BR [:alpha:] ,
387 .BR [:cntrl:] ,
388 .BR [:digit:] ,
389 .BR [:graph:] ,
390 .BR [:lower:] ,
391 .BR [:print:] ,
392 .BR [:punct:] ,
393 .BR [:space:] ,
394 .BR [:upper:] ,
395 and
396 .BR [:xdigit:].
397 For example,
398 .B [[:alnum:]]
399 means
400 .BR [0-9A-Za-z] ,
401 except the latter form depends upon the \s-1POSIX\s0 locale and the
402 \s-1ASCII\s0 character encoding, whereas the former is independent
403 of locale and character set.
404 (Note that the brackets in these class names are part of the symbolic
405 names, and must be included in addition to the brackets delimiting
406 the bracket list.)  Most metacharacters lose their special meaning
407 inside lists.  To include a literal
408 .B ]
409 place it first in the list.  Similarly, to include a literal
410 .B ^
411 place it anywhere but first.  Finally, to include a literal
412 .B \-
413 place it last.
414 .PP
415 The period
416 .B .
417 matches any single character.
418 The symbol
419 .B \ew
420 is a synonym for
421 .B [[:alnum:]]
422 and
423 .B \eW
424 is a synonym for
425 .BR [^[:alnum]] .
426 .PP
427 The caret
428 .B ^
429 and the dollar sign
430 .B $
431 are metacharacters that respectively match the empty string at the
432 beginning and end of a line.
433 The symbols
434 .B \e<
435 and
436 .B \e>
437 respectively match the empty string at the beginning and end of a word.
438 The symbol
439 .B \eb
440 matches the empty string at the edge of a word,
441 and
442 .B \eB
443 matches the empty string provided it's
444 .I not
445 at the edge of a word.
446 .PP
447 A regular expression may be followed by one of several repetition operators:
448 .PD 0
449 .TP
450 .B ?
451 The preceding item is optional and matched at most once.
452 .TP
453 .B *
454 The preceding item will be matched zero or more times.
455 .TP
456 .B +
457 The preceding item will be matched one or more times.
458 .TP
459 .BI { n }
460 The preceding item is matched exactly
461 .I n
462 times.
463 .TP
464 .BI { n ,}
465 The preceding item is matched
466 .I n
467 or more times.
468 .TP
469 .BI { n , m }
470 The preceding item is matched at least
471 .I n
472 times, but not more than
473 .I m
474 times.
475 .PD
476 .PP
477 Two regular expressions may be concatenated; the resulting
478 regular expression matches any string formed by concatenating
479 two substrings that respectively match the concatenated
480 subexpressions.
481 .PP
482 Two regular expressions may be joined by the infix operator
483 .BR | ;
484 the resulting regular expression matches any string matching
485 either subexpression.
486 .PP
487 Repetition takes precedence over concatenation, which in turn
488 takes precedence over alternation.  A whole subexpression may be
489 enclosed in parentheses to override these precedence rules.
490 .PP
491 The backreference
492 .BI \e n\c
493 \&, where
494 .I n
495 is a single digit, matches the substring
496 previously matched by the
497 .IR n th
498 parenthesized subexpression of the regular expression.
499 .PP
500 In basic regular expressions the metacharacters
501 .BR ? ,
502 .BR + ,
503 .BR { ,
504 .BR | ,
505 .BR ( ,
506 and
507 .BR )
508 lose their special meaning; instead use the backslashed
509 versions
510 .BR \e? ,
511 .BR \e+ ,
512 .BR \e{ ,
513 .BR \e| ,
514 .BR \e( ,
515 and
516 .BR \e) .
517 .PP
518 Traditional
519 .B egrep
520 did not support the
521 .B {
522 metacharacter, and some
523 .B egrep
524 implementations support
525 .B \e{
526 instead, so portable scripts should avoid
527 .B {
528 in
529 .B egrep
530 patterns and should use
531 .B [{]
532 to match a literal
533 .BR { .
534 .PP
535 \s-1GNU\s0
536 .B egrep
537 attempts to support traditional usage by assuming that
538 .B {
539 is not special if it would be the start of an invalid interval
540 specification.  For example, the shell command
541 .B "egrep '{1'"
542 searches for the two-character string
543 .B {1
544 instead of reporting a syntax error in the regular expression.
545 \s-1POSIX.2\s0 allows this behavior as an extension, but portable scripts
546 should avoid it.
547 .SH "ENVIRONMENT VARIABLES"
548 .TP
549 .B GREP_OPTIONS
550 This variable specifies default options to be placed in front of any
551 explicit options.  For example, if
552 .B GREP_OPTIONS
553 is
554 .BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,
555 .B grep
556 behaves as if the two options
557 .B \-\^\-binary-files=without-match
558 and
559 .B \-\^\-directories=skip
560 had been specified before any explicit options.
561 Option specifications are separated by whitespace.
562 A backslash escapes the next character,
563 so it can be used to specify an option containing whitespace or a backslash.
564 .TP
565 \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
566 These variables specify the
567 .B LC_MESSAGES
568 locale, which determines the language that
569 .B grep
570 uses for messages.
571 The locale is determined by the first of these variables that is set.
572 American English is used if none of these environment variables are set,
573 or if the message catalog is not installed, or if
574 .B grep
575 was not compiled with national language support (\s-1NLS\s0).
576 .TP
577 \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
578 These variables specify the
579 .B LC_CTYPE
580 locale, which determines the type of characters, e.g., which
581 characters are whitespace.
582 The locale is determined by the first of these variables that is set.
583 The \s-1POSIX\s0 locale is used if none of these environment variables
584 are set, or if the locale catalog is not installed, or if
585 .B grep
586 was not compiled with national language support (\s-1NLS\s0).
587 .TP
588 .B POSIXLY_CORRECT
589 If set,
590 .B grep
591 behaves as \s-1POSIX.2\s0 requires; otherwise,
592 .B grep
593 behaves more like other \s-1GNU\s0 programs.
594 \s-1POSIX.2\s0 requires that options that follow file names must be
595 treated as file names; by default, such options are permuted to the
596 front of the operand list and are treated as options.
597 Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as
598 \*(lqillegal\*(rq, but since they are not really against the law the default
599 is to diagnose them as \*(lqinvalid\*(rq.
600 .SH DIAGNOSTICS
601 Normally, exit status is 0 if matches were found,
602 and 1 if no matches were found.  (The
603 .B \-v
604 option inverts the sense of the exit status.)
605 Exit status is 2 if there were syntax errors
606 in the pattern, inaccessible input files, or
607 other system errors.
608 .SH BUGS
609 Email bug reports to
610 .BR bug-gnu-utils@gnu.org .
611 Be sure to include the word \*(lqgrep\*(rq somewhere in the
612 \*(lqSubject:\*(rq field.
613 .PP
614 Large repetition counts in the
615 .BI { m , n }
616 construct may cause grep to use lots of memory.
617 In addition,
618 certain other obscure regular expressions require exponential time
619 and space, and may cause
620 .B grep
621 to run out of memory.
622 .PP
623 Backreferences are very slow, and may require exponential time.
624 .\" Work around problems with some troff -man implementations.
625 .br