]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/grep/grep.1
This commit was generated by cvs2svn to compensate for changes in r156803,
[FreeBSD/FreeBSD.git] / gnu / usr.bin / grep / grep.1
1 .\" grep man page
2 .\" $FreeBSD$
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.23 2002/01/22 13:20:04 bero Exp $
17 .TH GREP 1 \*(Dt "GNU Project"
18 .SH NAME
19 grep, egrep, fgrep, zgrep, zegrep, zfgrep,
20 bzgrep, bzegrep, bzfgrep \- 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 .B zegrep
64 is the same as
65 .BR "grep\ \-EZ" .
66 .B zfgrep
67 is the same as
68 .BR "grep\ \-FZ" .
69 .SH OPTIONS
70 .TP
71 .BI \-A " NUM" "\fR,\fP \-\^\-after-context=" NUM
72 Print
73 .I NUM
74 lines of trailing context after matching lines.
75 Places a line containing
76 .B \-\^\-
77 between contiguous groups of matches.
78 .TP
79 .BR \-a ", " \-\^\-text
80 Process a binary file as if it were text; this is equivalent to the
81 .B \-\^\-binary-files=text
82 option.
83 .TP
84 .BI \-B " NUM" "\fR,\fP \-\^\-before-context=" NUM
85 Print
86 .I NUM
87 lines of leading context before matching lines.
88 Places a line containing
89 .B \-\^\-
90 between contiguous groups of matches.
91 .TP
92 .BI \-C " NUM" "\fR,\fP \-\^\-context=" NUM
93 Print
94 .I NUM
95 lines of output context.
96 Places a line containing
97 .B \-\^\-
98 between contiguous groups of matches.
99 .TP
100 .BR \-b ", " \-\^\-byte-offset
101 Print the byte offset within the input file before
102 each line of output.
103 .TP
104 .BI \-\^\-binary-files= TYPE
105 If the first few bytes of a file indicate that the file contains binary
106 data, assume that the file is of type
107 .IR TYPE .
108 By default,
109 .I TYPE
110 is
111 .BR binary ,
112 and
113 .B grep
114 normally outputs either
115 a one-line message saying that a binary file matches, or no message if
116 there is no match.
117 If
118 .I TYPE
119 is
120 .BR without-match ,
121 .B grep
122 assumes that a binary file does not match; this is equivalent to the
123 .B \-I
124 option.
125 If
126 .I TYPE
127 is
128 .BR text ,
129 .B grep
130 processes a binary file as if it were text; this is equivalent to the
131 .B \-a
132 option.
133 .I Warning:
134 .B "grep \-\^\-binary-files=text"
135 might output binary garbage,
136 which can have nasty side effects if the output is a terminal and if the
137 terminal driver interprets some of it as commands.
138 .TP
139 .BI \-\^\-colour[=\fIWHEN\fR] ", " \-\^\-color[=\fIWHEN\fR]
140 Surround the matching string with the marker find in
141 .B GREP_COLOR
142 environment variable. WHEN may be `never', `always', or `auto'
143 .TP
144 .BR \-c ", " \-\^\-count
145 Suppress normal output; instead print a count of
146 matching lines for each input file.
147 With the
148 .BR \-v ", " \-\^\-invert-match
149 option (see below), count non-matching lines.
150 .TP
151 .BI \-D " ACTION" "\fR,\fP \-\^\-devices=" ACTION
152 If an input file is a device, FIFO or socket, use
153 .I ACTION
154 to process it.  By default,
155 .I ACTION
156 is
157 .BR read ,
158 which means that devices are read just as if they were ordinary files.
159 If
160 .I ACTION
161 is
162 .BR skip ,
163 devices are silently skipped.
164 .TP
165 .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
166 If an input file is a directory, use
167 .I ACTION
168 to process it.  By default,
169 .I ACTION
170 is
171 .BR read ,
172 which means that directories are read just as if they were ordinary files.
173 If
174 .I ACTION
175 is
176 .BR skip ,
177 directories are silently skipped.
178 If
179 .I ACTION
180 is
181 .BR recurse ,
182 .B grep
183 reads all files under each directory, recursively;
184 this is equivalent to the
185 .B \-r
186 option.
187 .TP
188 .BR \-E ", " \-\^\-extended-regexp
189 Interpret
190 .I PATTERN
191 as an extended regular expression (see below).
192 .TP
193 .BI \-e " PATTERN" "\fR,\fP \-\^\-regexp=" PATTERN
194 Use
195 .I PATTERN
196 as the pattern; useful to protect patterns beginning with
197 .BR \- .
198 .TP
199 .BR \-F ", " \-\^\-fixed-strings
200 Interpret
201 .I PATTERN
202 as a list of fixed strings, separated by newlines,
203 any of which is to be matched.
204 .TP
205 .BR \-P ", " \-\^\-perl-regexp
206 Interpret
207 .I PATTERN
208 as a Perl regular expression.
209 .TP
210 .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
211 Obtain patterns from
212 .IR FILE ,
213 one per line.
214 The empty file contains zero patterns, and therefore matches nothing.
215 .TP
216 .BR \-G ", " \-\^\-basic-regexp
217 Interpret
218 .I PATTERN
219 as a basic regular expression (see below).  This is the default.
220 .TP
221 .BR \-H ", " \-\^\-with-filename
222 Print the filename for each match.
223 .TP
224 .BR \-h ", " \-\^\-no-filename
225 Suppress the prefixing of filenames on output
226 when multiple files are searched.
227 .TP
228 .B \-\^\-help
229 Output a brief help message.
230 .TP
231 .BR \-I
232 Process a binary file as if it did not contain matching data; this is
233 equivalent to the
234 .B \-\^\-binary-files=without-match
235 option.
236 .TP
237 .BR \-i ", " \-\^\-ignore-case
238 Ignore case distinctions in both the
239 .I PATTERN
240 and the input files.
241 .TP
242 .BR \-L ", " \-\^\-files-without-match
243 Suppress normal output; instead print the name
244 of each input file from which no output would
245 normally have been printed.  The scanning will stop
246 on the first match.
247 .TP
248 .BR \-l ", " \-\^\-files-with-matches
249 Suppress normal output; instead print
250 the name of each input file from which output
251 would normally have been printed.  The scanning will
252 stop on the first match.
253 .TP
254 .BI \-m " NUM" "\fR,\fP \-\^\-max-count=" NUM
255 Stop reading a file after
256 .I NUM
257 matching lines.  If the input is standard input from a regular file,
258 and
259 .I NUM
260 matching lines are output,
261 .B grep
262 ensures that the standard input is positioned to just after the last
263 matching line before exiting, regardless of the presence of trailing
264 context lines.  This enables a calling process to resume a search.
265 When
266 .B grep
267 stops after
268 .I NUM
269 matching lines, it outputs any trailing context lines.  When the
270 .B \-c
271 or
272 .B \-\^\-count
273 option is also used,
274 .B grep
275 does not output a count greater than
276 .IR NUM .
277 When the
278 .B \-v
279 or
280 .B \-\^\-invert-match
281 option is also used,
282 .B grep
283 stops after outputting
284 .I NUM
285 non-matching lines.
286 .TP
287 .B \-\^\-mmap
288 If possible, use the
289 .BR mmap (2)
290 system call to read input, instead of
291 the default
292 .BR read (2)
293 system call.  In some situations,
294 .B \-\^\-mmap
295 yields better performance.  However,
296 .B \-\^\-mmap
297 can cause undefined behavior (including core dumps)
298 if an input file shrinks while
299 .B grep
300 is operating, or if an I/O error occurs.
301 .TP
302 .BR \-n ", " \-\^\-line-number
303 Prefix each line of output with the line number
304 within its input file.
305 .TP
306 .BR \-o ", " \-\^\-only-matching
307 Show only the part of a matching line that matches
308 .I PATTERN.
309 .TP
310 .BI \-\^\-label= LABEL
311 Displays input actually coming from standard input as input coming from file
312 .I LABEL.
313 This is especially useful for tools like zgrep, e.g.
314 .B "gzip -cd foo.gz |grep --label=foo something"
315 .TP
316 .BR \-\^\-line-buffered
317 Flush output on every line.
318 Note that this incurs a performance penalty.
319 .TP
320 .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
321 Quiet; do not write anything to standard output.
322 Exit immediately with zero status if any match is found,
323 even if an error was detected.
324 Also see the
325 .B \-s
326 or
327 .B \-\^\-no-messages
328 option.
329 .TP
330 .BR \-R ", " \-r ", " \-\^\-recursive
331 Read all files under each directory, recursively;
332 this is equivalent to the
333 .B "\-d recurse"
334 option.
335 .TP
336 .BR "\fR \fP \-\^\-include=" PATTERN
337 Recurse in directories only searching file matching
338 .I PATTERN.
339 .TP
340 .BR "\fR \fP \-\^\-exclude=" PATTERN
341 Recurse in directories skip file matching
342 .I PATTERN.
343 .TP
344 .BR \-s ", " \-\^\-no-messages
345 Suppress error messages about nonexistent or unreadable files.
346 Portability note: unlike \s-1GNU\s0
347 .BR grep ,
348 traditional
349 .B grep
350 did not conform to \s-1POSIX.2\s0, because traditional
351 .B grep
352 lacked a
353 .B \-q
354 option and its
355 .B \-s
356 option behaved like \s-1GNU\s0
357 .BR grep 's
358 .B \-q
359 option.
360 Shell scripts intended to be portable to traditional
361 .B grep
362 should avoid both
363 .B \-q
364 and
365 .B \-s
366 and should redirect output to /dev/null instead.
367 .TP
368 .BR \-U ", " \-\^\-binary
369 Treat the file(s) as binary.  By default, under MS-DOS and MS-Windows,
370 .BR grep
371 guesses the file type by looking at the contents of the first 32KB
372 read from the file.  If
373 .BR grep
374 decides the file is a text file, it strips the CR characters from the
375 original file contents (to make regular expressions with
376 .B ^
377 and
378 .B $
379 work correctly).  Specifying
380 .B \-U
381 overrules this guesswork, causing all files to be read and passed to the
382 matching mechanism verbatim; if the file is a text file with CR/LF
383 pairs at the end of each line, this will cause some regular
384 expressions to fail.
385 This option has no effect on platforms other than MS-DOS and
386 MS-Windows.
387 .TP
388 .BR \-u ", " \-\^\-unix-byte-offsets
389 Report Unix-style byte offsets.  This switch causes
390 .B grep
391 to report byte offsets as if the file were Unix-style text file, i.e. with
392 CR characters stripped off.  This will produce results identical to running
393 .B grep
394 on a Unix machine.  This option has no effect unless
395 .B \-b
396 option is also used;
397 it has no effect on platforms other than MS-DOS and MS-Windows.
398 .TP
399 .BR \-V ", " \-\^\-version
400 Print the version number of
401 .B grep
402 to standard error.  This version number should
403 be included in all bug reports (see below).
404 .TP
405 .BR \-v ", " \-\^\-invert-match
406 Invert the sense of matching, to select non-matching lines.
407 .TP
408 .BR \-w ", " \-\^\-word-regexp
409 Select only those lines containing matches that form whole words.
410 The test is that the matching substring must either be at the
411 beginning of the line, or preceded by a non-word constituent
412 character.  Similarly, it must be either at the end of the line
413 or followed by a non-word constituent character.  Word-constituent
414 characters are letters, digits, and the underscore.
415 .TP
416 .BR \-x ", " \-\^\-line-regexp
417 Select only those matches that exactly match the whole line.
418 .TP
419 .B \-y
420 Obsolete synonym for
421 .BR \-i .
422 .TP
423 .B \-\^\-null
424 Output a zero byte (the \s-1ASCII\s0
425 .B NUL
426 character) instead of the character that normally follows a file name.
427 For example,
428 .B "grep \-l \-\^\-null"
429 outputs a zero byte after each file name instead of the usual newline.
430 This option makes the output unambiguous, even in the presence of file
431 names containing unusual characters like newlines.  This option can be
432 used with commands like
433 .BR "find \-print0" ,
434 .BR "perl \-0" ,
435 .BR "sort \-z" ,
436 and
437 .B "xargs \-0"
438 to process arbitrary file names,
439 even those that contain newline characters.
440 .TP
441 .BR \-Z ", " \-\^\-decompress
442 Decompress the input data before searching.
443 This option is only available if compiled with
444 .BR zlib (3)
445 library.
446 .TP
447 .BR \-J ", " \-\^\-bz2decompress
448 Decompress the
449 .BR bzip2 (1)
450 compressed input data before searching.
451 .SH "REGULAR EXPRESSIONS"
452 A regular expression is a pattern that describes a set of strings.
453 Regular expressions are constructed analogously to arithmetic
454 expressions, by using various operators to combine smaller expressions.
455 .PP
456 .B grep
457 understands two different versions of regular expression syntax:
458 \*(lqbasic\*(rq and \*(lqextended.\*(rq  In
459 .RB "\s-1GNU\s0\ " grep ,
460 there is no difference in available functionality using either syntax.
461 In other implementations, basic regular expressions are less powerful.
462 The following description applies to extended regular expressions;
463 differences for basic regular expressions are summarized afterwards.
464 .PP
465 The fundamental building blocks are the regular expressions that match
466 a single character.  Most characters, including all letters and digits,
467 are regular expressions that match themselves.  Any metacharacter with
468 special meaning may be quoted by preceding it with a backslash.
469 .PP
470 A
471 .I "bracket expression"
472 is a list of characters enclosed by
473 .B [
474 and
475 .BR ] .
476 It matches any single
477 character in that list; if the first character of the list
478 is the caret
479 .B ^
480 then it matches any character
481 .I not
482 in the list.
483 For example, the regular expression
484 .B [0123456789]
485 matches any single digit.
486 .PP
487 Within a bracket expression, a
488 .I "range expression"
489 consists of two characters separated by a hyphen.
490 It matches any single character that sorts between the two characters,
491 inclusive, using the locale's collating sequence and character set.
492 For example, in the default C locale,
493 .B [a\-d]
494 is equivalent to
495 .BR [abcd] .
496 Many locales sort characters in dictionary order, and in these locales
497 .B [a\-d]
498 is typically not equivalent to
499 .BR [abcd] ;
500 it might be equivalent to
501 .BR [aBbCcDd] ,
502 for example.
503 To obtain the traditional interpretation of bracket expressions,
504 you can use the C locale by setting the
505 .B LC_ALL
506 environment variable to the value
507 .BR C .
508 .PP
509 Finally, certain named classes of characters are predefined within
510 bracket expressions, as follows.
511 Their names are self explanatory, and they are
512 .BR [:alnum:] ,
513 .BR [:alpha:] ,
514 .BR [:blank:] ,
515 .BR [:cntrl:] ,
516 .BR [:digit:] ,
517 .BR [:graph:] ,
518 .BR [:lower:] ,
519 .BR [:print:] ,
520 .BR [:punct:] ,
521 .BR [:space:] ,
522 .BR [:upper:] ,
523 and
524 .BR [:xdigit:].
525 For example,
526 .B [[:alnum:]]
527 means
528 .BR [0\-9A\-Za\-z] ,
529 except the latter form depends upon the C locale and the
530 \s-1ASCII\s0 character encoding, whereas the former is independent
531 of locale and character set.
532 (Note that the brackets in these class names are part of the symbolic
533 names, and must be included in addition to the brackets delimiting
534 the bracket list.)  Most metacharacters lose their special meaning
535 inside lists.  To include a literal
536 .B ]
537 place it first in the list.  Similarly, to include a literal
538 .B ^
539 place it anywhere but first.  Finally, to include a literal
540 .B \-
541 place it last.
542 .PP
543 The period
544 .B .
545 matches any single character.
546 The symbol
547 .B \ew
548 is a synonym for
549 .B [[:alnum:]]
550 and
551 .B \eW
552 is a synonym for
553 .BR [^[:alnum:]] .
554 .PP
555 The caret
556 .B ^
557 and the dollar sign
558 .B $
559 are metacharacters that respectively match the empty string at the
560 beginning and end of a line.
561 The symbols
562 .B \e<
563 and
564 .B \e>
565 respectively match the empty string at the beginning and end of a word.
566 The symbol
567 .B \eb
568 matches the empty string at the edge of a word,
569 and
570 .B \eB
571 matches the empty string provided it's
572 .I not
573 at the edge of a word.
574 .PP
575 A regular expression may be followed by one of several repetition operators:
576 .PD 0
577 .TP
578 .B ?
579 The preceding item is optional and matched at most once.
580 .TP
581 .B *
582 The preceding item will be matched zero or more times.
583 .TP
584 .B +
585 The preceding item will be matched one or more times.
586 .TP
587 .BI { n }
588 The preceding item is matched exactly
589 .I n
590 times.
591 .TP
592 .BI { n ,}
593 The preceding item is matched
594 .I n
595 or more times.
596 .TP
597 .BI { n , m }
598 The preceding item is matched at least
599 .I n
600 times, but not more than
601 .I m
602 times.
603 .PD
604 .PP
605 Two regular expressions may be concatenated; the resulting
606 regular expression matches any string formed by concatenating
607 two substrings that respectively match the concatenated
608 subexpressions.
609 .PP
610 Two regular expressions may be joined by the infix operator
611 .BR | ;
612 the resulting regular expression matches any string matching
613 either subexpression.
614 .PP
615 Repetition takes precedence over concatenation, which in turn
616 takes precedence over alternation.  A whole subexpression may be
617 enclosed in parentheses to override these precedence rules.
618 .PP
619 The backreference
620 .BI \e n\c
621 \&, where
622 .I n
623 is a single digit, matches the substring
624 previously matched by the
625 .IR n th
626 parenthesized subexpression of the regular expression.
627 .PP
628 In basic regular expressions the metacharacters
629 .BR ? ,
630 .BR + ,
631 .BR { ,
632 .BR | ,
633 .BR ( ,
634 and
635 .BR )
636 lose their special meaning; instead use the backslashed
637 versions
638 .BR \e? ,
639 .BR \e+ ,
640 .BR \e{ ,
641 .BR \e| ,
642 .BR \e( ,
643 and
644 .BR \e) .
645 .PP
646 Traditional
647 .B egrep
648 did not support the
649 .B {
650 metacharacter, and some
651 .B egrep
652 implementations support
653 .B \e{
654 instead, so portable scripts should avoid
655 .B {
656 in
657 .B egrep
658 patterns and should use
659 .B [{]
660 to match a literal
661 .BR { .
662 .PP
663 \s-1GNU\s0
664 .B egrep
665 attempts to support traditional usage by assuming that
666 .B {
667 is not special if it would be the start of an invalid interval
668 specification.  For example, the shell command
669 .B "egrep '{1'"
670 searches for the two-character string
671 .B {1
672 instead of reporting a syntax error in the regular expression.
673 \s-1POSIX.2\s0 allows this behavior as an extension, but portable scripts
674 should avoid it.
675 .SH "ENVIRONMENT VARIABLES"
676 Grep's behavior is affected by the following environment variables.
677 .PP
678 A locale
679 .BI LC_ foo
680 is specified by examining the three environment variables
681 .BR LC_ALL ,
682 .BR LC_\fIfoo\fP ,
683 .BR LANG ,
684 in that order.
685 The first of these variables that is set specifies the locale.
686 For example, if
687 .B LC_ALL
688 is not set, but
689 .B LC_MESSAGES
690 is set to
691 .BR pt_BR ,
692 then Brazilian Portuguese is used for the
693 .B LC_MESSAGES
694 locale.
695 The C locale is used if none of these environment variables are set,
696 or if the locale catalog is not installed, or if
697 .B grep
698 was not compiled with national language support (\s-1NLS\s0).
699 .TP
700 .B GREP_OPTIONS
701 This variable specifies default options to be placed in front of any
702 explicit options.  For example, if
703 .B GREP_OPTIONS
704 is
705 .BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,
706 .B grep
707 behaves as if the two options
708 .B \-\^\-binary-files=without-match
709 and
710 .B \-\^\-directories=skip
711 had been specified before any explicit options.
712 Option specifications are separated by whitespace.
713 A backslash escapes the next character,
714 so it can be used to specify an option containing whitespace or a backslash.
715 .TP
716 .B GREP_COLOR
717 Specifies the marker for highlighting.
718 .TP
719 \fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fP
720 These variables specify the
721 .B LC_COLLATE
722 locale, which determines the collating sequence used to interpret
723 range expressions like
724 .BR [a\-z] .
725 .TP
726 \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
727 These variables specify the
728 .B LC_CTYPE
729 locale, which determines the type of characters, e.g., which
730 characters are whitespace.
731 .TP
732 \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
733 These variables specify the
734 .B LC_MESSAGES
735 locale, which determines the language that
736 .B grep
737 uses for messages.
738 The default C locale uses American English messages.
739 .TP
740 .B POSIXLY_CORRECT
741 If set,
742 .B grep
743 behaves as \s-1POSIX.2\s0 requires; otherwise,
744 .B grep
745 behaves more like other \s-1GNU\s0 programs.
746 \s-1POSIX.2\s0 requires that options that follow file names must be
747 treated as file names; by default, such options are permuted to the
748 front of the operand list and are treated as options.
749 Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as
750 \*(lqillegal\*(rq, but since they are not really against the law the default
751 is to diagnose them as \*(lqinvalid\*(rq.
752 .SH DIAGNOSTICS
753 .PP
754 Normally, exit status is 0 if selected lines are found and 1 otherwise.
755 But the exit status is 2 if an error occurred, unless the
756 .B \-q
757 or
758 .B \-\^\-quiet
759 or
760 .B \-\^\-silent
761 option is used and a selected line is found.
762 .SH BUGS
763 Email bug reports to
764 .BR bug-gnu-utils@gnu.org .
765 Be sure to include the word \*(lqgrep\*(rq somewhere in the
766 \*(lqSubject:\*(rq field.
767 .PP
768 Large repetition counts in the
769 .BI { n , m }
770 construct may cause grep to use lots of memory.
771 In addition,
772 certain other obscure regular expressions require exponential time
773 and space, and may cause
774 .B grep
775 to run out of memory.
776 .PP
777 Backreferences are very slow, and may require exponential time.
778 .\" Work around problems with some troff -man implementations.
779 .br