3546 add support for grep -o option

   1 '\" te

   2 .\" Copyright 2012 Nexenta Systems, Inc. All rights reserved.
   3 .\" Copyright 1989 AT&T
   4 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved
   5 .\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
   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 .\" http://www.opengroup.org/bookstore/.
   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 .\"  This notice shall appear on any product containing this material.
  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 .\" 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 .\" 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 .TH GREP 1 "May 3, 2013"
  14 .SH NAME
  15 grep \- search a file for a pattern
  16 .SH SYNOPSIS
  17 .LP
  18 .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     \fIlimited-regular-expression\fR [\fIfilename\fR]...
  21 .fi
  22 
  23 .LP
  24 .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-bHhinsvwx\fR] \fB-e\fR \fIpattern_list\fR... [\fB-f\fR \fIpattern_file\fR]...
  27     [\fIfile\fR]...
  28 .fi
  29 
  30 .LP
  31 .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-bHhinsvwx\fR] [\fB-e\fR \fIpattern_list\fR]... \fB-f\fR \fIpattern_file\fR...
  34     [\fIfile\fR]...
  35 .fi
  36 
  37 .LP
  38 .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-bHhinsvwx\fR] \fIpattern\fR [\fIfile\fR]...
  41 .fi
  42 
  43 .SH DESCRIPTION
  44 .sp
  45 .LP
  46 The \fBgrep\fR utility searches text files for a pattern and prints all lines
  47 that contain that pattern.  It uses a compact non-deterministic algorithm.
  48 .sp
  49 .LP
  50 Be careful using the characters \fB$\fR, \fB*\fR, \fB[\fR, \fB^\fR, \fB|\fR,
  51 \fB(\fR, \fB)\fR, and \fB\e\fR in the \fIpattern_list\fR because they are also
  52 meaningful to the shell. It is safest to enclose the entire \fIpattern_list\fR
  53 in single quotes \fBa\'\fR\&...\fBa\'\fR\&.
  54 .sp
  55 .LP
  56 If no files are specified, \fBgrep\fR assumes standard input. Normally, each
  57 line found is copied to standard output. The file name is printed before each
  58 line found if there is more than one input file.
  59 .SS "/usr/bin/grep"
  60 .sp
  61 .LP
  62 The \fB/usr/bin/grep\fR utility uses limited regular expressions like those
  63 described on the \fBregexp\fR(5) manual page to match the patterns.
  64 .SS "/usr/xpg4/bin/grep"
  65 .sp
  66 .LP
  67 The options \fB-E\fR and \fB-F\fR affect the way \fB/usr/xpg4/bin/grep\fR
  68 interprets \fIpattern_list\fR. If \fB-E\fR is specified,
  69 \fB/usr/xpg4/bin/grep\fR interprets \fIpattern_list\fR as a full regular
  70 expression (see \fB-E\fR for description).  If \fB-F\fR is specified,
  71 \fBgrep\fR interprets \fIpattern_list\fR as a fixed string. If neither are
  72 specified, \fBgrep\fR interprets \fIpattern_list\fR as a basic regular
  73 expression as described on \fBregex\fR(5) manual page.
  74 .SH OPTIONS
  75 .sp
  76 .LP
  77 The following options are supported for both \fB/usr/bin/grep\fR and
  78 \fB/usr/xpg4/bin/grep\fR:
  79 .sp
  80 .ne 2
  81 .na
  82 \fB\fB-b\fR\fR
  83 .ad
  84 .RS 6n
  85 Precedes each line by the block number on which it was found. This can be
  86 useful in locating block numbers by context (first block is 0).
  87 .RE
  88 
  89 .sp
  90 .ne 2
  91 .na
  92 \fB\fB-c\fR\fR
  93 .ad
  94 .RS 6n
  95 Prints only a count of the lines that contain the pattern.
  96 .RE
  97 
  98 .sp
  99 .ne 2
 100 .na
 101 \fB\fB-H\fR\fR
 102 .ad
 103 .RS 6n
 104 Precedes each line by the name of the file containing the matching line.
 105 .RE
 106 
 107 .sp
 108 .ne 2
 109 .na
 110 \fB\fB-h\fR\fR
 111 .ad
 112 .RS 6n
 113 Prevents the name of the file containing the matching line from being prepended
 114 to that line.  Used when searching multiple files.
 115 .RE
 116 
 117 .sp
 118 .ne 2
 119 .na
 120 \fB\fB-i\fR\fR
 121 .ad
 122 .RS 6n
 123 Ignores upper/lower case distinction during comparisons.
 124 .RE
 125 
 126 .sp
 127 .ne 2
 128 .na
 129 \fB\fB-l\fR\fR
 130 .ad
 131 .RS 6n
 132 Prints only the names of files with matching lines, separated by NEWLINE
 133 characters.  Does not repeat the names of files when the pattern is found more
 134 than once.
 135 .RE
 136 
 137 .sp
 138 .ne 2
 139 .na
 140 \fB\fB-n\fR\fR
 141 .ad
 142 .RS 6n
 143 Precedes each line by its line number in the file (first line is 1).
 144 .RE
 145 
 146 .sp
 147 .ne 2
 148 .na









 149 \fB\fB-r\fR\fR
 150 .ad
 151 .RS 6n
 152 Read all files under each directory, recursively. Follow symbolic links on
 153 the command line, but skip symlinks that are encountered recursively. If file
 154 is a device, FIFO, or socket, skip it.
 155 .RE
 156 
 157 .sp
 158 .ne 2
 159 .na
 160 \fB\fB-R\fR\fR
 161 .ad
 162 .RS 6n
 163 Read all files under each directory, recursively, following all symbolic links.
 164 .RE
 165 
 166 .sp
 167 .ne 2
 168 .na
 169 \fB\fB-q\fR\fR
 170 .ad
 171 .RS 6n
 172 Quiet. Does not write anything to the standard output, regardless of matching
 173 lines. Exits with zero status if an input line is selected.
 174 .RE
 175 
 176 .sp
 177 .ne 2
 178 .na
 179 \fB\fB-s\fR\fR
 180 .ad
 181 .RS 6n
 182 Suppresses error messages about nonexistent or unreadable files.
 183 .RE
 184 
 185 .sp
 186 .ne 2
 187 .na
 188 \fB\fB-v\fR\fR
 189 .ad
 190 .RS 6n
 191 Prints all lines except those that contain the pattern.
 192 .RE
 193 
 194 .sp
 195 .ne 2
 196 .na
 197 \fB\fB-w\fR\fR
 198 .ad
 199 .RS 6n
 200 Searches for the expression as a word as if surrounded by \fB\e<\fR and
 201 \fB\e>\fR\&.
 202 .RE
 203 
 204 .SS "/usr/xpg4/bin/grep"
 205 .sp
 206 .LP
 207 The following options are supported for \fB/usr/xpg4/bin/grep\fR only:
 208 .sp
 209 .ne 2
 210 .na
 211 \fB\fB-e\fR \fIpattern_list\fR\fR
 212 .ad
 213 .RS 19n
 214 Specifies one or more patterns to be used during the search for input. Patterns
 215 in \fIpattern_list\fR must be separated by a NEWLINE character. A null pattern
 216 can be specified by two adjacent newline characters in \fIpattern_list\fR.
 217 Unless the \fB-E\fR or \fB-F\fR option is also specified, each pattern is
 218 treated as a basic regular expression.  Multiple \fB-e\fR and \fB-f\fR options
 219 are accepted by \fBgrep\fR. All of the specified patterns are used when
 220 matching lines, but the order of evaluation is unspecified.
 221 .RE
 222 
 223 .sp
 224 .ne 2
 225 .na
 226 \fB\fB-E\fR\fR
 227 .ad
 228 .RS 19n
 229 Matches using full regular expressions. Treats each pattern specified as a full
 230 regular expression. If any entire full regular expression pattern matches an
 231 input line, the line is matched. A null full regular expression matches every
 232 line. Each pattern is interpreted as a full regular expression as described on
 233 the \fBregex\fR(5) manual page, except for \fB\e(\fR and \fB\e)\fR, and
 234 including:
 235 .RS +4
 236 .TP
 237 1.
 238 A full regular expression followed by \fB+\fR that matches one or more
 239 occurrences of the full regular expression.
 240 .RE
 241 .RS +4
 242 .TP
 243 2.
 244 A full regular expression followed by \fB?\fR that matches 0 or 1
 245 occurrences of the full regular expression.
 246 .RE
 247 .RS +4
 248 .TP
 249 3.
 250 Full regular expressions separated by | or by a new-line that match strings
 251 that are matched by any of the expressions.
 252 .RE
 253 .RS +4
 254 .TP
 255 4.
 256 A full regular expression that is enclosed in parentheses \fB()\fR for
 257 grouping.
 258 .RE
 259 The order of precedence of operators is \fB[\|]\fR, then \fB*\|?\|+\fR, then
 260 concatenation, then | and new-line.
 261 .RE
 262 
 263 .sp
 264 .ne 2
 265 .na
 266 \fB\fB-f\fR \fIpattern_file\fR\fR
 267 .ad
 268 .RS 19n
 269 Reads one or more patterns from the file named by the path name
 270 \fIpattern_file\fR. Patterns in \fIpattern_file\fR are terminated by a NEWLINE
 271 character. A null pattern can be specified by an empty line in
 272 \fIpattern_file\fR. Unless the \fB-E\fR or \fB-F\fR option is also specified,
 273 each pattern is treated as a basic regular expression.
 274 .RE
 275 
 276 .sp
 277 .ne 2
 278 .na
 279 \fB\fB-F\fR\fR
 280 .ad
 281 .RS 19n
 282 Matches using fixed strings. Treats each pattern specified as a string instead
 283 of a regular expression. If an input line contains any of the patterns as a
 284 contiguous sequence of bytes, the line is matched. A null string matches every
 285 line. See \fBfgrep\fR(1) for more information.
 286 .RE
 287 
 288 .sp
 289 .ne 2
 290 .na
 291 \fB\fB-x\fR\fR
 292 .ad
 293 .RS 19n
 294 Considers only input lines that use all characters in the line to match an
 295 entire fixed string or regular expression to be matching lines.
 296 .RE
 297 
 298 .SH OPERANDS
 299 .sp
 300 .LP
 301 The following operands are supported:
 302 .sp
 303 .ne 2
 304 .na
 305 \fB\fIfile\fR\fR
 306 .ad
 307 .RS 8n
 308 A path name of a file to be searched for the patterns. If no \fIfile\fR
 309 operands are specified, the standard input is used.
 310 .RE
 311 
 312 .SS "/usr/bin/grep"
 313 .sp
 314 .ne 2
 315 .na
 316 \fB\fIpattern\fR\fR
 317 .ad
 318 .RS 11n
 319 Specifies a pattern to be used during the search for input.
 320 .RE
 321 
 322 .SS "/usr/xpg4/bin/grep"
 323 .sp
 324 .ne 2
 325 .na
 326 \fB\fIpattern\fR\fR
 327 .ad
 328 .RS 11n
 329 Specifies one or more patterns to be used during the search for input. This
 330 operand is treated as if it were specified as \fB-e\fR \fIpattern_list\fR.
 331 .RE
 332 
 333 .SH USAGE
 334 .sp
 335 .LP
 336 The \fB-e\fR \fIpattern_list\fR option has the same effect as the
 337 \fIpattern_list\fR operand, but is useful when \fIpattern_list\fR begins with
 338 the hyphen delimiter. It is also useful when it is more convenient to provide
 339 multiple patterns as separate arguments.
 340 .sp
 341 .LP
 342 Multiple \fB-e\fR and \fB-f\fR options are accepted and \fBgrep\fR uses all of
 343 the patterns it is given while matching input text lines. Notice that the order
 344 of evaluation is not specified. If an implementation finds a null string as a
 345 pattern, it is allowed to use that pattern first, matching every line, and
 346 effectively ignore any other patterns.
 347 .sp
 348 .LP
 349 The \fB-q\fR option provides a means of easily determining whether or not a
 350 pattern (or string) exists in a group of files. When searching several files,
 351 it provides a performance improvement (because it can quit as soon as it finds
 352 the first match) and requires less care by the user in choosing the set of
 353 files to supply as arguments (because it exits zero if it finds a match even if
 354 \fBgrep\fR detected an access or read error on earlier file operands).
 355 .SS "Large File Behavior"
 356 .sp
 357 .LP
 358 See \fBlargefile\fR(5) for the description of the behavior of \fBgrep\fR when
 359 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
 360 .SH EXAMPLES
 361 .LP
 362 \fBExample 1 \fRFinding All Uses of a Word
 363 .sp
 364 .LP
 365 To find all uses of the word "\fBPosix\fR" (in any case) in the file
 366 \fBtext.mm\fR, and write with line numbers:
 367 
 368 .sp
 369 .in +2
 370 .nf
 371 example% \fB/usr/bin/grep -i -n posix text.mm\fR
 372 .fi
 373 .in -2
 374 .sp
 375 
 376 .LP
 377 \fBExample 2 \fRFinding All Empty Lines
 378 .sp
 379 .LP
 380 To find all empty lines in the standard input:
 381 
 382 .sp
 383 .in +2
 384 .nf
 385 example% \fB/usr/bin/grep ^$\fR
 386 .fi
 387 .in -2
 388 .sp
 389 
 390 .sp
 391 .LP
 392 or
 393 
 394 .sp
 395 .in +2
 396 .nf
 397 example% \fB/usr/bin/grep -v .\fR
 398 .fi
 399 .in -2
 400 .sp
 401 
 402 .LP
 403 \fBExample 3 \fRFinding Lines Containing Strings
 404 .sp
 405 .LP
 406 All of the following commands print all lines containing strings \fBabc\fR or
 407 \fBdef\fR or both:
 408 
 409 .sp
 410 .in +2
 411 .nf
 412 example% \fB/usr/xpg4/bin/grep 'abc
 413 def'\fR
 414 example% \fB/usr/xpg4/bin/grep -e 'abc
 415 def'\fR
 416 example% \fB/usr/xpg4/bin/grep -e 'abc' -e 'def'\fR
 417 example% \fB/usr/xpg4/bin/grep -E 'abc|def'\fR
 418 example% \fB/usr/xpg4/bin/grep -E -e 'abc|def'\fR
 419 example% \fB/usr/xpg4/bin/grep -E -e 'abc' -e 'def'\fR
 420 example% \fB/usr/xpg4/bin/grep -E 'abc
 421 def'\fR
 422 example% \fB/usr/xpg4/bin/grep -E -e 'abc
 423 def'\fR
 424 example% \fB/usr/xpg4/bin/grep -F -e 'abc' -e 'def'\fR
 425 example% \fB/usr/xpg4/bin/grep -F 'abc
 426 def'\fR
 427 example% \fB/usr/xpg4/bin/grep -F -e 'abc
 428 def'\fR
 429 .fi
 430 .in -2
 431 .sp
 432 
 433 .LP
 434 \fBExample 4 \fRFinding Lines with Matching Strings
 435 .sp
 436 .LP
 437 Both of the following commands print all lines matching exactly \fBabc\fR or
 438 \fBdef\fR:
 439 
 440 .sp
 441 .in +2
 442 .nf
 443 example% \fB/usr/xpg4/bin/grep -E '^abc$ ^def$'\fR
 444 example% \fB/usr/xpg4/bin/grep -F -x 'abc def'\fR
 445 .fi
 446 .in -2
 447 .sp
 448 
 449 .SH ENVIRONMENT VARIABLES
 450 .sp
 451 .LP
 452 See \fBenviron\fR(5) for descriptions of the following environment variables
 453 that affect the execution of \fBgrep\fR: \fBLANG\fR, \fBLC_ALL\fR,
 454 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
 455 .SH EXIT STATUS
 456 .sp
 457 .LP
 458 The following exit values are returned:
 459 .sp
 460 .ne 2
 461 .na
 462 \fB\fB0\fR\fR
 463 .ad
 464 .RS 5n
 465 One or more matches were found.
 466 .RE
 467 
 468 .sp
 469 .ne 2
 470 .na
 471 \fB\fB1\fR\fR
 472 .ad
 473 .RS 5n
 474 No matches were found.
 475 .RE
 476 
 477 .sp
 478 .ne 2
 479 .na
 480 \fB\fB2\fR\fR
 481 .ad
 482 .RS 5n
 483 Syntax errors or inaccessible files (even if matches were found).
 484 .RE
 485 
 486 .SH ATTRIBUTES
 487 .sp
 488 .LP
 489 See \fBattributes\fR(5) for descriptions of the following attributes:
 490 .SS "/usr/bin/grep"
 491 .sp
 492 
 493 .sp
 494 .TS
 495 box;
 496 c | c
 497 l | l .
 498 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 499 _
 500 CSI     Not Enabled
 501 .TE
 502 
 503 .SS "/usr/xpg4/bin/grep"
 504 .sp
 505 
 506 .sp
 507 .TS
 508 box;
 509 c | c
 510 l | l .
 511 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 512 _
 513 CSI     Enabled
 514 _
 515 Interface Stability     Committed
 516 _
 517 Standard        See \fBstandards\fR(5).
 518 .TE
 519 
 520 .SH SEE ALSO
 521 .sp
 522 .LP
 523 \fBegrep\fR(1), \fBfgrep\fR(1), \fBsed\fR(1), \fBsh\fR(1), \fBattributes\fR(5),
 524 \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5), \fBregexp\fR(5),
 525 \fBstandards\fR(5)
 526 .SH NOTES
 527 .SS "/usr/bin/grep"
 528 .sp
 529 .LP
 530 Lines are limited only by the size of the available virtual memory. If there is
 531 a line with embedded nulls, \fBgrep\fR only matches up to the first null. If
 532 the line matches, the entire line is printed.
 533 .SS "/usr/xpg4/bin/grep"
 534 .sp
 535 .LP
 536 The results are unspecified if input files contain lines longer than
 537 \fBLINE_MAX\fR bytes or contain binary data. \fBLINE_MAX\fR is defined in
 538 \fB/usr/include/limits.h\fR.
--- EOF ---