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