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