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