1 .\" $OpenBSD: grep.1,v 1.35 2007/05/31 19:20:10 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: May 31 2007 $
35 .Nm grep , egrep , fgrep ,
36 .Nm zgrep , zegrep , zfgrep
37 .Nd file pattern searcher
41 .Op Fl abcEFGHhIiLlnoPqRSsUVvwxZ
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.
66 is used for simple patterns and
67 basic regular expressions
70 can handle extended regular expressions
74 for more information on regular expressions.
80 but can only handle fixed patterns
81 (i.e. it does not interpret regular expressions).
82 Patterns may consist of one or more lines,
83 allowing any of the pattern lines to match a portion of the input.
94 respectively, but accept input files compressed with the
98 compression utilities.
100 The following options are available:
101 .Bl -tag -width indent
105 lines of trailing context after each match.
112 Treat all files as ASCII text.
116 .Dq Binary file ... matches
117 if files contain binary characters.
118 Use of this option forces
120 to output lines matching the specified pattern.
124 lines of leading context before each match.
131 The offset in bytes of a matched pattern is
132 displayed in front of the respective matched line.
133 .It Fl C Ns Op Ar num
136 lines of leading and trailing context surrounding each match.
137 The default is 2 and is equivalent to
143 no whitespace may be given between the option and its argument.
145 Only a count of selected lines is written to standard output.
149 as an extended regular expression
155 Specify a pattern used during the search of the input:
156 an input line is selected if it matches any of the specified patterns.
157 This option is most useful when multiple
159 options are used to specify multiple patterns,
160 or when a pattern begins with a dash
165 as a set of fixed strings
171 Read one or more newline separated patterns from
173 Empty pattern lines match every input line.
174 Newlines are not considered part of a pattern.
177 is empty, nothing is matched.
181 as a basic regular expression
184 to behave as traditional
189 is specified, follow symbolic links only if they were explicitly listed
191 The default is not to follow symbolic links.
193 Never print filename headers
199 Perform case insensitive matching.
204 Only the names of files not containing selected lines are written to
206 Pathnames are listed once per file searched.
207 If the standard input is searched, the string
211 Only the names of files containing selected lines are written to
214 will only search a file until a match has been found,
215 making searches potentially less expensive.
216 Pathnames are listed once per file searched.
217 If the standard input is searched, the string
221 Each output line is preceded by its relative line number in the file,
223 The line number counter is reset for each file processed.
224 This option is ignored if
233 Always print filename headers with output lines.
237 is specified, no symbolic links are followed.
241 suppress normal output.
243 will only search a file until a match has been found,
244 making searches potentially less expensive.
246 Recursively search subdirectories listed.
250 is specified, all symbolic links are followed.
251 The default is not to follow symbolic links.
254 Nonexistent and unreadable files are ignored
255 (i.e. their error messages are suppressed).
257 Search binary files, but do not attempt to print them.
259 Display version information.
260 All other options are ignored.
262 Selected lines are those
264 matching any of the specified patterns.
266 The expression is searched for as a word (as if surrounded by
273 Only input lines selected against an entire fixed string or regular
274 expression are considered to be matching lines.
280 .It Fl Fl binary-files Ns = Ns Ar value
281 Controls searching and printing of binary files.
284 the default: search binary files but do not print them;
286 do not search binary files;
289 treat all files as text.
291 .It Fl Fl context Op = Ar num
295 lines of leading and trailing context.
297 .It Fl Fl line-buffered
298 Force output to be line buffered.
299 By default, output is line buffered when standard output is a terminal
300 and block buffered otherwise.
303 If no file arguments are specified, the standard input is used.
307 utility exits with one of the following values:
309 .Bl -tag -width flag -compact
311 One or more lines were selected.
313 No lines were selected.
318 To find all occurrences of the word
322 .Dl $ grep 'patricia' myfile
324 To find all occurrences of the pattern
326 at the beginning of a line:
328 .Dl $ grep '^\e.Pp' myfile
330 The apostrophes ensure the entire expression is evaluated by
332 instead of by the user's shell.
335 matches the null string at the beginning of a line,
340 which would otherwise match any character.
342 To find all lines in a file which do not contain the words
347 .Dl $ grep -v -e 'foo' -e 'bar' myfile
349 A simple example of an extended regular expression:
351 .Dl $ egrep '19|20|25' calendar
355 looking for either 19, 20, or 25.
365 utility is compliant with the
370 .Op Fl AaBbCGHhILoPRSUVwZ
371 are extensions to that specification, and the behaviour of the
373 flag when used with an empty pattern file is left undefined.
375 All long options are provided for compatibility with
376 GNU versions of this utility.
378 Historic versions of the
380 utility also supported the flags
382 This implementation supports those options;
383 however, their use is strongly discouraged.
387 command first appeared in