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