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