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