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