2 .\" $OpenBSD: grep.1,v 1.38 2010/04/05 06:30:59 jmc Exp $
3 .\" Copyright (c) 1980, 1990, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)grep.1 8.3 (Berkeley) 4/18/94
36 .Nm grep , egrep , fgrep ,
37 .Nm zgrep , zegrep , zfgrep
38 .Nd file pattern searcher
42 .Op Fl abcdDEFGHhIiJLlmnOopqRSsUVvwxZ
48 .Op Fl Fl binary-files Ns = Ns Ar value
49 .Op Fl Fl color Ns Op = Ns Ar when
50 .Op Fl Fl colour Ns Op = Ns Ar when
51 .Op Fl Fl context Ns Op = Ns Ar num
53 .Op Fl Fl line-buffered
61 utility searches any given input files,
62 selecting lines that match one or more patterns.
63 By default, a pattern matches an input line if the regular expression
64 (RE) in the pattern matches the input line
65 without its trailing newline.
66 An empty expression matches every line.
67 Each input line that matches at least one of the patterns is written
68 to the standard output.
71 is used for simple patterns and
72 basic regular expressions
75 can handle extended regular expressions
79 for more information on regular expressions.
85 but can only handle fixed patterns
86 (i.e. it does not interpret regular expressions).
87 Patterns may consist of one or more lines,
88 allowing any of the pattern lines to match a portion of the input.
99 respectively, but accept input files compressed with the
103 compression utilities.
105 The following options are available:
106 .Bl -tag -width indent
107 .It Fl A Ar num , Fl Fl after-context Ns = Ns Ar num
110 lines of trailing context after each match.
116 .It Fl a , Fl Fl text
117 Treat all files as ASCII text.
121 .Dq Binary file ... matches
122 if files contain binary characters.
123 Use of this option forces
125 to output lines matching the specified pattern.
126 .It Fl B Ar num , Fl Fl before-context Ns = Ns Ar num
129 lines of leading context before each match.
135 .It Fl b , Fl Fl byte-offset
136 The offset in bytes of a matched pattern is
137 displayed in front of the respective matched line.
138 .It Fl C Ns Op Ar num , Fl Fl context Ns = Ns Ar num
141 lines of leading and trailing context surrounding each match.
142 The default is 2 and is equivalent to
148 no whitespace may be given between the option and its argument.
149 .It Fl c , Fl Fl count
150 Only a count of selected lines is written to standard output.
151 .It Fl Fl colour Ns = Ns Op Ar when , Fl Fl color Ns = Ns Op Ar when
152 Mark up the matching text with the expression stored in
154 environment variable.
155 The possible values of when can be `never', `always' or `auto'.
156 .It Fl D Ar action , Fl Fl devices Ns = Ns Ar action
157 Specify the demanded action for devices, FIFOs and sockets.
158 The default action is `read', which means, that they are read
159 as if they were normal files.
160 If the action is set to `skip', devices will be silently skipped.
161 .It Fl d Ar action , Fl Fl directories Ns = Ns Ar action
162 Specify the demanded action for directories.
163 It is `read' by default, which means that the directories
164 are read in the same manner as normal files.
165 Other possible values are `skip' to silently ignore the
166 directories, and `recurse' to read them recursively, which
167 has the same effect as the
172 .It Fl E , Fl Fl extended-regexp
175 as an extended regular expression
180 .It Fl e Ar pattern , Fl Fl regexp Ns = Ns Ar pattern
181 Specify a pattern used during the search of the input:
182 an input line is selected if it matches any of the specified patterns.
183 This option is most useful when multiple
185 options are used to specify multiple patterns,
186 or when a pattern begins with a dash
189 If specified, it excludes files matching the given
190 filename pattern from the search.
193 patterns take priority over
197 pattern is specified, all files are searched that are
199 Patterns are matched to the full path specified,
200 not only to the filename component.
201 .It Fl Fl exclude-dir
204 is specified, it excludes directories matching the
205 given filename pattern from the search.
208 patterns take priority over
212 pattern is specified, all directories are searched that are
214 .It Fl F , Fl Fl fixed-strings
217 as a set of fixed strings
222 .It Fl f Ar file , Fl Fl file Ns = Ns Ar file
223 Read one or more newline separated patterns from
225 Empty pattern lines match every input line.
226 Newlines are not considered part of a pattern.
229 is empty, nothing is matched.
230 .It Fl G , Fl Fl basic-regexp
233 as a basic regular expression
236 to behave as traditional
239 Always print filename headers with output lines.
240 .It Fl h , Fl Fl no-filename
241 Never print filename headers
245 Print a brief help message.
248 This option is equivalent to
249 .Fl Fl binary-file Ns = Ns Ar without-match
251 .It Fl i , Fl Fl ignore-case
252 Perform case insensitive matching.
257 If specified, only files matching the
258 given filename pattern are searched.
261 patterns take priority over
264 Patterns are matched to the full path specified,
265 not only to the filename component.
266 .It Fl Fl include-dir
269 is specified, only directories matching the
270 given filename pattern are searched.
273 patterns take priority over
276 .It Fl J, Fl Fl bz2decompress
279 compressed file before looking for the text.
280 .It Fl L , Fl Fl files-without-match
281 Only the names of files not containing selected lines are written to
283 Pathnames are listed once per file searched.
284 If the standard input is searched, the string
287 .It Fl l , Fl Fl files-with-matches
288 Only the names of files containing selected lines are written to
291 will only search a file until a match has been found,
292 making searches potentially less expensive.
293 Pathnames are listed once per file searched.
294 If the standard input is searched, the string
302 to read input, which can result in better performance under some
303 circumstances but can cause undefined behaviour.
304 .It Fl m Ar num, Fl Fl max-count Ns = Ns Ar num
305 Stop reading the file after
308 .It Fl n , Fl Fl line-number
309 Each output line is preceded by its relative line number in the file,
311 The line number counter is reset for each file processed.
312 This option is ignored if
321 Prints a zero-byte after the file name.
325 is specified, follow symbolic links only if they were explicitly listed
327 The default is not to follow symbolic links.
328 .It Fl o, Fl Fl only-matching
329 Prints only the matching part of the lines.
333 is specified, no symbolic links are followed.
335 .It Fl q , Fl Fl quiet , Fl Fl silent
337 suppress normal output.
339 will only search a file until a match has been found,
340 making searches potentially less expensive.
341 .It Fl R , Fl r , Fl Fl recursive
342 Recursively search subdirectories listed.
346 is specified, all symbolic links are followed.
347 The default is not to follow symbolic links.
348 .It Fl s , Fl Fl no-messages
350 Nonexistent and unreadable files are ignored
351 (i.e. their error messages are suppressed).
352 .It Fl U , Fl Fl binary
353 Search binary files, but do not attempt to print them.
354 .It Fl V , Fl Fl version
355 Display version information and exit.
356 .It Fl v , Fl Fl invert-match
357 Selected lines are those
359 matching any of the specified patterns.
360 .It Fl w , Fl Fl word-regexp
361 The expression is searched for as a word (as if surrounded by
367 .It Fl x , Fl Fl line-regexp
368 Only input lines selected against an entire fixed string or regular
369 expression are considered to be matching lines.
374 .It Fl Z , Fl z , Fl Fl decompress
379 .It Fl Fl binary-files Ns = Ns Ar value
380 Controls searching and printing of binary files.
383 the default: search binary files but do not print them;
385 do not search binary files;
388 treat all files as text.
390 .It Fl Fl context Op = Ar num
394 lines of leading and trailing context.
396 .It Fl Fl line-buffered
397 Force output to be line buffered.
398 By default, output is line buffered when standard output is a terminal
399 and block buffered otherwise.
402 If no file arguments are specified, the standard input is used.
406 utility exits with one of the following values:
408 .Bl -tag -width flag -compact
410 One or more lines were selected.
412 No lines were selected.
417 To find all occurrences of the word
421 .Dl $ grep 'patricia' myfile
423 To find all occurrences of the pattern
425 at the beginning of a line:
427 .Dl $ grep '^\e.Pp' myfile
429 The apostrophes ensure the entire expression is evaluated by
431 instead of by the user's shell.
434 matches the null string at the beginning of a line,
439 which would otherwise match any character.
441 To find all lines in a file which do not contain the words
446 .Dl $ grep -v -e 'foo' -e 'bar' myfile
448 A simple example of an extended regular expression:
450 .Dl $ egrep '19|20|25' calendar
454 looking for either 19, 20, or 25.
464 utility is compliant with the
469 .Op Fl AaBbCDdGHhIJLmoPRSUVwZ
470 are extensions to that specification, and the behaviour of the
472 flag when used with an empty pattern file is left undefined.
474 All long options are provided for compatibility with
475 GNU versions of this utility.
477 Historic versions of the
479 utility also supported the flags
481 This implementation supports those options;
482 however, their use is strongly discouraged.
486 command first appeared in