xref: /illumos-gate/usr/src/man/man1/grep.1 (revision 8459c777fc1aaabb2f7dad05de1313aa169417cd)
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 13, 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 bchHilLnorRsqvwx
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.Op Fl -label Ns = Ns Ar name
65.Oo Fl e Ar pattern_list Oc Ns ...
66.Oo Fl f Ar pattern_file Oc Ns ...
67.Op Ar pattern_list
68.Oo Ar file Oc Ns ...
69.Sh DESCRIPTION
70The
71.Nm
72utility searches text files for a pattern and prints all lines that contain that
73pattern.
74If no files are specified,
75.Nm
76assumes standard input.
77Normally, each line found is copied to standard output.
78The file name is printed before each line found if there is more than one input
79file.
80.Pp
81.Nm
82handles patterns as basic regular expressions (BREs);
83.Nm egrep
84.Pq same as Nm Fl E
85handles patterns as extended regular expressions (EREs);
86.Nm fgrep
87.Pq same as Nm Fl F
88handles patterns as fixed strings.
89.Sh OPTIONS
90The following options are supported:
91.Bl -tag -width Ds
92.It Fl A Ar num
93Prints
94.Ar num
95input lines of context after each matching line.
96If there are multiple matching lines, their context lines are separated by a
97.Ql --
98delimiter line.
99.It Fl b
100Precedes each line by the block number on which it was found.
101This can be useful in locating block numbers by context (first block is 0).
102.It Fl B Ar num
103Prints
104.Ar num
105input lines of context before each matching line.
106If there are multiple matching lines, their context lines are separated by a
107.Ql --
108delimiter line.
109.It Fl c
110Prints only a count of the lines that contain the pattern.
111Overrides
112.Fl l
113and
114.Fl L .
115.It Fl C Ar num Ns \&, Fl Ns Ar num
116Prints
117.Ar num
118input lines of context before and
119.Ar number
120input lines of context after each matching line.
121If there are multiple matching lines, their context lines are separated by a
122.Ql --
123delimiter line.
124.It Fl e Ar pattern_list
125Specifies one or more patterns to be used during the search for input.
126Patterns in
127.Ar pattern_list
128must be separated by a NEWLINE character.
129A null pattern can be specified by two adjacent newline characters in
130.Ar pattern_list .
131Unless the
132.Fl E
133or
134.Fl F
135option is also specified, each pattern is treated as a BRE, as described in
136.Xr regex 5 .
137.It Fl E
138Matches using extended regular expressions.
139Treats each pattern specified as an ERE, as described in
140.Xr regex 5 .
141If any entire ERE pattern matches an input line, the line is matched.
142A null ERE matches every line.
143.It Fl f Ar pattern_file
144Reads one or more patterns from the file named by the path name
145.Ar pattern_file .
146Patterns in
147.Ar pattern_file
148are terminated by a NEWLINE character.
149A null pattern can be specified by an empty line in
150.Ar pattern_file .
151Unless the
152.Fl E
153or
154.Fl F
155option is also specified, each pattern is treated as a BRE, as described in
156.Xr regex 5 .
157.It Fl F
158Matches using fixed strings.
159Treats each pattern specified as a string instead of a regular expression.
160If an input line contains any of the patterns as a contiguous sequence of bytes,
161the line is matched.
162A null string matches every line.
163.It Fl h
164Prevents the name of the file containing the matching line from being prepended
165to that line.
166Used when searching multiple files.
167.It Fl H
168Precedes each line by the name of the file containing the matching line.
169.It Fl i
170Ignores upper/lower case distinction during comparisons.
171.It Fl -label Ns = Ns Ar name
172When the name of the matching file is printed
173.Pq Fl H ,
174instead of printing the string
175.Ql (standard input)
176the string
177.Ar name
178is printed instead.
179See
180.Sx Example 5 .
181.It Fl l
182Prints only the names of files with matching lines, separated by NEWLINE
183characters.
184Does not repeat the names of files when the pattern is found more than once.
185If both
186.Fl l
187and
188.Fl L
189are specified, only the last specified takes effect.
190Overrides
191.Fl H .
192.It Fl L
193The opposite of the
194.Fl l
195flag.
196Prints only the names of files without matching lines.
197If both
198.Fl l
199and
200.Fl L
201are specified, only the last specified takes effect.
202Overrides
203.Fl H .
204.It Fl n
205Precedes each line by its line number in the file (first line is 1).
206.It Fl o
207Prints only the matching part of a line.
208If a pattern appears more than once in a line, it will be matched and
209printed multiple times.
210.Pp
211The
212.Fl o
213option is overridden when any of the
214.Fl l ,
215.Fl L ,
216or
217.Fl c
218options are specified.
219When the
220.Fl o
221option is specified, all context options are ignored.
222The
223.Fl o
224and
225.Fl v
226options are not supported together at this time.
227.It Fl q
228Quiet.
229Does not write anything to the standard output, regardless of matching lines.
230Exits with zero status if an input line is selected.
231Overrides
232.Fl c ,
233.Fl l ,
234and
235.Fl L .
236.It Fl r
237Read all files under each directory, recursively.
238Follow symbolic links on the command line, but skip symlinks that are
239encountered recursively.
240If file is a device, FIFO, or socket, skip it.
241.It Fl R
242Read all files under each directory, recursively, following all symbolic links.
243.It Fl s
244Suppresses error messages about nonexistent or unreadable files.
245.It Fl v
246Prints all lines except those that contain the pattern.
247.It Fl w
248Searches for the expression as a word as if surrounded by
249.Ql \e<
250and
251.Ql \e> .
252.It Fl x
253Considers only input lines that use all characters in the line to match an
254entire fixed string or regular expression to be matching lines.
255.El
256.Sh OPERANDS
257The following operands are supported:
258.Bl -tag -width Ds
259.It Ar file
260A path name of a file to be searched for the patterns.
261If no
262.Ar file
263operands are specified, the standard input is used.
264.It Ar pattern_list
265Specifies one or more patterns to be used during the search for input.
266This operand is treated as if it were specified as
267.Fl e Ar pattern_list .
268Should not be specified if either
269.Fl e
270or
271.Fl f
272is specified.
273.El
274.Sh USAGE
275Be careful using the characters
276.Ql $ ,
277.Ql * ,
278.Ql \&[ ,
279.Ql ^ ,
280.Ql | ,
281.Ql \&( ,
282.Ql \&) ,
283and
284.Ql \e
285in the
286.Ar pattern_list
287because they are also meaningful to the shell.
288It is safest to enclose the entire
289.Ar pattern_list
290in single quotes:
291.Li '...' .
292.Pp
293The
294.Fl e Ar pattern
295option has the same effect as the
296.Ar pattern
297operand, but is useful when
298.Ar pattern
299begins with the hyphen delimiter.
300It is also useful when it is more convenient to provide multiple patterns as
301separate arguments.
302.Pp
303Multiple
304.Fl e
305and
306.Fl f
307options are accepted and
308.Nm
309uses all of the patterns it is given while matching input text lines.
310Notice that the order of evaluation is not specified.
311If an implementation finds a null string as a pattern, it is allowed to use that
312pattern first, matching every line, and effectively ignore any other patterns.
313.Pp
314The
315.Fl q
316option provides a means of easily determining whether or not a pattern (or
317string) exists in a group of files.
318When searching several files, it provides a performance improvement (because it
319can quit as soon as it finds the first match) and requires less care by the user
320in choosing the set of files to supply as arguments (because it exits zero if it
321finds a match even if
322.Nm
323detected an access or read error on earlier file operands).
324.Ss Large File Behavior
325See
326.Xr largefile 5
327for the description of the behavior of
328.Nm
329when encountering files greater than or equal to 2 Gbyte (2^31 bytes).
330.Sh EXIT STATUS
331The following exit values are returned:
332.Bl -tag -width Ds
333.It Sy 0
334One or more matches were found.
335.It Sy 1
336No matches were found.
337.It Sy 2
338Syntax errors or inaccessible files (even if matches were found).
339.El
340.Sh EXAMPLES
341.Bl -tag -width Ds
342.It Sy Example 1 No Finding All Uses of a Word
343To find all uses of the word
344.Ql Posix
345(in any case) in the file
346.Pa text.mm ,
347and write with line numbers:
348.Bd -literal
349$ grep -i -n posix text.mm
350.Ed
351.It Sy Example 2 No Finding All Empty Lines
352To find all empty lines in the standard input:
353.Bd -literal
354$ grep ^$
355.Ed
356.Pp
357or
358.Bd -literal
359$ grep -v .
360.Ed
361.It Sy Example 3 No Finding Lines Containing Strings
362All of the following commands print all lines containing strings
363.Ql abc
364or
365.Ql def
366or both:
367.Bd -literal
368$ grep 'abc
369def'
370$ grep -e 'abc
371def'
372$ grep -e 'abc' -e 'def'
373$ grep -E 'abc|def'
374$ grep -E -e 'abc|def'
375$ grep -E -e 'abc' -e 'def'
376$ grep -E 'abc
377def'
378$ grep -E -e 'abc
379def'
380$ grep -F -e 'abc' -e 'def'
381$ grep -F 'abc
382def'
383$ grep -F -e 'abc
384def'
385.Ed
386.It Sy Example 4 No Finding Lines with Matching Strings
387Both of the following commands print all lines matching exactly
388.Ql abc
389or
390.Ql def :
391.Bd -literal
392$ grep -E '^abc$
393^def$'
394$ grep -F -x 'abc
395def'
396.Ed
397.It Sy Example 5 No Using Fl -label
398When piping standard input into
399.Nm ,
400as part of a pipeline, occasionally it can be useful override the file
401name
402.Ql (standard input)
403with something from the pipeline.
404This would output each matching line instead with the name of the input
405file.
406.Bd -literal
407$ for f in *.gz; do
408> gzcat $f | grep -H --label=$f foo
409> done
410.Ed
411.El
412.Sh ENVIRONMENT VARIABLES
413See
414.Xr environ 5
415for descriptions of the following environment variables that affect the
416execution of
417.Nm :
418.Ev LANG , LC_ALL , LC_COLLATE , LC_CTYPE , LC_MESSAGES ,
419and
420.Ev NLSPATH .
421.Sh CODE SET INDEPENDENCE
422.Sy Enabled
423.Sh INTERFACE STABILITY
424.Sy Committed
425.Sh SEE ALSO
426.Xr sed 1 ,
427.Xr sh 1 ,
428.Xr attributes 5 ,
429.Xr environ 5 ,
430.Xr largefile 5 ,
431.Xr regex 5 ,
432.Xr standards 5
433.Sh STANDARDS
434The
435.Nm
436utility is compliant with the
437.St -p1003.1-2008
438specification with the exception of
439.Fl s
440option being the same as
441.Fl q
442in current implementation for historic reasons.
443The flags
444.Op Fl AbBChHrRw
445are extensions to that specification.
446.Sh NOTES
447The results are unspecified if input files contain lines longer than
448.Dv LINE_MAX
449bytes or contain binary data.
450.Dv LINE_MAX
451is defined in
452.In limits.h .
453.Pp
454Portable applications should use
455.Nm Fl E
456and
457.Nm Fl F
458instead of
459.Nm egrep
460and
461.Nm fgrep ,
462respectively.
463.Sh HISTORY
464The
465.Nm grep
466command first appeared in
467.At v6 .
468.Pp
469In the past
470.Pa /usr/bin/grep ,
471.Pa /usr/bin/egrep ,
472and
473.Pa /usr/bin/fgrep
474were separate implementations, and were not standard conforming, with standard
475conforming ones installed as
476.Pa /usr/xpg4/bin/grep ,
477.Pa /usr/xpg4/bin/egrep ,
478and
479.Pa /usr/xpg4/bin/fgrep ,
480respectively.
481Now all non-conforming implementations are removed, and the ones previously
482found in
483.Pa /usr/xpg4/bin
484are installed in
485.Pa /usr/bin .
486