1 .\" $OpenBSD: grep.1,v 1.42 2013/01/17 21:29:14 jmc Exp $
2 .\" Copyright (c) 1980, 1990, 1993
3 .\" The Regents of the University of California. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. Neither the name of the University nor the names of its contributors
14 .\" may be used to endorse or promote products derived from this software
15 .\" without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 .\" @(#)grep.1 8.3 (Berkeley) 4/18/94
31 .Dd $Mdocdate: January 17 2013 $
35 .Nm grep , egrep , fgrep ,
36 .Nm zgrep , zegrep , zfgrep
37 .Nd file pattern searcher
41 .Op Fl abcEFGHhIiLlnoqRsUVvwxZ
47 .Op Fl -binary-files Ns = Ns Ar value
48 .Op Fl -context Ns Op = Ns Ar num
56 utility searches any given input files,
57 selecting lines that match one or more patterns.
58 By default, a pattern matches an input line if the regular expression
59 (RE) in the pattern matches the input line
60 without its trailing newline.
61 An empty expression matches every line.
62 Each input line that matches at least one of the patterns is written
63 to the standard output.
64 If no file arguments are specified, the standard input is used.
67 is used for simple patterns and
68 basic regular expressions
71 can handle extended regular expressions
75 for more information on regular expressions.
81 but can only handle fixed patterns
82 (i.e. it does not interpret regular expressions).
83 Patterns may consist of one or more lines,
84 allowing any of the pattern lines to match a portion of the input.
95 respectively, but accept input files compressed with the
99 compression utilities.
101 The following options are available:
102 .Bl -tag -width indent
106 lines of trailing context after each match.
113 Treat all files as ASCII text.
117 .Dq Binary file ... matches
118 if files contain binary characters.
119 Use of this option forces
121 to output lines matching the specified pattern.
125 lines of leading context before each match.
132 The offset in bytes of a matched pattern is
133 displayed in front of the respective matched line.
134 .It Fl C Ns Op Ar num
137 lines of leading and trailing context surrounding each match.
138 The default is 2 and is equivalent to
144 no whitespace may be given between the option and its argument.
146 Only a count of selected lines is written to standard output.
150 as an extended regular expression
156 Specify a pattern used during the search of the input:
157 an input line is selected if it matches any of the specified patterns.
158 This option is most useful when multiple
160 options are used to specify multiple patterns,
161 or when a pattern begins with a dash
166 as a set of fixed strings
172 Read one or more newline separated patterns from
174 Empty pattern lines match every input line.
175 Newlines are not considered part of a pattern.
178 is empty, nothing is matched.
182 as a basic regular expression
185 to behave as traditional
188 Always print filename headers
192 Never print filename headers
198 Perform case insensitive matching.
203 Only the names of files not containing selected lines are written to
205 Pathnames are listed once per file searched.
206 If the standard input is searched, the string
210 Only the names of files containing selected lines are written to
213 will only search a file until a match has been found,
214 making searches potentially less expensive.
215 Pathnames are listed once per file searched.
216 If the standard input is searched, the string
220 Each output line is preceded by its relative line number in the file,
222 The line number counter is reset for each file processed.
223 This option is ignored if
232 Print each match, but only the match, not the entire line.
235 suppress normal output.
237 will only search a file until a match has been found,
238 making searches potentially less expensive.
240 Recursively search subdirectories listed.
243 Nonexistent and unreadable files are ignored
244 (i.e. their error messages are suppressed).
246 Search binary files, but do not attempt to print them.
248 Display version information.
249 All other options are ignored.
251 Selected lines are those
253 matching any of the specified patterns.
255 The expression is searched for as a word (as if surrounded by
262 Only input lines selected against an entire fixed string or regular
263 expression are considered to be matching lines.
269 .It Fl Fl binary-files Ns = Ns Ar value
270 Controls searching and printing of binary files.
273 the default: search binary files but do not print them;
275 do not search binary files;
278 treat all files as text.
280 .It Fl Fl context Op = Ar num
284 lines of leading and trailing context.
286 .It Fl Fl line-buffered
287 Force output to be line buffered.
288 By default, output is line buffered when standard output is a terminal
289 and block buffered otherwise.
294 utility exits with one of the following values:
296 .Bl -tag -width Ds -offset indent -compact
298 One or more lines were selected.
300 No lines were selected.
305 To find all occurrences of the word
309 .Dl $ grep 'patricia' myfile
311 To find all occurrences of the pattern
313 at the beginning of a line:
315 .Dl $ grep '^\e.Pp' myfile
317 The apostrophes ensure the entire expression is evaluated by
319 instead of by the user's shell.
322 matches the null string at the beginning of a line,
327 which would otherwise match any character.
329 To find all lines in a file which do not contain the words
334 .Dl $ grep -v -e 'foo' -e 'bar' myfile
336 A simple example of an extended regular expression:
338 .Dl $ egrep '19|20|25' calendar
342 looking for either 19, 20, or 25.
352 utility is compliant with the
357 .Op Fl AaBbCGHhILoRUVwZ
358 are extensions to that specification, and the behaviour of the
360 flag when used with an empty pattern file is left undefined.
362 All long options are provided for compatibility with
363 GNU versions of this utility.
365 Historic versions of the
367 utility also supported the flags
369 This implementation supports those options;
370 however, their use is strongly discouraged.
374 command first appeared in