Print this page
8858 /usr/bin/grep doesn't support -E option
4580 /usr/bin/grep can't handle multibyte characters
8929 8868 tests are not delivered with system/test/utiltest
8860 Example in grep(1) is incorrect
Reviewed by: Peter Tribble <peter.tribble@gmail.com>
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Yuri Pankov <yuripv@gmx.com>
   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


 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


 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.




   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


 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


 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.