Print this page
4701 would like grep context options (-A, -B, -C)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/man/man1/grep.1
+++ new/usr/src/man/man1/grep.1
1 1 '\" te
2 2 .\" Copyright 2012 Nexenta Systems, Inc. All rights reserved.
3 3 .\" Copyright 1989 AT&T
4 4 .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved
5 5 .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
6 6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
7 7 .\" http://www.opengroup.org/bookstore/.
8 8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 9 .\" This notice shall appear on any product containing this material.
10 10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
11 11 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
12 12 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 13 .TH GREP 1 "May 3, 2013"
14 14 .SH NAME
15 15 grep \- search a file for a pattern
16 16 .SH SYNOPSIS
17 17 .LP
18 18 .nf
19 19 \fB/usr/bin/grep\fR [\fB-c\fR | \fB-l\fR |\fB-q\fR] [\fB-r\fR | \fB-R\fR] [\fB-bHhinsvw\fR]
20 20 \fIlimited-regular-expression\fR [\fIfilename\fR]...
21 21 .fi
22 22
23 23 .LP
24 24 .nf
25 25 \fB/usr/xpg4/bin/grep\fR [\fB-E\fR | \fB-F\fR] [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-r\fR | \fB-R\fR]
26 26 [\fB-bHhinsvwx\fR] \fB-e\fR \fIpattern_list\fR... [\fB-f\fR \fIpattern_file\fR]...
27 27 [\fIfile\fR]...
28 28 .fi
29 29
30 30 .LP
31 31 .nf
32 32 \fB/usr/xpg4/bin/grep\fR [\fB-E\fR | \fB-F\fR] [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-r\fR | \fB-R\fR]
33 33 [\fB-bHhinsvwx\fR] [\fB-e\fR \fIpattern_list\fR]... \fB-f\fR \fIpattern_file\fR...
34 34 [\fIfile\fR]...
35 35 .fi
36 36
37 37 .LP
38 38 .nf
39 39 \fB/usr/xpg4/bin/grep\fR [\fB-E\fR | \fB-F\fR] [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-r\fR | \fB-R\fR]
40 40 [\fB-bHhinsvwx\fR] \fIpattern\fR [\fIfile\fR]...
41 41 .fi
42 42
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
43 43 .SH DESCRIPTION
44 44 .sp
45 45 .LP
46 46 The \fBgrep\fR utility searches text files for a pattern and prints all lines
47 47 that contain that pattern. It uses a compact non-deterministic algorithm.
48 48 .sp
49 49 .LP
50 50 Be careful using the characters \fB$\fR, \fB*\fR, \fB[\fR, \fB^\fR, \fB|\fR,
51 51 \fB(\fR, \fB)\fR, and \fB\e\fR in the \fIpattern_list\fR because they are also
52 52 meaningful to the shell. It is safest to enclose the entire \fIpattern_list\fR
53 -in single quotes \fBa\'\fR\&...\fBa\'\fR\&.
53 +in single quotes \fB\'\fR\&...\fB\'\fR\&.
54 54 .sp
55 55 .LP
56 56 If no files are specified, \fBgrep\fR assumes standard input. Normally, each
57 57 line found is copied to standard output. The file name is printed before each
58 58 line found if there is more than one input file.
59 59 .SS "/usr/bin/grep"
60 60 .sp
61 61 .LP
62 62 The \fB/usr/bin/grep\fR utility uses limited regular expressions like those
63 63 described on the \fBregexp\fR(5) manual page to match the patterns.
64 64 .SS "/usr/xpg4/bin/grep"
65 65 .sp
66 66 .LP
67 67 The options \fB-E\fR and \fB-F\fR affect the way \fB/usr/xpg4/bin/grep\fR
68 68 interprets \fIpattern_list\fR. If \fB-E\fR is specified,
69 69 \fB/usr/xpg4/bin/grep\fR interprets \fIpattern_list\fR as a full regular
70 70 expression (see \fB-E\fR for description). If \fB-F\fR is specified,
71 71 \fBgrep\fR interprets \fIpattern_list\fR as a fixed string. If neither are
72 72 specified, \fBgrep\fR interprets \fIpattern_list\fR as a basic regular
73 73 expression as described on \fBregex\fR(5) manual page.
74 74 .SH OPTIONS
75 75 .sp
76 76 .LP
77 77 The following options are supported for both \fB/usr/bin/grep\fR and
78 78 \fB/usr/xpg4/bin/grep\fR:
79 79 .sp
80 80 .ne 2
81 81 .na
82 82 \fB\fB-b\fR\fR
83 83 .ad
84 84 .RS 6n
85 85 Precedes each line by the block number on which it was found. This can be
86 86 useful in locating block numbers by context (first block is 0).
87 87 .RE
88 88
89 89 .sp
90 90 .ne 2
91 91 .na
92 92 \fB\fB-c\fR\fR
93 93 .ad
94 94 .RS 6n
95 95 Prints only a count of the lines that contain the pattern.
96 96 .RE
97 97
98 98 .sp
99 99 .ne 2
100 100 .na
101 101 \fB\fB-H\fR\fR
102 102 .ad
103 103 .RS 6n
104 104 Precedes each line by the name of the file containing the matching line.
105 105 .RE
106 106
107 107 .sp
108 108 .ne 2
109 109 .na
110 110 \fB\fB-h\fR\fR
111 111 .ad
112 112 .RS 6n
113 113 Prevents the name of the file containing the matching line from being prepended
114 114 to that line. Used when searching multiple files.
115 115 .RE
116 116
117 117 .sp
118 118 .ne 2
119 119 .na
120 120 \fB\fB-i\fR\fR
121 121 .ad
122 122 .RS 6n
123 123 Ignores upper/lower case distinction during comparisons.
124 124 .RE
125 125
126 126 .sp
127 127 .ne 2
128 128 .na
129 129 \fB\fB-l\fR\fR
130 130 .ad
131 131 .RS 6n
132 132 Prints only the names of files with matching lines, separated by NEWLINE
133 133 characters. Does not repeat the names of files when the pattern is found more
134 134 than once.
135 135 .RE
136 136
137 137 .sp
138 138 .ne 2
139 139 .na
140 140 \fB\fB-n\fR\fR
141 141 .ad
142 142 .RS 6n
143 143 Precedes each line by its line number in the file (first line is 1).
144 144 .RE
145 145
146 146 .sp
147 147 .ne 2
148 148 .na
149 149 \fB\fB-r\fR\fR
150 150 .ad
151 151 .RS 6n
152 152 Read all files under each directory, recursively. Follow symbolic links on
153 153 the command line, but skip symlinks that are encountered recursively. If file
154 154 is a device, FIFO, or socket, skip it.
155 155 .RE
156 156
157 157 .sp
158 158 .ne 2
159 159 .na
160 160 \fB\fB-R\fR\fR
161 161 .ad
162 162 .RS 6n
163 163 Read all files under each directory, recursively, following all symbolic links.
164 164 .RE
165 165
166 166 .sp
167 167 .ne 2
168 168 .na
169 169 \fB\fB-q\fR\fR
170 170 .ad
171 171 .RS 6n
172 172 Quiet. Does not write anything to the standard output, regardless of matching
173 173 lines. Exits with zero status if an input line is selected.
174 174 .RE
175 175
176 176 .sp
177 177 .ne 2
178 178 .na
179 179 \fB\fB-s\fR\fR
180 180 .ad
181 181 .RS 6n
182 182 Suppresses error messages about nonexistent or unreadable files.
183 183 .RE
184 184
185 185 .sp
186 186 .ne 2
187 187 .na
188 188 \fB\fB-v\fR\fR
189 189 .ad
190 190 .RS 6n
191 191 Prints all lines except those that contain the pattern.
192 192 .RE
193 193
194 194 .sp
195 195 .ne 2
196 196 .na
197 197 \fB\fB-w\fR\fR
198 198 .ad
199 199 .RS 6n
↓ open down ↓ |
136 lines elided |
↑ open up ↑ |
200 200 Searches for the expression as a word as if surrounded by \fB\e<\fR and
201 201 \fB\e>\fR\&.
202 202 .RE
203 203
204 204 .SS "/usr/xpg4/bin/grep"
205 205 .sp
206 206 .LP
207 207 The following options are supported for \fB/usr/xpg4/bin/grep\fR only:
208 208 .sp
209 209 .ne 2
210 +.na
211 +\fB\fB-A\fR \fInumber\fR\fR
212 +.ad
213 +.RS 19n
214 +Prints \fInumber\fR input lines of context after each matching line. If there
215 +are multiple matching lines, their context lines are separated by a \fB--\fR
216 +delimiter line.
217 +.RE
218 +
219 +.sp
220 +.ne 2
221 +.na
222 +\fB\fB-B\fR \fInumber\fR\fR
223 +.ad
224 +.RS 19n
225 +Prints \fInumber\fR input lines of context before each matching line. If there
226 +are multiple matching lines, their context lines are separated by a \fB--\fR
227 +delimiter line.
228 +.RE
229 +
230 +.sp
231 +.ne 2
232 +.na
233 +\fB\fB-\fR\fInumber\fR\fR
234 +.ad
235 +.br
236 +.na
237 +\fB\fB-C\fR \fInumber\fR\fR
238 +.ad
239 +.RS 19n
240 +Prints \fInumber\fR input lines of context before and \fInumber\fR input lines
241 +of context after each matching line. If there are multiple matching lines,
242 +their context lines are separated by a \fB--\fR delimiter line.
243 +.RE
244 +
245 +
246 +.sp
247 +.ne 2
210 248 .na
211 249 \fB\fB-e\fR \fIpattern_list\fR\fR
212 250 .ad
213 251 .RS 19n
214 252 Specifies one or more patterns to be used during the search for input. Patterns
215 253 in \fIpattern_list\fR must be separated by a NEWLINE character. A null pattern
216 254 can be specified by two adjacent newline characters in \fIpattern_list\fR.
217 255 Unless the \fB-E\fR or \fB-F\fR option is also specified, each pattern is
218 256 treated as a basic regular expression. Multiple \fB-e\fR and \fB-f\fR options
219 257 are accepted by \fBgrep\fR. All of the specified patterns are used when
220 258 matching lines, but the order of evaluation is unspecified.
221 259 .RE
222 260
223 261 .sp
224 262 .ne 2
225 263 .na
226 264 \fB\fB-E\fR\fR
227 265 .ad
228 266 .RS 19n
229 267 Matches using full regular expressions. Treats each pattern specified as a full
230 268 regular expression. If any entire full regular expression pattern matches an
231 269 input line, the line is matched. A null full regular expression matches every
232 270 line. Each pattern is interpreted as a full regular expression as described on
233 271 the \fBregex\fR(5) manual page, except for \fB\e(\fR and \fB\e)\fR, and
234 272 including:
235 273 .RS +4
236 274 .TP
237 275 1.
238 276 A full regular expression followed by \fB+\fR that matches one or more
239 277 occurrences of the full regular expression.
240 278 .RE
241 279 .RS +4
242 280 .TP
243 281 2.
244 282 A full regular expression followed by \fB?\fR that matches 0 or 1
245 283 occurrences of the full regular expression.
246 284 .RE
247 285 .RS +4
248 286 .TP
249 287 3.
250 288 Full regular expressions separated by | or by a new-line that match strings
251 289 that are matched by any of the expressions.
252 290 .RE
253 291 .RS +4
254 292 .TP
255 293 4.
256 294 A full regular expression that is enclosed in parentheses \fB()\fR for
257 295 grouping.
258 296 .RE
259 297 The order of precedence of operators is \fB[\|]\fR, then \fB*\|?\|+\fR, then
260 298 concatenation, then | and new-line.
261 299 .RE
262 300
263 301 .sp
264 302 .ne 2
265 303 .na
266 304 \fB\fB-f\fR \fIpattern_file\fR\fR
267 305 .ad
268 306 .RS 19n
269 307 Reads one or more patterns from the file named by the path name
270 308 \fIpattern_file\fR. Patterns in \fIpattern_file\fR are terminated by a NEWLINE
271 309 character. A null pattern can be specified by an empty line in
272 310 \fIpattern_file\fR. Unless the \fB-E\fR or \fB-F\fR option is also specified,
273 311 each pattern is treated as a basic regular expression.
274 312 .RE
275 313
276 314 .sp
277 315 .ne 2
278 316 .na
279 317 \fB\fB-F\fR\fR
280 318 .ad
281 319 .RS 19n
282 320 Matches using fixed strings. Treats each pattern specified as a string instead
283 321 of a regular expression. If an input line contains any of the patterns as a
284 322 contiguous sequence of bytes, the line is matched. A null string matches every
285 323 line. See \fBfgrep\fR(1) for more information.
286 324 .RE
287 325
288 326 .sp
289 327 .ne 2
290 328 .na
291 329 \fB\fB-x\fR\fR
292 330 .ad
293 331 .RS 19n
294 332 Considers only input lines that use all characters in the line to match an
295 333 entire fixed string or regular expression to be matching lines.
296 334 .RE
297 335
298 336 .SH OPERANDS
299 337 .sp
300 338 .LP
301 339 The following operands are supported:
302 340 .sp
303 341 .ne 2
304 342 .na
305 343 \fB\fIfile\fR\fR
306 344 .ad
307 345 .RS 8n
308 346 A path name of a file to be searched for the patterns. If no \fIfile\fR
309 347 operands are specified, the standard input is used.
310 348 .RE
311 349
312 350 .SS "/usr/bin/grep"
313 351 .sp
314 352 .ne 2
315 353 .na
316 354 \fB\fIpattern\fR\fR
317 355 .ad
318 356 .RS 11n
319 357 Specifies a pattern to be used during the search for input.
320 358 .RE
321 359
322 360 .SS "/usr/xpg4/bin/grep"
323 361 .sp
324 362 .ne 2
325 363 .na
326 364 \fB\fIpattern\fR\fR
327 365 .ad
328 366 .RS 11n
329 367 Specifies one or more patterns to be used during the search for input. This
330 368 operand is treated as if it were specified as \fB-e\fR \fIpattern_list\fR.
331 369 .RE
332 370
333 371 .SH USAGE
334 372 .sp
335 373 .LP
336 374 The \fB-e\fR \fIpattern_list\fR option has the same effect as the
337 375 \fIpattern_list\fR operand, but is useful when \fIpattern_list\fR begins with
338 376 the hyphen delimiter. It is also useful when it is more convenient to provide
339 377 multiple patterns as separate arguments.
340 378 .sp
341 379 .LP
342 380 Multiple \fB-e\fR and \fB-f\fR options are accepted and \fBgrep\fR uses all of
343 381 the patterns it is given while matching input text lines. Notice that the order
344 382 of evaluation is not specified. If an implementation finds a null string as a
345 383 pattern, it is allowed to use that pattern first, matching every line, and
346 384 effectively ignore any other patterns.
347 385 .sp
348 386 .LP
349 387 The \fB-q\fR option provides a means of easily determining whether or not a
350 388 pattern (or string) exists in a group of files. When searching several files,
351 389 it provides a performance improvement (because it can quit as soon as it finds
352 390 the first match) and requires less care by the user in choosing the set of
353 391 files to supply as arguments (because it exits zero if it finds a match even if
354 392 \fBgrep\fR detected an access or read error on earlier file operands).
355 393 .SS "Large File Behavior"
356 394 .sp
357 395 .LP
358 396 See \fBlargefile\fR(5) for the description of the behavior of \fBgrep\fR when
359 397 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
360 398 .SH EXAMPLES
361 399 .LP
362 400 \fBExample 1 \fRFinding All Uses of a Word
363 401 .sp
364 402 .LP
365 403 To find all uses of the word "\fBPosix\fR" (in any case) in the file
366 404 \fBtext.mm\fR, and write with line numbers:
367 405
368 406 .sp
369 407 .in +2
370 408 .nf
371 409 example% \fB/usr/bin/grep -i -n posix text.mm\fR
372 410 .fi
373 411 .in -2
374 412 .sp
375 413
376 414 .LP
377 415 \fBExample 2 \fRFinding All Empty Lines
378 416 .sp
379 417 .LP
380 418 To find all empty lines in the standard input:
381 419
382 420 .sp
383 421 .in +2
384 422 .nf
385 423 example% \fB/usr/bin/grep ^$\fR
386 424 .fi
387 425 .in -2
388 426 .sp
389 427
390 428 .sp
391 429 .LP
392 430 or
393 431
394 432 .sp
395 433 .in +2
396 434 .nf
397 435 example% \fB/usr/bin/grep -v .\fR
398 436 .fi
399 437 .in -2
400 438 .sp
401 439
402 440 .LP
403 441 \fBExample 3 \fRFinding Lines Containing Strings
404 442 .sp
405 443 .LP
406 444 All of the following commands print all lines containing strings \fBabc\fR or
407 445 \fBdef\fR or both:
408 446
409 447 .sp
410 448 .in +2
411 449 .nf
412 450 example% \fB/usr/xpg4/bin/grep 'abc
413 451 def'\fR
414 452 example% \fB/usr/xpg4/bin/grep -e 'abc
415 453 def'\fR
416 454 example% \fB/usr/xpg4/bin/grep -e 'abc' -e 'def'\fR
417 455 example% \fB/usr/xpg4/bin/grep -E 'abc|def'\fR
418 456 example% \fB/usr/xpg4/bin/grep -E -e 'abc|def'\fR
419 457 example% \fB/usr/xpg4/bin/grep -E -e 'abc' -e 'def'\fR
420 458 example% \fB/usr/xpg4/bin/grep -E 'abc
421 459 def'\fR
422 460 example% \fB/usr/xpg4/bin/grep -E -e 'abc
423 461 def'\fR
424 462 example% \fB/usr/xpg4/bin/grep -F -e 'abc' -e 'def'\fR
425 463 example% \fB/usr/xpg4/bin/grep -F 'abc
426 464 def'\fR
427 465 example% \fB/usr/xpg4/bin/grep -F -e 'abc
428 466 def'\fR
429 467 .fi
430 468 .in -2
431 469 .sp
432 470
433 471 .LP
434 472 \fBExample 4 \fRFinding Lines with Matching Strings
435 473 .sp
436 474 .LP
437 475 Both of the following commands print all lines matching exactly \fBabc\fR or
438 476 \fBdef\fR:
439 477
440 478 .sp
441 479 .in +2
442 480 .nf
443 481 example% \fB/usr/xpg4/bin/grep -E '^abc$ ^def$'\fR
444 482 example% \fB/usr/xpg4/bin/grep -F -x 'abc def'\fR
445 483 .fi
446 484 .in -2
447 485 .sp
448 486
449 487 .SH ENVIRONMENT VARIABLES
450 488 .sp
451 489 .LP
452 490 See \fBenviron\fR(5) for descriptions of the following environment variables
453 491 that affect the execution of \fBgrep\fR: \fBLANG\fR, \fBLC_ALL\fR,
454 492 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
455 493 .SH EXIT STATUS
456 494 .sp
457 495 .LP
458 496 The following exit values are returned:
459 497 .sp
460 498 .ne 2
461 499 .na
462 500 \fB\fB0\fR\fR
463 501 .ad
464 502 .RS 5n
465 503 One or more matches were found.
466 504 .RE
467 505
468 506 .sp
469 507 .ne 2
470 508 .na
471 509 \fB\fB1\fR\fR
472 510 .ad
473 511 .RS 5n
474 512 No matches were found.
475 513 .RE
476 514
477 515 .sp
478 516 .ne 2
479 517 .na
480 518 \fB\fB2\fR\fR
481 519 .ad
482 520 .RS 5n
483 521 Syntax errors or inaccessible files (even if matches were found).
484 522 .RE
485 523
486 524 .SH ATTRIBUTES
487 525 .sp
488 526 .LP
489 527 See \fBattributes\fR(5) for descriptions of the following attributes:
490 528 .SS "/usr/bin/grep"
491 529 .sp
492 530
493 531 .sp
494 532 .TS
495 533 box;
496 534 c | c
497 535 l | l .
498 536 ATTRIBUTE TYPE ATTRIBUTE VALUE
499 537 _
500 538 CSI Not Enabled
501 539 .TE
502 540
503 541 .SS "/usr/xpg4/bin/grep"
504 542 .sp
505 543
506 544 .sp
507 545 .TS
508 546 box;
509 547 c | c
510 548 l | l .
511 549 ATTRIBUTE TYPE ATTRIBUTE VALUE
512 550 _
513 551 CSI Enabled
514 552 _
515 553 Interface Stability Committed
516 554 _
517 555 Standard See \fBstandards\fR(5).
518 556 .TE
519 557
520 558 .SH SEE ALSO
521 559 .sp
522 560 .LP
523 561 \fBegrep\fR(1), \fBfgrep\fR(1), \fBsed\fR(1), \fBsh\fR(1), \fBattributes\fR(5),
524 562 \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5), \fBregexp\fR(5),
525 563 \fBstandards\fR(5)
526 564 .SH NOTES
527 565 .SS "/usr/bin/grep"
528 566 .sp
529 567 .LP
530 568 Lines are limited only by the size of the available virtual memory. If there is
531 569 a line with embedded nulls, \fBgrep\fR only matches up to the first null. If
532 570 the line matches, the entire line is printed.
533 571 .SS "/usr/xpg4/bin/grep"
534 572 .sp
535 573 .LP
536 574 The results are unspecified if input files contain lines longer than
537 575 \fBLINE_MAX\fR bytes or contain binary data. \fBLINE_MAX\fR is defined in
538 576 \fB/usr/include/limits.h\fR.
↓ open down ↓ |
319 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX