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