1 .\" $NetBSD: grep.1,v 1.15 2009/10/23 23:04:29 joerg Exp $
4 .\" Id: grep.1,v 1.24 2003/06/16 08:34:16 kasal Exp
5 .TH GREP 1 "June 16, 2003" "GNU Project"
7 grep, egrep, fgrep \- print lines matching a pattern
25 searches the named input
27 (or standard input if no files are named, or
31 for lines containing a match to the given
35 prints the matching lines.
37 In addition, two variant programs
50 .BI \-A " NUM" "\fR,\fP \-\^\-after-context=" NUM
53 lines of trailing context after matching lines.
54 Places a line containing
56 between contiguous groups of matches.
58 .BR \-a ", " \-\^\-text
59 Process a binary file as if it were text; this is equivalent to the
60 .B \-\^\-binary-files=text
63 .BI \-B " NUM" "\fR,\fP \-\^\-before-context=" NUM
66 lines of leading context before matching lines.
67 Places a line containing
69 between contiguous groups of matches.
71 .BR \-b ", " \-\^\-byte-offset
72 Print the byte offset within the input file before
75 .BI \-\^\-binary-files= TYPE
76 If the first few bytes of a file indicate that the file contains binary
77 data, assume that the file is of type
85 normally outputs either
86 a one-line message saying that a binary file matches, or no message if
93 assumes that a binary file does not match; this is equivalent to the
101 processes a binary file as if it were text; this is equivalent to the
105 .B "grep \-\^\-binary-files=text"
106 might output binary garbage,
107 which can have nasty side effects if the output is a terminal and if the
108 terminal driver interprets some of it as commands.
110 .BI \-C " NUM" "\fR,\fP \-\^\-context=" NUM
113 lines of output context.
114 Places a line containing
116 between contiguous groups of matches.
118 .BR \-c ", " \-\^\-count
119 Suppress normal output; instead print a count of
120 matching lines for each input file.
122 .BR \-v ", " \-\^\-invert-match
123 option (see below), count non-matching lines.
125 .BI \-\^\-colour[=\fIWHEN\fR] ", " \-\^\-color[=\fIWHEN\fR]
126 Surround the matching string with the marker find in
128 environment variable. WHEN may be `never', `always', or `auto'
130 .BI \-D " ACTION" "\fR,\fP \-\^\-devices=" ACTION
131 If an input file is a device, FIFO or socket, use
133 to process it. By default,
137 which means that devices are read just as if they were ordinary files.
142 devices are silently skipped.
144 .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
145 If an input file is a directory, use
147 to process it. By default,
151 which means that directories are read just as if they were ordinary files.
156 directories are silently skipped.
162 reads all files under each directory, recursively;
163 this is equivalent to the
167 .BR \-E ", " \-\^\-extended-regexp
170 as an extended regular expression (see below).
172 .BI \-e " PATTERN" "\fR,\fP \-\^\-regexp=" PATTERN
175 as the pattern; useful to protect patterns beginning with
177 May be specified more than once.
179 .BR \-F ", " \-\^\-fixed-strings
182 as a list of fixed strings, separated by newlines,
183 any of which is to be matched.
185 .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
189 The empty file contains zero patterns, and therefore matches nothing.
191 .BR \-G ", " \-\^\-basic-regexp
194 as a basic regular expression (see below). This is the default.
196 .BR \-H ", " \-\^\-with-filename
197 Print the filename for each match.
199 .BR \-h ", " \-\^\-no-filename
200 Suppress the prefixing of filenames on output
201 when multiple files are searched.
204 Output a brief help message.
207 Process a binary file as if it did not contain matching data; this is
209 .B \-\^\-binary-files=without-match
212 .BR \-i ", " \-\^\-ignore-case
213 Ignore case distinctions in both the
217 .BR \-L ", " \-\^\-files-without-match
218 Suppress normal output; instead print the name
219 of each input file from which no output would
220 normally have been printed. The scanning will stop
223 .BR \-l ", " \-\^\-files-with-matches
224 Suppress normal output; instead print
225 the name of each input file from which output
226 would normally have been printed. The scanning will
227 stop on the first match.
229 .BI \-\^\-label= LABEL
230 Displays input actually coming from standard input as input coming from file
232 This is especially useful for tools like zgrep, e.g.
233 .B "gzip -cd foo.gz |grep --label=foo something"
235 .BR \-\^\-line-buffered
236 Use line buffering, it can be a performance penalty.
238 .BI \-m " NUM" "\fR,\fP \-\^\-max-count=" NUM
239 Stop reading a file after
241 matching lines. If the input is standard input from a regular file,
244 matching lines are output,
246 ensures that the standard input is positioned to just after the last
247 matching line before exiting, regardless of the presence of trailing
248 context lines. This enables a calling process to resume a search.
253 matching lines, it outputs any trailing context lines. When the
259 does not output a count greater than
264 .B \-\^\-invert-match
267 stops after outputting
274 system call to read input, instead of
277 system call. In some situations,
279 yields better performance. However,
281 can cause undefined behavior (including core dumps)
282 if an input file shrinks while
284 is operating, or if an I/O error occurs.
286 .BR \-n ", " \-\^\-line-number
287 Prefix each line of output with the line number
288 within its input file.
290 .BR \-o ", " \-\^\-only-matching
291 Show only the part of a matching line that matches
293 .\" NetBSD doesn't support perl regexps
295 .\" .BR \-P ", " \-\^\-perl-regexp
298 .\" as a Perl regular expression.
300 .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
301 Quiet; do not write anything to standard output.
302 Exit immediately with zero status if any match is found,
303 even if an error was detected.
310 .BR \-R ", " \-r ", " \-\^\-recursive
311 Read all files under each directory, recursively;
312 this is equivalent to the
316 .BR "\fR \fP \-\^\-include=" PATTERN
317 Recurse in directories only searching file matching
320 .BR "\fR \fP \-\^\-exclude=" PATTERN
321 Recurse in directories skip file matching
324 .BR \-s ", " \-\^\-no-messages
325 Suppress error messages about nonexistent or unreadable files.
326 Portability note: unlike \s-1GNU\s0
330 did not conform to \s-1POSIX.2\s0, because traditional
336 option behaved like \s-1GNU\s0
340 Shell scripts intended to be portable to traditional
346 and should redirect output to /dev/null instead.
348 .BR \-U ", " \-\^\-binary
349 Treat the file(s) as binary. By default, under MS-DOS and MS-Windows,
351 guesses the file type by looking at the contents of the first 32KB
352 read from the file. If
354 decides the file is a text file, it strips the CR characters from the
355 original file contents (to make regular expressions with
359 work correctly). Specifying
361 overrules this guesswork, causing all files to be read and passed to the
362 matching mechanism verbatim; if the file is a text file with CR/LF
363 pairs at the end of each line, this will cause some regular
365 This option has no effect on platforms other than MS-DOS and
368 .BR \-u ", " \-\^\-unix-byte-offsets
369 Report Unix-style byte offsets. This switch causes
371 to report byte offsets as if the file were Unix-style text file, i.e. with
372 CR characters stripped off. This will produce results identical to running
374 on a Unix machine. This option has no effect unless
377 it has no effect on platforms other than MS-DOS and MS-Windows.
379 .BR \-V ", " \-\^\-version
380 Print the version number of
382 to standard error. This version number should
383 be included in all bug reports (see below).
385 .BR \-v ", " \-\^\-invert-match
386 Invert the sense of matching, to select non-matching lines.
388 .BR \-w ", " \-\^\-word-regexp
389 Select only those lines containing matches that form whole words.
390 The test is that the matching substring must either be at the
391 beginning of the line, or preceded by a non-word constituent
392 character. Similarly, it must be either at the end of the line
393 or followed by a non-word constituent character. Word-constituent
394 characters are letters, digits, and the underscore.
396 .BR \-x ", " \-\^\-line-regexp
397 Select only those matches that exactly match the whole line.
403 .BR \-Z ", " \-\^\-null
404 Output a zero byte (the \s-1ASCII\s0
406 character) instead of the character that normally follows a file name.
409 outputs a zero byte after each file name instead of the usual newline.
410 This option makes the output unambiguous, even in the presence of file
411 names containing unusual characters like newlines. This option can be
412 used with commands like
413 .BR "find \-print0" ,
418 to process arbitrary file names,
419 even those that contain newline characters.
420 .SH "REGULAR EXPRESSIONS"
422 A regular expression is a pattern that describes a set of strings.
423 Regular expressions are constructed analogously to arithmetic
424 expressions, by using various operators to combine smaller expressions.
427 understands two different versions of regular expression syntax:
428 \*(lqbasic\*(rq and \*(lqextended.\*(rq In
429 .RB "\s-1GNU\s0\ " grep ,
430 there is no difference in available functionality using either syntax.
431 In other implementations, basic regular expressions are less powerful.
432 The following description applies to extended regular expressions;
433 differences for basic regular expressions are summarized afterwards.
435 The fundamental building blocks are the regular expressions that match
436 a single character. Most characters, including all letters and digits,
437 are regular expressions that match themselves. Any metacharacter with
438 special meaning may be quoted by preceding it with a backslash.
441 .I "bracket expression"
442 is a list of characters enclosed by
446 It matches any single
447 character in that list; if the first character of the list
450 then it matches any character
453 For example, the regular expression
455 matches any single digit.
457 Within a bracket expression, a
458 .I "range expression"
459 consists of two characters separated by a hyphen.
460 It matches any single character that sorts between the two characters,
461 inclusive, using the locale's collating sequence and character set.
462 For example, in the default C locale,
466 Many locales sort characters in dictionary order, and in these locales
468 is typically not equivalent to
470 it might be equivalent to
473 To obtain the traditional interpretation of bracket expressions,
474 you can use the C locale by setting the
476 environment variable to the value
479 Finally, certain named classes of characters are predefined within
480 bracket expressions, as follows.
481 Their names are self explanatory, and they are
498 except the latter form depends upon the C locale and the
499 \s-1ASCII\s0 character encoding, whereas the former is independent
500 of locale and character set.
501 (Note that the brackets in these class names are part of the symbolic
502 names, and must be included in addition to the brackets delimiting
503 the bracket list.) Most metacharacters lose their special meaning
504 inside lists. To include a literal
506 place it first in the list. Similarly, to include a literal
508 place it anywhere but first. Finally, to include a literal
514 matches any single character.
528 are metacharacters that respectively match the empty string at the
529 beginning and end of a line.
534 respectively match the empty string at the beginning and end of a word.
537 matches the empty string at the edge of a word,
540 matches the empty string provided it's
542 at the edge of a word.
544 A regular expression may be followed by one of several repetition operators:
548 The preceding item is optional and matched at most once.
551 The preceding item will be matched zero or more times.
554 The preceding item will be matched one or more times.
557 The preceding item is matched exactly
562 The preceding item is matched
567 The preceding item is matched at least
569 times, but not more than
574 Two regular expressions may be concatenated; the resulting
575 regular expression matches any string formed by concatenating
576 two substrings that respectively match the concatenated
579 Two regular expressions may be joined by the infix operator
581 the resulting regular expression matches any string matching
582 either subexpression.
584 Repetition takes precedence over concatenation, which in turn
585 takes precedence over alternation. A whole subexpression may be
586 enclosed in parentheses to override these precedence rules.
592 is a single digit, matches the substring
593 previously matched by the
595 parenthesized subexpression of the regular expression.
597 In basic regular expressions the metacharacters
605 lose their special meaning; instead use the backslashed
619 metacharacter, and some
621 implementations support
623 instead, so portable scripts should avoid
627 patterns and should use
634 attempts to support traditional usage by assuming that
636 is not special if it would be the start of an invalid interval
637 specification. For example, the shell command
639 searches for the two-character string
641 instead of reporting a syntax error in the regular expression.
642 \s-1POSIX.2\s0 allows this behavior as an extension, but portable scripts
644 .SH "ENVIRONMENT VARIABLES"
645 Grep's behavior is affected by the following environment variables.
649 is specified by examining the three environment variables
654 The first of these variables that is set specifies the locale.
661 then Brazilian Portuguese is used for the
664 The C locale is used if none of these environment variables are set,
665 or if the locale catalog is not installed, or if
667 was not compiled with national language support (\s-1NLS\s0).
670 This variable specifies default options to be placed in front of any
671 explicit options. For example, if
674 .BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,
676 behaves as if the two options
677 .B \-\^\-binary-files=without-match
679 .B \-\^\-directories=skip
680 had been specified before any explicit options.
681 Option specifications are separated by whitespace.
682 A backslash escapes the next character,
683 so it can be used to specify an option containing whitespace or a backslash.
686 Specifies the marker for highlighting.
688 \fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fP
689 These variables specify the
691 locale, which determines the collating sequence used to interpret
692 range expressions like
695 \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
696 These variables specify the
698 locale, which determines the type of characters, e.g., which
699 characters are whitespace.
701 \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
702 These variables specify the
704 locale, which determines the language that
707 The default C locale uses American English messages.
712 behaves as \s-1POSIX.2\s0 requires; otherwise,
714 behaves more like other \s-1GNU\s0 programs.
715 \s-1POSIX.2\s0 requires that options that follow file names must be
716 treated as file names; by default, such options are permuted to the
717 front of the operand list and are treated as options.
718 Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as
719 \*(lqillegal\*(rq, but since they are not really against the law the default
720 is to diagnose them as \*(lqinvalid\*(rq.
722 also disables \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP,
725 \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP
730 numeric process ID.) If the
732 character of this environment variable's value is
738 to be an option, even if it appears to be one.
739 A shell can put this variable in the environment for each command it runs,
740 specifying which operands are the results of file name wildcard
741 expansion and therefore should not be treated as options.
742 This behavior is available only with the \s-1GNU\s0 C library, and only
748 Normally, exit status is 0 if selected lines are found and 1 otherwise.
749 But the exit status is 2 if an error occurred, unless the
755 option is used and a selected line is found.
759 .BR bug-gnu-utils@gnu.org .
760 Be sure to include the word \*(lqgrep\*(rq somewhere in the
761 \*(lqSubject:\*(rq field.
763 Large repetition counts in the
765 construct may cause grep to use lots of memory.
767 certain other obscure regular expressions require exponential time
768 and space, and may cause
770 to run out of memory.
772 Backreferences are very slow, and may require exponential time.
773 .\" Work around problems with some troff -man implementations.