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