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 \fB\'\fR\&...\fB\'\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-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
 248 .na
 249 \fB\fB-e\fR \fIpattern_list\fR\fR
 250 .ad
 251 .RS 19n
 252 Specifies one or more patterns to be used during the search for input. Patterns
 253 in \fIpattern_list\fR must be separated by a NEWLINE character. A null pattern
 254 can be specified by two adjacent newline characters in \fIpattern_list\fR.
 255 Unless the \fB-E\fR or \fB-F\fR option is also specified, each pattern is
 256 treated as a basic regular expression.  Multiple \fB-e\fR and \fB-f\fR options
 257 are accepted by \fBgrep\fR. All of the specified patterns are used when
 258 matching lines, but the order of evaluation is unspecified.
 259 .RE
 260 
 261 .sp
 262 .ne 2
 263 .na
 264 \fB\fB-E\fR\fR
 265 .ad
 266 .RS 19n
 267 Matches using full regular expressions. Treats each pattern specified as a full
 268 regular expression. If any entire full regular expression pattern matches an
 269 input line, the line is matched. A null full regular expression matches every
 270 line. Each pattern is interpreted as a full regular expression as described on
 271 the \fBregex\fR(5) manual page, except for \fB\e(\fR and \fB\e)\fR, and
 272 including:
 273 .RS +4
 274 .TP
 275 1.
 276 A full regular expression followed by \fB+\fR that matches one or more
 277 occurrences of the full regular expression.
 278 .RE
 279 .RS +4
 280 .TP
 281 2.
 282 A full regular expression followed by \fB?\fR that matches 0 or 1
 283 occurrences of the full regular expression.
 284 .RE
 285 .RS +4
 286 .TP
 287 3.
 288 Full regular expressions separated by | or by a new-line that match strings
 289 that are matched by any of the expressions.
 290 .RE
 291 .RS +4
 292 .TP
 293 4.
 294 A full regular expression that is enclosed in parentheses \fB()\fR for
 295 grouping.
 296 .RE
 297 The order of precedence of operators is \fB[\|]\fR, then \fB*\|?\|+\fR, then
 298 concatenation, then | and new-line.
 299 .RE
 300 
 301 .sp
 302 .ne 2
 303 .na
 304 \fB\fB-f\fR \fIpattern_file\fR\fR
 305 .ad
 306 .RS 19n
 307 Reads one or more patterns from the file named by the path name
 308 \fIpattern_file\fR. Patterns in \fIpattern_file\fR are terminated by a NEWLINE
 309 character. A null pattern can be specified by an empty line in
 310 \fIpattern_file\fR. Unless the \fB-E\fR or \fB-F\fR option is also specified,
 311 each pattern is treated as a basic regular expression.
 312 .RE
 313 
 314 .sp
 315 .ne 2
 316 .na
 317 \fB\fB-F\fR\fR
 318 .ad
 319 .RS 19n
 320 Matches using fixed strings. Treats each pattern specified as a string instead
 321 of a regular expression. If an input line contains any of the patterns as a
 322 contiguous sequence of bytes, the line is matched. A null string matches every
 323 line. See \fBfgrep\fR(1) for more information.
 324 .RE
 325 
 326 .sp
 327 .ne 2
 328 .na
 329 \fB\fB-x\fR\fR
 330 .ad
 331 .RS 19n
 332 Considers only input lines that use all characters in the line to match an
 333 entire fixed string or regular expression to be matching lines.
 334 .RE
 335 
 336 .SH OPERANDS
 337 .sp
 338 .LP
 339 The following operands are supported:
 340 .sp
 341 .ne 2
 342 .na
 343 \fB\fIfile\fR\fR
 344 .ad
 345 .RS 8n
 346 A path name of a file to be searched for the patterns. If no \fIfile\fR
 347 operands are specified, the standard input is used.
 348 .RE
 349 
 350 .SS "/usr/bin/grep"
 351 .sp
 352 .ne 2
 353 .na
 354 \fB\fIpattern\fR\fR
 355 .ad
 356 .RS 11n
 357 Specifies a pattern to be used during the search for input.
 358 .RE
 359 
 360 .SS "/usr/xpg4/bin/grep"
 361 .sp
 362 .ne 2
 363 .na
 364 \fB\fIpattern\fR\fR
 365 .ad
 366 .RS 11n
 367 Specifies one or more patterns to be used during the search for input. This
 368 operand is treated as if it were specified as \fB-e\fR \fIpattern_list\fR.
 369 .RE
 370 
 371 .SH USAGE
 372 .sp
 373 .LP
 374 The \fB-e\fR \fIpattern_list\fR option has the same effect as the
 375 \fIpattern_list\fR operand, but is useful when \fIpattern_list\fR begins with
 376 the hyphen delimiter. It is also useful when it is more convenient to provide
 377 multiple patterns as separate arguments.
 378 .sp
 379 .LP
 380 Multiple \fB-e\fR and \fB-f\fR options are accepted and \fBgrep\fR uses all of
 381 the patterns it is given while matching input text lines. Notice that the order
 382 of evaluation is not specified. If an implementation finds a null string as a
 383 pattern, it is allowed to use that pattern first, matching every line, and
 384 effectively ignore any other patterns.
 385 .sp
 386 .LP
 387 The \fB-q\fR option provides a means of easily determining whether or not a
 388 pattern (or string) exists in a group of files. When searching several files,
 389 it provides a performance improvement (because it can quit as soon as it finds
 390 the first match) and requires less care by the user in choosing the set of
 391 files to supply as arguments (because it exits zero if it finds a match even if
 392 \fBgrep\fR detected an access or read error on earlier file operands).
 393 .SS "Large File Behavior"
 394 .sp
 395 .LP
 396 See \fBlargefile\fR(5) for the description of the behavior of \fBgrep\fR when
 397 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
 398 .SH EXAMPLES
 399 .LP
 400 \fBExample 1 \fRFinding All Uses of a Word
 401 .sp
 402 .LP
 403 To find all uses of the word "\fBPosix\fR" (in any case) in the file
 404 \fBtext.mm\fR, and write with line numbers:
 405 
 406 .sp
 407 .in +2
 408 .nf
 409 example% \fB/usr/bin/grep -i -n posix text.mm\fR
 410 .fi
 411 .in -2
 412 .sp
 413 
 414 .LP
 415 \fBExample 2 \fRFinding All Empty Lines
 416 .sp
 417 .LP
 418 To find all empty lines in the standard input:
 419 
 420 .sp
 421 .in +2
 422 .nf
 423 example% \fB/usr/bin/grep ^$\fR
 424 .fi
 425 .in -2
 426 .sp
 427 
 428 .sp
 429 .LP
 430 or
 431 
 432 .sp
 433 .in +2
 434 .nf
 435 example% \fB/usr/bin/grep -v .\fR
 436 .fi
 437 .in -2
 438 .sp
 439 
 440 .LP
 441 \fBExample 3 \fRFinding Lines Containing Strings
 442 .sp
 443 .LP
 444 All of the following commands print all lines containing strings \fBabc\fR or
 445 \fBdef\fR or both:
 446 
 447 .sp
 448 .in +2
 449 .nf
 450 example% \fB/usr/xpg4/bin/grep 'abc
 451 def'\fR
 452 example% \fB/usr/xpg4/bin/grep -e 'abc
 453 def'\fR
 454 example% \fB/usr/xpg4/bin/grep -e 'abc' -e 'def'\fR
 455 example% \fB/usr/xpg4/bin/grep -E 'abc|def'\fR
 456 example% \fB/usr/xpg4/bin/grep -E -e 'abc|def'\fR
 457 example% \fB/usr/xpg4/bin/grep -E -e 'abc' -e 'def'\fR
 458 example% \fB/usr/xpg4/bin/grep -E 'abc
 459 def'\fR
 460 example% \fB/usr/xpg4/bin/grep -E -e 'abc
 461 def'\fR
 462 example% \fB/usr/xpg4/bin/grep -F -e 'abc' -e 'def'\fR
 463 example% \fB/usr/xpg4/bin/grep -F 'abc
 464 def'\fR
 465 example% \fB/usr/xpg4/bin/grep -F -e 'abc
 466 def'\fR
 467 .fi
 468 .in -2
 469 .sp
 470 
 471 .LP
 472 \fBExample 4 \fRFinding Lines with Matching Strings
 473 .sp
 474 .LP
 475 Both of the following commands print all lines matching exactly \fBabc\fR or
 476 \fBdef\fR:
 477 
 478 .sp
 479 .in +2
 480 .nf
 481 example% \fB/usr/xpg4/bin/grep -E '^abc$ ^def$'\fR
 482 example% \fB/usr/xpg4/bin/grep -F -x 'abc def'\fR
 483 .fi
 484 .in -2
 485 .sp
 486 
 487 .SH ENVIRONMENT VARIABLES
 488 .sp
 489 .LP
 490 See \fBenviron\fR(5) for descriptions of the following environment variables
 491 that affect the execution of \fBgrep\fR: \fBLANG\fR, \fBLC_ALL\fR,
 492 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
 493 .SH EXIT STATUS
 494 .sp
 495 .LP
 496 The following exit values are returned:
 497 .sp
 498 .ne 2
 499 .na
 500 \fB\fB0\fR\fR
 501 .ad
 502 .RS 5n
 503 One or more matches were found.
 504 .RE
 505 
 506 .sp
 507 .ne 2
 508 .na
 509 \fB\fB1\fR\fR
 510 .ad
 511 .RS 5n
 512 No matches were found.
 513 .RE
 514 
 515 .sp
 516 .ne 2
 517 .na
 518 \fB\fB2\fR\fR
 519 .ad
 520 .RS 5n
 521 Syntax errors or inaccessible files (even if matches were found).
 522 .RE
 523 
 524 .SH ATTRIBUTES
 525 .sp
 526 .LP
 527 See \fBattributes\fR(5) for descriptions of the following attributes:
 528 .SS "/usr/bin/grep"
 529 .sp
 530 
 531 .sp
 532 .TS
 533 box;
 534 c | c
 535 l | l .
 536 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 537 _
 538 CSI     Not Enabled
 539 .TE
 540 
 541 .SS "/usr/xpg4/bin/grep"
 542 .sp
 543 
 544 .sp
 545 .TS
 546 box;
 547 c | c
 548 l | l .
 549 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 550 _
 551 CSI     Enabled
 552 _
 553 Interface Stability     Committed
 554 _
 555 Standard        See \fBstandards\fR(5).
 556 .TE
 557 
 558 .SH SEE ALSO
 559 .sp
 560 .LP
 561 \fBegrep\fR(1), \fBfgrep\fR(1), \fBsed\fR(1), \fBsh\fR(1), \fBattributes\fR(5),
 562 \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5), \fBregexp\fR(5),
 563 \fBstandards\fR(5)
 564 .SH NOTES
 565 .SS "/usr/bin/grep"
 566 .sp
 567 .LP
 568 Lines are limited only by the size of the available virtual memory. If there is
 569 a line with embedded nulls, \fBgrep\fR only matches up to the first null. If
 570 the line matches, the entire line is printed.
 571 .SS "/usr/xpg4/bin/grep"
 572 .sp
 573 .LP
 574 The results are unspecified if input files contain lines longer than
 575 \fBLINE_MAX\fR bytes or contain binary data. \fBLINE_MAX\fR is defined in
 576 \fB/usr/include/limits.h\fR.