xref: /freebsd/usr.bin/grep/grep.1 (revision 2008043f386721d58158e37e0d7e50df8095942d)
1.\"	$NetBSD: grep.1,v 1.2 2011/02/16 01:31:33 joerg Exp $
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.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
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.
17.\"
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
28.\" SUCH DAMAGE.
29.\"
30.\"	@(#)grep.1	8.3 (Berkeley) 4/18/94
31.\"
32.Dd December 18, 2022
33.Dt GREP 1
34.Os
35.Sh NAME
36.Nm grep ,
37.Nm egrep ,
38.Nm fgrep ,
39.Nm rgrep
40.Nd file pattern searcher
41.Sh SYNOPSIS
42.Nm grep
43.Bk -words
44.Op Fl abcdDEFGHhIiLlmnOopqRSsUVvwxz
45.Op Fl A Ar num
46.Op Fl B Ar num
47.Op Fl C Ar num
48.Op Fl e Ar pattern
49.Op Fl f Ar file
50.Op Fl Fl binary-files= Ns Ar value
51.Op Fl Fl color Ns Op Cm = Ns Ar when
52.Op Fl Fl colour Ns Op Cm = Ns Ar when
53.Op Fl Fl context= Ns Ar num
54.Op Fl Fl label
55.Op Fl Fl line-buffered
56.Op Fl Fl null
57.Op Ar pattern
58.Op Ar
59.Ek
60.Sh DESCRIPTION
61The
62.Nm grep
63utility searches any given input files,
64selecting lines that match one or more patterns.
65By default, a pattern matches an input line if the regular expression
66(RE) in the pattern matches the input line
67without its trailing newline.
68An empty expression matches every line.
69Each input line that matches at least one of the patterns is written
70to the standard output.
71.Pp
72.Nm grep
73is used for simple patterns and
74basic regular expressions
75.Pq BREs ;
76.Nm egrep
77can handle extended regular expressions
78.Pq EREs .
79See
80.Xr re_format 7
81for more information on regular expressions.
82.Nm fgrep
83is quicker than both
84.Nm grep
85and
86.Nm egrep ,
87but can only handle fixed patterns
88(i.e., it does not interpret regular expressions).
89Patterns may consist of one or more lines,
90allowing any of the pattern lines to match a portion of the input.
91.Pp
92The following options are available:
93.Bl -tag -width indent
94.It Fl A Ar num , Fl Fl after-context= Ns Ar num
95Print
96.Ar num
97lines of trailing context after each match.
98See also the
99.Fl B
100and
101.Fl C
102options.
103.It Fl a , Fl Fl text
104Treat all files as ASCII text.
105Normally
106.Nm
107will simply print
108.Dq Binary file ... matches
109if files contain binary characters.
110Use of this option forces
111.Nm
112to output lines matching the specified pattern.
113.It Fl B Ar num , Fl Fl before-context= Ns Ar num
114Print
115.Ar num
116lines of leading context before each match.
117See also the
118.Fl A
119and
120.Fl C
121options.
122.It Fl b , Fl Fl byte-offset
123The offset in bytes of a matched pattern is
124displayed in front of the respective matched line.
125.It Fl C Ar num , Fl Fl context= Ns Ar num
126Print
127.Ar num
128lines of leading and trailing context surrounding each match.
129See also the
130.Fl A
131and
132.Fl B
133options.
134.It Fl c , Fl Fl count
135Only a count of selected lines is written to standard output.
136.It Fl Fl colour= Ns Oo Ar when Oc , Fl Fl color= Ns Oo Ar when Oc
137Mark up the matching text with the expression stored in the
138.Ev GREP_COLOR
139environment variable.
140The possible values of
141.Ar when
142are
143.Dq Cm never ,
144.Dq Cm always
145and
146.Dq Cm auto .
147.It Fl D Ar action , Fl Fl devices= Ns Ar action
148Specify the demanded
149.Ar action
150for devices, FIFOs and sockets.
151The default
152.Ar action
153is
154.Dq Cm read ,
155which means, that they are read as if they were normal files.
156If the
157.Ar action
158is set to
159.Dq Cm skip ,
160devices are silently skipped.
161.It Fl d Ar action , Fl Fl directories= Ns Ar action
162Specify the demanded
163.Ar action
164for directories.
165It is
166.Dq Cm read
167by default, which means that the directories
168are read in the same manner as normal files.
169Other possible values are
170.Dq Cm skip
171to silently ignore the directories, and
172.Dq Cm recurse
173to read them recursively, which has the same effect as the
174.Fl R
175and
176.Fl r
177option.
178.It Fl E , Fl Fl extended-regexp
179Interpret
180.Ar pattern
181as an extended regular expression
182(i.e., force
183.Nm grep
184to behave as
185.Nm egrep ) .
186.It Fl e Ar pattern , Fl Fl regexp= Ns Ar pattern
187Specify a
188.Ar pattern
189used during the search of the input:
190an input line is selected if it matches any of the specified patterns.
191This option is most useful when multiple
192.Fl e
193options are used to specify multiple patterns,
194or when a
195.Ar pattern
196begins with a dash
197.Pq Sq - .
198.It Fl Fl exclude Ar pattern
199If specified, it excludes files matching the given
200filename
201.Ar pattern
202from the search.
203Note that
204.Fl Fl exclude
205and
206.Fl Fl include
207patterns are processed in the order given.
208If a name matches multiple patterns, the latest matching rule wins.
209If no
210.Fl Fl include
211pattern is specified, all files are searched that are
212not excluded.
213Patterns are matched to the full path specified,
214not only to the filename component.
215.It Fl Fl exclude-dir Ar pattern
216If
217.Fl R
218is specified, it excludes directories matching the
219given filename
220.Ar pattern
221from the search.
222Note that
223.Fl Fl exclude-dir
224and
225.Fl Fl include-dir
226patterns are processed in the order given.
227If a name matches multiple patterns, the latest matching rule wins.
228If no
229.Fl Fl include-dir
230pattern is specified, all directories are searched that are
231not excluded.
232.It Fl F , Fl Fl fixed-strings
233Interpret
234.Ar pattern
235as a set of fixed strings
236(i.e., force
237.Nm grep
238to behave as
239.Nm fgrep ) .
240.It Fl f Ar file , Fl Fl file= Ns Ar file
241Read one or more newline separated patterns from
242.Ar file .
243Empty pattern lines match every input line.
244Newlines are not considered part of a pattern.
245If
246.Ar file
247is empty, nothing is matched.
248.It Fl G , Fl Fl basic-regexp
249Interpret
250.Ar pattern
251as a basic regular expression
252(i.e., force
253.Nm grep
254to behave as traditional
255.Nm grep ) .
256.It Fl H
257Always print filename headers with output lines.
258.It Fl h , Fl Fl no-filename
259Never print filename headers
260.Pq i.e., filenames
261with output lines.
262.It Fl Fl help
263Print a brief help message.
264.It Fl I
265Ignore binary files.
266This option is equivalent to the
267.Dq Fl Fl binary-files= Ns Cm without-match
268option.
269.It Fl i , Fl Fl ignore-case
270Perform case insensitive matching.
271By default,
272.Nm grep
273is case sensitive.
274.It Fl Fl include Ar pattern
275If specified, only files matching the given filename
276.Ar pattern
277are searched.
278Note that
279.Fl Fl include
280and
281.Fl Fl exclude
282patterns are processed in the order given.
283If a name matches multiple patterns, the latest matching rule wins.
284Patterns are matched to the full path specified,
285not only to the filename component.
286.It Fl Fl include-dir Ar pattern
287If
288.Fl R
289is specified, only directories matching the given filename
290.Ar pattern
291are searched.
292Note that
293.Fl Fl include-dir
294and
295.Fl Fl exclude-dir
296patterns are processed in the order given.
297If a name matches multiple patterns, the latest matching rule wins.
298.It Fl L , Fl Fl files-without-match
299Only the names of files not containing selected lines are written to
300standard output.
301Pathnames are listed once per file searched.
302If the standard input is searched, the string
303.Dq (standard input)
304is written unless a
305.Fl Fl label
306is specified.
307.It Fl l , Fl Fl files-with-matches
308Only the names of files containing selected lines are written to
309standard output.
310.Nm grep
311will only search a file until a match has been found,
312making searches potentially less expensive.
313Pathnames are listed once per file searched.
314If the standard input is searched, the string
315.Dq (standard input)
316is written unless a
317.Fl Fl label
318is specified.
319.It Fl Fl label
320Label to use in place of
321.Dq (standard input)
322for a file name where a file name would normally be printed.
323This option applies to
324.Fl H ,
325.Fl L ,
326and
327.Fl l .
328.It Fl Fl mmap
329Use
330.Xr mmap 2
331instead of
332.Xr read 2
333to read input, which can result in better performance under some
334circumstances but can cause undefined behaviour.
335.It Fl m Ar num , Fl Fl max-count= Ns Ar num
336Stop reading the file after
337.Ar num
338matches.
339.It Fl n , Fl Fl line-number
340Each output line is preceded by its relative line number in the file,
341starting at line 1.
342The line number counter is reset for each file processed.
343This option is ignored if
344.Fl c ,
345.Fl L ,
346.Fl l ,
347or
348.Fl q
349is
350specified.
351.It Fl Fl null
352Prints a zero-byte after the file name.
353.It Fl O
354If
355.Fl R
356is specified, follow symbolic links only if they were explicitly listed
357on the command line.
358The default is not to follow symbolic links.
359.It Fl o , Fl Fl only-matching
360Prints only the matching part of the lines.
361.It Fl p
362If
363.Fl R
364is specified, no symbolic links are followed.
365This is the default.
366.It Fl q , Fl Fl quiet , Fl Fl silent
367Quiet mode:
368suppress normal output.
369.Nm grep
370will only search a file until a match has been found,
371making searches potentially less expensive.
372.It Fl R , Fl r , Fl Fl recursive
373Recursively search subdirectories listed.
374(i.e., force
375.Nm grep
376to behave as
377.Nm rgrep ) .
378.It Fl S
379If
380.Fl R
381is specified, all symbolic links are followed.
382The default is not to follow symbolic links.
383.It Fl s , Fl Fl no-messages
384Silent mode.
385Nonexistent and unreadable files are ignored
386(i.e., their error messages are suppressed).
387.It Fl U , Fl Fl binary
388Search binary files, but do not attempt to print them.
389.It Fl u
390This option has no effect and is provided only for compatibility with GNU grep.
391.It Fl V , Fl Fl version
392Display version information and exit.
393.It Fl v , Fl Fl invert-match
394Selected lines are those
395.Em not
396matching any of the specified patterns.
397.It Fl w , Fl Fl word-regexp
398The expression is searched for as a word (as if surrounded by
399.Sq [[:<:]]
400and
401.Sq [[:>:]] ;
402see
403.Xr re_format 7 ) .
404This option has no effect if
405.Fl x
406is also specified.
407.It Fl x , Fl Fl line-regexp
408Only input lines selected against an entire fixed string or regular
409expression are considered to be matching lines.
410.It Fl y
411Equivalent to
412.Fl i .
413Obsoleted.
414.It Fl z , Fl Fl null-data
415Treat input and output data as sequences of lines terminated by a
416zero-byte instead of a newline.
417.It Fl Fl binary-files= Ns Ar value
418Controls searching and printing of binary files.
419Options are:
420.Bl -tag -compact -width "binary (default)"
421.It Cm binary No (default)
422Search binary files but do not print them.
423.It Cm without-match
424Do not search binary files.
425.It Cm text
426Treat all files as text.
427.El
428.It Fl Fl line-buffered
429Force output to be line buffered.
430By default, output is line buffered when standard output is a terminal
431and block buffered otherwise.
432.El
433.Pp
434If no file arguments are specified, the standard input is used.
435Additionally,
436.Dq Cm -
437may be used in place of a file name, anywhere that a file name is accepted, to
438read from standard input.
439This includes both
440.Fl f
441and file arguments.
442.Sh ENVIRONMENT
443The following environment variables affect the execution of
444.Nm :
445.Bl -tag -width "GREP_OPTIONS"
446.It Ev GREP_COLOR
447This variable specifies the color used to highlight matched (non-empty) text.
448.It Ev GREP_OPTIONS
449This variable specifies default options
450to be placed in front of any explicit options.
451It may cause problems with portable scripts.
452.It Ev TERM
453This variable specifies the type name of the terminal, console or display-device
454type to be used.
455See
456.Xr term 7 .
457.El
458.Sh EXIT STATUS
459The
460.Nm grep
461utility exits with one of the following values:
462.Pp
463.Bl -tag -width flag -compact
464.It Li 0
465One or more lines were selected.
466.It Li 1
467No lines were selected.
468.It Li \*(Gt1
469An error occurred.
470.El
471.Sh EXAMPLES
472.Bl -dash
473.It
474Find all occurrences of the pattern
475.Sq patricia
476in a file:
477.Pp
478.Dl $ grep 'patricia' myfile
479.It
480Same as above but looking only for complete words:
481.Pp
482.Dl $ grep -w 'patricia' myfile
483.It
484Count occurrences of the exact pattern
485.Sq FOO
486:
487.Pp
488.Dl $ grep -c FOO myfile
489.It
490Same as above but ignoring case:
491.Pp
492.Dl $ grep -c -i FOO myfile
493.It
494Find all occurrences of the pattern
495.Ql .Pp
496at the beginning of a line:
497.Pp
498.Dl $ grep '^\e.Pp' myfile
499.Pp
500The apostrophes ensure the entire expression is evaluated by
501.Nm grep
502instead of by the user's shell.
503The caret
504.Ql ^
505matches the null string at the beginning of a line,
506and the
507.Ql \e
508escapes the
509.Ql \&. ,
510which would otherwise match any character.
511.It
512Find all lines in a file which do not contain the words
513.Sq foo
514or
515.Sq bar :
516.Pp
517.Dl $ grep -v -e 'foo' -e 'bar' myfile
518.It
519Peruse the file
520.Sq calendar
521looking for either 19, 20, or 25 using extended regular expressions:
522.Pp
523.Dl $ egrep '19|20|25' calendar
524.It
525Show matching lines and the name of the
526.Sq *.h
527files which contain the pattern
528.Sq FIXME .
529Do the search recursively from the
530.Pa /usr/src/sys/arm
531directory
532.Pp
533.Dl $ grep -H -R FIXME --include="*.h" /usr/src/sys/arm/
534.It
535Same as above but show only the name of the matching file:
536.Pp
537.Dl $ grep -l -R FIXME --include="*.h" /usr/src/sys/arm/
538.It
539Show lines containing the text
540.Sq foo .
541The matching part of the output is colored and every line is prefixed with
542the line number and the offset in the file for those lines that matched.
543.Pp
544.Dl $ grep -b --colour -n foo myfile
545.It
546Show lines that match the extended regular expression patterns read from the
547standard input:
548.Pp
549.Dl $ echo -e 'Free\enBSD\enAll.*reserved' | grep -E -f - myfile
550.It
551Show lines from the output of the
552.Xr pciconf 8
553command matching the specified extended regular expression along with
554three lines of leading context and one line of trailing context:
555.Pp
556.Dl $ pciconf -lv | grep -B3 -A1 -E 'class.*=.*storage'
557.It
558Suppress any output and use the exit status to show an appropriate message:
559.Pp
560.Dl $ grep -q foo myfile && echo File matches
561.El
562.Sh SEE ALSO
563.Xr ed 1 ,
564.Xr ex 1 ,
565.Xr sed 1 ,
566.Xr zgrep 1 ,
567.Xr re_format 7
568.Sh STANDARDS
569The
570.Nm
571utility is compliant with the
572.St -p1003.1-2008
573specification.
574.Pp
575The flags
576.Op Fl AaBbCDdGHhILmopRSUVw
577are extensions to that specification, and the behaviour of the
578.Fl f
579flag when used with an empty pattern file is left undefined.
580.Pp
581All long options are provided for compatibility with
582GNU versions of this utility.
583.Pp
584Historic versions of the
585.Nm grep
586utility also supported the flags
587.Op Fl ruy .
588This implementation supports those options;
589however, their use is strongly discouraged.
590.Sh HISTORY
591The
592.Nm grep
593command first appeared in
594.At v6 .
595