xref: /illumos-gate/usr/src/man/man1/grep.1 (revision 2e5ac464a5683c5bf11439ccad74c9f279051820)
1.\"
2.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3.\" permission to reproduce portions of its copyrighted documentation.
4.\" Original documentation from The Open Group can be obtained online at
5.\" http://www.opengroup.org/bookstore/.
6.\"
7.\" The Institute of Electrical and Electronics Engineers and The Open
8.\" Group, have given us permission to reprint portions of their
9.\" documentation.
10.\"
11.\" In the following statement, the phrase ``this text'' refers to portions
12.\" of the system documentation.
13.\"
14.\" Portions of this text are reprinted and reproduced in electronic form
15.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16.\" Standard for Information Technology -- Portable Operating System
17.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20.\" between these versions and the original IEEE and The Open Group
21.\" Standard, the original IEEE and The Open Group Standard is the referee
22.\" document.  The original Standard can be obtained online at
23.\" http://www.opengroup.org/unix/online.html.
24.\"
25.\" This notice shall appear on any product containing this material.
26.\"
27.\" The contents of this file are subject to the terms of the
28.\" Common Development and Distribution License (the "License").
29.\" You may not use this file except in compliance with the License.
30.\"
31.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32.\" or http://www.opensolaris.org/os/licensing.
33.\" See the License for the specific language governing permissions
34.\" and limitations under the License.
35.\"
36.\" When distributing Covered Code, include this CDDL HEADER in each
37.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38.\" If applicable, add the following below this CDDL HEADER, with the
39.\" fields enclosed by brackets "[]" replaced with your own identifying
40.\" information: Portions Copyright [yyyy] [name of copyright owner]
41.\"
42.\"
43.\" Copyright 1989 AT&T
44.\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
45.\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved
46.\" Copyright 2018 Nexenta Systems, Inc.
47.\" Copyright 2020 Oxide Computer Company
48.\"
49.Dd August 02, 2020
50.Dt GREP 1
51.Os
52.Sh NAME
53.Nm grep ,
54.Nm egrep ,
55.Nm fgrep
56.Nd search a file for a pattern
57.Sh SYNOPSIS
58.Nm grep
59.Op Fl E Ns | Ns Fl F
60.Op Fl bchHilLnrRsqvwx
61.Op Fl A Ar num
62.Op Fl B Ar num
63.Op Fl C Ar num Ns | Ns Fl Ns Ar num
64.Oo Fl e Ar pattern_list Oc Ns ...
65.Oo Fl f Ar pattern_file Oc Ns ...
66.Op Ar pattern_list
67.Oo Ar file Oc Ns ...
68.Sh DESCRIPTION
69The
70.Nm
71utility searches text files for a pattern and prints all lines that contain that
72pattern.
73If no files are specified,
74.Nm
75assumes standard input.
76Normally, each line found is copied to standard output.
77The file name is printed before each line found if there is more than one input
78file.
79.Pp
80.Nm
81handles patterns as basic regular expressions (BREs);
82.Nm egrep
83.Pq same as Nm Fl E
84handles patterns as extended regular expressions (EREs);
85.Nm fgrep
86.Pq same as Nm Fl F
87handles patterns as fixed strings.
88.Sh OPTIONS
89The following options are supported:
90.Bl -tag -width Ds
91.It Fl A Ar num
92Prints
93.Ar num
94input lines of context after each matching line.
95If there are multiple matching lines, their context lines are separated by a
96.Ql --
97delimiter line.
98.It Fl b
99Precedes each line by the block number on which it was found.
100This can be useful in locating block numbers by context (first block is 0).
101.It Fl B Ar num
102Prints
103.Ar num
104input lines of context before each matching line.
105If there are multiple matching lines, their context lines are separated by a
106.Ql --
107delimiter line.
108.It Fl c
109Prints only a count of the lines that contain the pattern.
110Overrides
111.Fl l
112and
113.Fl L .
114.It Fl C Ar num Ns \&, Fl Ns Ar num
115Prints
116.Ar num
117input lines of context before and
118.Ar number
119input lines of context after each matching line.
120If there are multiple matching lines, their context lines are separated by a
121.Ql --
122delimiter line.
123.It Fl e Ar pattern_list
124Specifies one or more patterns to be used during the search for input.
125Patterns in
126.Ar pattern_list
127must be separated by a NEWLINE character.
128A null pattern can be specified by two adjacent newline characters in
129.Ar pattern_list .
130Unless the
131.Fl E
132or
133.Fl F
134option is also specified, each pattern is treated as a BRE, as described in
135.Xr regex 5 .
136.It Fl E
137Matches using extended regular expressions.
138Treats each pattern specified as an ERE, as described in
139.Xr regex 5 .
140If any entire ERE pattern matches an input line, the line is matched.
141A null ERE matches every line.
142.It Fl f Ar pattern_file
143Reads one or more patterns from the file named by the path name
144.Ar pattern_file .
145Patterns in
146.Ar pattern_file
147are terminated by a NEWLINE character.
148A null pattern can be specified by an empty line in
149.Ar pattern_file .
150Unless the
151.Fl E
152or
153.Fl F
154option is also specified, each pattern is treated as a BRE, as described in
155.Xr regex 5 .
156.It Fl F
157Matches using fixed strings.
158Treats each pattern specified as a string instead of a regular expression.
159If an input line contains any of the patterns as a contiguous sequence of bytes,
160the line is matched.
161A null string matches every line.
162.It Fl h
163Prevents the name of the file containing the matching line from being prepended
164to that line.
165Used when searching multiple files.
166.It Fl H
167Precedes each line by the name of the file containing the matching line.
168.It Fl i
169Ignores upper/lower case distinction during comparisons.
170.It Fl l
171Prints only the names of files with matching lines, separated by NEWLINE
172characters.
173Does not repeat the names of files when the pattern is found more than once.
174If both
175.Fl l
176and
177.Fl L
178are specified, only the last specified takes effect.
179Overrides
180.Fl H .
181.It Fl L
182The opposite of the
183.Fl l
184flag.
185Prints only the names of files without matching lines.
186If both
187.Fl l
188and
189.Fl L
190are specified, only the last specified takes effect.
191Overrides
192.Fl H .
193.It Fl n
194Precedes each line by its line number in the file (first line is 1).
195.It Fl q
196Quiet.
197Does not write anything to the standard output, regardless of matching lines.
198Exits with zero status if an input line is selected.
199Overrides
200.Fl c ,
201.Fl l ,
202and
203.Fl L .
204.It Fl r
205Read all files under each directory, recursively.
206Follow symbolic links on the command line, but skip symlinks that are
207encountered recursively.
208If file is a device, FIFO, or socket, skip it.
209.It Fl R
210Read all files under each directory, recursively, following all symbolic links.
211.It Fl s
212Suppresses error messages about nonexistent or unreadable files.
213.It Fl v
214Prints all lines except those that contain the pattern.
215.It Fl w
216Searches for the expression as a word as if surrounded by
217.Ql \e<
218and
219.Ql \e> .
220.It Fl x
221Considers only input lines that use all characters in the line to match an
222entire fixed string or regular expression to be matching lines.
223.El
224.Sh OPERANDS
225The following operands are supported:
226.Bl -tag -width Ds
227.It Ar file
228A path name of a file to be searched for the patterns.
229If no
230.Ar file
231operands are specified, the standard input is used.
232.It Ar pattern_list
233Specifies one or more patterns to be used during the search for input.
234This operand is treated as if it were specified as
235.Fl e Ar pattern_list .
236Should not be specified if either
237.Fl e
238or
239.Fl f
240is specified.
241.El
242.Sh USAGE
243Be careful using the characters
244.Ql $ ,
245.Ql * ,
246.Ql \&[ ,
247.Ql ^ ,
248.Ql | ,
249.Ql \&( ,
250.Ql \&) ,
251and
252.Ql \e
253in the
254.Ar pattern_list
255because they are also meaningful to the shell.
256It is safest to enclose the entire
257.Ar pattern_list
258in single quotes:
259.Li '...' .
260.Pp
261The
262.Fl e Ar pattern
263option has the same effect as the
264.Ar pattern
265operand, but is useful when
266.Ar pattern
267begins with the hyphen delimiter.
268It is also useful when it is more convenient to provide multiple patterns as
269separate arguments.
270.Pp
271Multiple
272.Fl e
273and
274.Fl f
275options are accepted and
276.Nm
277uses all of the patterns it is given while matching input text lines.
278Notice that the order of evaluation is not specified.
279If an implementation finds a null string as a pattern, it is allowed to use that
280pattern first, matching every line, and effectively ignore any other patterns.
281.Pp
282The
283.Fl q
284option provides a means of easily determining whether or not a pattern (or
285string) exists in a group of files.
286When searching several files, it provides a performance improvement (because it
287can quit as soon as it finds the first match) and requires less care by the user
288in choosing the set of files to supply as arguments (because it exits zero if it
289finds a match even if
290.Nm
291detected an access or read error on earlier file operands).
292.Ss Large File Behavior
293See
294.Xr largefile 5
295for the description of the behavior of
296.Nm
297when encountering files greater than or equal to 2 Gbyte (2^31 bytes).
298.Sh EXIT STATUS
299The following exit values are returned:
300.Bl -tag -width Ds
301.It Sy 0
302One or more matches were found.
303.It Sy 1
304No matches were found.
305.It Sy 2
306Syntax errors or inaccessible files (even if matches were found).
307.El
308.Sh EXAMPLES
309.Bl -tag -width Ds
310.It Sy Example 1 No Finding All Uses of a Word
311To find all uses of the word
312.Ql Posix
313(in any case) in the file
314.Pa text.mm ,
315and write with line numbers:
316.Bd -literal
317$ grep -i -n posix text.mm
318.Ed
319.It Sy Example 2 No Finding All Empty Lines
320To find all empty lines in the standard input:
321.Bd -literal
322$ grep ^$
323.Ed
324.Pp
325or
326.Bd -literal
327$ grep -v .
328.Ed
329.It Sy Example 3 No Finding Lines Containing Strings
330All of the following commands print all lines containing strings
331.Ql abc
332or
333.Ql def
334or both:
335.Bd -literal
336$ grep 'abc
337def'
338$ grep -e 'abc
339def'
340$ grep -e 'abc' -e 'def'
341$ grep -E 'abc|def'
342$ grep -E -e 'abc|def'
343$ grep -E -e 'abc' -e 'def'
344$ grep -E 'abc
345def'
346$ grep -E -e 'abc
347def'
348$ grep -F -e 'abc' -e 'def'
349$ grep -F 'abc
350def'
351$ grep -F -e 'abc
352def'
353.Ed
354.It Sy Example 4 No Finding Lines with Matching Strings
355Both of the following commands print all lines matching exactly
356.Ql abc
357or
358.Ql def :
359.Bd -literal
360$ grep -E '^abc$
361^def$'
362$ grep -F -x 'abc
363def'
364.Ed
365.El
366.Sh ENVIRONMENT VARIABLES
367See
368.Xr environ 5
369for descriptions of the following environment variables that affect the
370execution of
371.Nm :
372.Ev LANG , LC_ALL , LC_COLLATE , LC_CTYPE , LC_MESSAGES ,
373and
374.Ev NLSPATH .
375.Sh CODE SET INDEPENDENCE
376.Sy Enabled
377.Sh INTERFACE STABILITY
378.Sy Committed
379.Sh SEE ALSO
380.Xr sed 1 ,
381.Xr sh 1 ,
382.Xr attributes 5 ,
383.Xr environ 5 ,
384.Xr largefile 5 ,
385.Xr regex 5 ,
386.Xr standards 5
387.Sh STANDARDS
388The
389.Nm
390utility is compliant with the
391.St -p1003.1-2008
392specification with the exception of
393.Fl s
394option being the same as
395.Fl q
396in current implementation for historic reasons.
397The flags
398.Op Fl AbBChHrRw
399are extensions to that specification.
400.Sh NOTES
401The results are unspecified if input files contain lines longer than
402.Dv LINE_MAX
403bytes or contain binary data.
404.Dv LINE_MAX
405is defined in
406.In limits.h .
407.Pp
408Portable applications should use
409.Nm Fl E
410and
411.Nm Fl F
412instead of
413.Nm egrep
414and
415.Nm fgrep ,
416respectively.
417.Sh HISTORY
418The
419.Nm grep
420command first appeared in
421.At v6 .
422.Pp
423In the past
424.Pa /usr/bin/grep ,
425.Pa /usr/bin/egrep ,
426and
427.Pa /usr/bin/fgrep
428were separate implementations, and were not standard conforming, with standard
429conforming ones installed as
430.Pa /usr/xpg4/bin/grep ,
431.Pa /usr/xpg4/bin/egrep ,
432and
433.Pa /usr/xpg4/bin/fgrep ,
434respectively.
435Now all non-conforming implementations are removed, and the ones previously
436found in
437.Pa /usr/xpg4/bin
438are installed in
439.Pa /usr/bin .
440