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