| 1 | .\" GNU grep man page
|
|---|
| 2 | .de dT
|
|---|
| 3 | .ds Dt \\$2
|
|---|
| 4 | ..
|
|---|
| 5 | .dT Time-stamp: "2019-12-29"
|
|---|
| 6 | .\" Update the above date whenever a change to either this file or
|
|---|
| 7 | .\" grep.c's 'usage' function results in a nontrivial change to the man page.
|
|---|
| 8 | .\" In Emacs, you can update the date by running 'M-x time-stamp'
|
|---|
| 9 | .\" after you make a change that you decide is nontrivial.
|
|---|
| 10 | .\" It is no big deal to forget to update the date.
|
|---|
| 11 | .
|
|---|
| 12 | .TH GREP 1 \*(Dt "GNU grep @VERSION@" "User Commands"
|
|---|
| 13 | .
|
|---|
| 14 | .if !\w|\*(lq| \{\
|
|---|
| 15 | .\" groff an-old.tmac does not seem to be in use, so define lq and rq.
|
|---|
| 16 | . ie \n(.g \{\
|
|---|
| 17 | . ds lq \(lq\"
|
|---|
| 18 | . ds rq \(rq\"
|
|---|
| 19 | . \}
|
|---|
| 20 | . el \{\
|
|---|
| 21 | . ds lq ``
|
|---|
| 22 | . ds rq ''
|
|---|
| 23 | . \}
|
|---|
| 24 | .\}
|
|---|
| 25 | .
|
|---|
| 26 | .if !\w|\*(la| \{\
|
|---|
| 27 | .\" groff an-ext.tmac does not seem to be in use, so define the parts of
|
|---|
| 28 | .\" it that are used below. For a copy of groff an-ext.tmac, please see:
|
|---|
| 29 | .\" https://git.savannah.gnu.org/cgit/groff.git/plain/tmac/an-ext.tmac
|
|---|
| 30 | .\" --- Start of lines taken from groff an-ext.tmac
|
|---|
| 31 | .
|
|---|
| 32 | .\" Check whether we are using grohtml.
|
|---|
| 33 | .nr mH 0
|
|---|
| 34 | .if \n(.g \
|
|---|
| 35 | . if '\*(.T'html' \
|
|---|
| 36 | . nr mH 1
|
|---|
| 37 | .
|
|---|
| 38 | .
|
|---|
| 39 | .\" Map mono-width fonts to standard fonts for groff's TTY device.
|
|---|
| 40 | .if n \{\
|
|---|
| 41 | . do ftr CR R
|
|---|
| 42 | . do ftr CI I
|
|---|
| 43 | . do ftr CB B
|
|---|
| 44 | .\}
|
|---|
| 45 | .
|
|---|
| 46 | .\" groff has glyph entities for angle brackets.
|
|---|
| 47 | .ie \n(.g \{\
|
|---|
| 48 | . ds la \(la\"
|
|---|
| 49 | . ds ra \(ra\"
|
|---|
| 50 | .\}
|
|---|
| 51 | .el \{\
|
|---|
| 52 | . ds la <\"
|
|---|
| 53 | . ds ra >\"
|
|---|
| 54 | . \" groff's man macros control hyphenation with this register.
|
|---|
| 55 | . nr HY 1
|
|---|
| 56 | .\}
|
|---|
| 57 | .
|
|---|
| 58 | .\" Start URL.
|
|---|
| 59 | .de UR
|
|---|
| 60 | . ds m1 \\$1\"
|
|---|
| 61 | . nh
|
|---|
| 62 | . if \\n(mH \{\
|
|---|
| 63 | . \" Start diversion in a new environment.
|
|---|
| 64 | . do ev URL-div
|
|---|
| 65 | . do di URL-div
|
|---|
| 66 | . \}
|
|---|
| 67 | ..
|
|---|
| 68 | .
|
|---|
| 69 | .
|
|---|
| 70 | .\" End URL.
|
|---|
| 71 | .de UE
|
|---|
| 72 | . ie \\n(mH \{\
|
|---|
| 73 | . br
|
|---|
| 74 | . di
|
|---|
| 75 | . ev
|
|---|
| 76 | .
|
|---|
| 77 | . \" Has there been one or more input lines for the link text?
|
|---|
| 78 | . ie \\n(dn \{\
|
|---|
| 79 | . do HTML-NS "<a href=""\\*(m1"">"
|
|---|
| 80 | . \" Yes, strip off final newline of diversion and emit it.
|
|---|
| 81 | . do chop URL-div
|
|---|
| 82 | . do URL-div
|
|---|
| 83 | \c
|
|---|
| 84 | . do HTML-NS </a>
|
|---|
| 85 | . \}
|
|---|
| 86 | . el \
|
|---|
| 87 | . do HTML-NS "<a href=""\\*(m1"">\\*(m1</a>"
|
|---|
| 88 | \&\\$*\"
|
|---|
| 89 | . \}
|
|---|
| 90 | . el \
|
|---|
| 91 | \\*(la\\*(m1\\*(ra\\$*\"
|
|---|
| 92 | .
|
|---|
| 93 | . hy \\n(HY
|
|---|
| 94 | ..
|
|---|
| 95 | .
|
|---|
| 96 | .
|
|---|
| 97 | .\" Start email address.
|
|---|
| 98 | .de MT
|
|---|
| 99 | . ds m1 \\$1\"
|
|---|
| 100 | . nh
|
|---|
| 101 | . if \\n(mH \{\
|
|---|
| 102 | . \" Start diversion in a new environment.
|
|---|
| 103 | . do ev URL-div
|
|---|
| 104 | . do di URL-div
|
|---|
| 105 | . \}
|
|---|
| 106 | ..
|
|---|
| 107 | .
|
|---|
| 108 | .
|
|---|
| 109 | .\" End email address.
|
|---|
| 110 | .de ME
|
|---|
| 111 | . ie \\n(mH \{\
|
|---|
| 112 | . br
|
|---|
| 113 | . di
|
|---|
| 114 | . ev
|
|---|
| 115 | .
|
|---|
| 116 | . \" Has there been one or more input lines for the link text?
|
|---|
| 117 | . ie \\n(dn \{\
|
|---|
| 118 | . do HTML-NS "<a href=""mailto:\\*(m1"">"
|
|---|
| 119 | . \" Yes, strip off final newline of diversion and emit it.
|
|---|
| 120 | . do chop URL-div
|
|---|
| 121 | . do URL-div
|
|---|
| 122 | \c
|
|---|
| 123 | . do HTML-NS </a>
|
|---|
| 124 | . \}
|
|---|
| 125 | . el \
|
|---|
| 126 | . do HTML-NS "<a href=""mailto:\\*(m1"">\\*(m1</a>"
|
|---|
| 127 | \&\\$*\"
|
|---|
| 128 | . \}
|
|---|
| 129 | . el \
|
|---|
| 130 | \\*(la\\*(m1\\*(ra\\$*\"
|
|---|
| 131 | .
|
|---|
| 132 | . hy \\n(HY
|
|---|
| 133 | ..
|
|---|
| 134 | .\" --- End of lines taken from groff an-ext.tmac
|
|---|
| 135 | .\}
|
|---|
| 136 | .
|
|---|
| 137 | .hy 0
|
|---|
| 138 | .
|
|---|
| 139 | .SH NAME
|
|---|
| 140 | grep, egrep, fgrep \- print lines that match patterns
|
|---|
| 141 | .
|
|---|
| 142 | .SH SYNOPSIS
|
|---|
| 143 | .B grep
|
|---|
| 144 | .RI [ OPTION .\|.\|.]\&
|
|---|
| 145 | .I PATTERNS
|
|---|
| 146 | .RI [ FILE .\|.\|.]
|
|---|
| 147 | .br
|
|---|
| 148 | .B grep
|
|---|
| 149 | .RI [ OPTION .\|.\|.]\&
|
|---|
| 150 | .B \-e
|
|---|
| 151 | .I PATTERNS
|
|---|
| 152 | \&.\|.\|.\&
|
|---|
| 153 | .RI [ FILE .\|.\|.]
|
|---|
| 154 | .br
|
|---|
| 155 | .B grep
|
|---|
| 156 | .RI [ OPTION .\|.\|.]\&
|
|---|
| 157 | .B \-f
|
|---|
| 158 | .I PATTERN_FILE
|
|---|
| 159 | \&.\|.\|.\&
|
|---|
| 160 | .RI [ FILE .\|.\|.]
|
|---|
| 161 | .
|
|---|
| 162 | .SH DESCRIPTION
|
|---|
| 163 | .B grep
|
|---|
| 164 | searches for
|
|---|
| 165 | .I PATTERNS
|
|---|
| 166 | in each
|
|---|
| 167 | .IR FILE .
|
|---|
| 168 | .I PATTERNS
|
|---|
| 169 | is one or more patterns separated by newline characters, and
|
|---|
| 170 | .B grep
|
|---|
| 171 | prints each line that matches a pattern.
|
|---|
| 172 | Typically
|
|---|
| 173 | .I PATTERNS
|
|---|
| 174 | should be quoted when
|
|---|
| 175 | .B grep
|
|---|
| 176 | is used in a shell command.
|
|---|
| 177 | .PP
|
|---|
| 178 | A
|
|---|
| 179 | .I FILE
|
|---|
| 180 | of
|
|---|
| 181 | .RB "\*(lq" \- "\*(rq"
|
|---|
| 182 | stands for standard input.
|
|---|
| 183 | If no
|
|---|
| 184 | .I FILE
|
|---|
| 185 | is given, recursive searches examine the working directory,
|
|---|
| 186 | and nonrecursive searches read standard input.
|
|---|
| 187 | .PP
|
|---|
| 188 | In addition, the variant programs
|
|---|
| 189 | .B egrep
|
|---|
| 190 | and
|
|---|
| 191 | .B fgrep
|
|---|
| 192 | are the same as
|
|---|
| 193 | .B "grep\ \-E"
|
|---|
| 194 | and
|
|---|
| 195 | .BR "grep\ \-F" ,
|
|---|
| 196 | respectively.
|
|---|
| 197 | These variants are deprecated, but are provided for backward compatibility.
|
|---|
| 198 | .
|
|---|
| 199 | .SH OPTIONS
|
|---|
| 200 | .SS "Generic Program Information"
|
|---|
| 201 | .TP
|
|---|
| 202 | .B \-\^\-help
|
|---|
| 203 | Output a usage message and exit.
|
|---|
| 204 | .TP
|
|---|
| 205 | .BR \-V ", " \-\^\-version
|
|---|
| 206 | Output the version number of
|
|---|
| 207 | .B grep
|
|---|
| 208 | and exit.
|
|---|
| 209 | .SS "Pattern Syntax"
|
|---|
| 210 | .TP
|
|---|
| 211 | .BR \-E ", " \-\^\-extended\-regexp
|
|---|
| 212 | Interpret
|
|---|
| 213 | .I PATTERNS
|
|---|
| 214 | as extended regular expressions (EREs, see below).
|
|---|
| 215 | .TP
|
|---|
| 216 | .BR \-F ", " \-\^\-fixed\-strings
|
|---|
| 217 | Interpret
|
|---|
| 218 | .I PATTERNS
|
|---|
| 219 | as fixed strings, not regular expressions.
|
|---|
| 220 | .TP
|
|---|
| 221 | .BR \-G ", " \-\^\-basic\-regexp
|
|---|
| 222 | Interpret
|
|---|
| 223 | .I PATTERNS
|
|---|
| 224 | as basic regular expressions (BREs, see below).
|
|---|
| 225 | This is the default.
|
|---|
| 226 | .TP
|
|---|
| 227 | .BR \-P ", " \-\^\-perl\-regexp
|
|---|
| 228 | Interpret I<PATTERNS> as Perl-compatible regular expressions (PCREs).
|
|---|
| 229 | This option is experimental when combined with the
|
|---|
| 230 | .B \-z
|
|---|
| 231 | .RB ( \-\^\-null\-data )
|
|---|
| 232 | option, and
|
|---|
| 233 | .B "grep \-P"
|
|---|
| 234 | may warn of unimplemented features.
|
|---|
| 235 | .SS "Matching Control"
|
|---|
| 236 | .TP
|
|---|
| 237 | .BI \-e " PATTERNS" "\fR,\fP \-\^\-regexp=" PATTERNS
|
|---|
| 238 | Use
|
|---|
| 239 | .I PATTERNS
|
|---|
| 240 | as the patterns.
|
|---|
| 241 | If this option is used multiple times or is combined with the
|
|---|
| 242 | .B \-f
|
|---|
| 243 | .RB ( \-\^\-file )
|
|---|
| 244 | option, search for all patterns given.
|
|---|
| 245 | This option can be used to protect a pattern beginning with \*(lq\-\*(rq.
|
|---|
| 246 | .TP
|
|---|
| 247 | .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
|
|---|
| 248 | Obtain patterns from
|
|---|
| 249 | .IR FILE ,
|
|---|
| 250 | one per line.
|
|---|
| 251 | If this option is used multiple times or is combined with the
|
|---|
| 252 | .B \-e
|
|---|
| 253 | .RB ( \-\^\-regexp )
|
|---|
| 254 | option, search for all patterns given.
|
|---|
| 255 | The empty file contains zero patterns, and therefore matches nothing.
|
|---|
| 256 | .TP
|
|---|
| 257 | .BR \-i ", " \-\^\-ignore\-case
|
|---|
| 258 | Ignore case distinctions in patterns and input data,
|
|---|
| 259 | so that characters that differ only in case
|
|---|
| 260 | match each other.
|
|---|
| 261 | .TP
|
|---|
| 262 | .B \-\^\-no\-ignore\-case
|
|---|
| 263 | Do not ignore case distinctions in patterns and input data.
|
|---|
| 264 | This is the default.
|
|---|
| 265 | This option is useful for passing to shell scripts that already use
|
|---|
| 266 | .BR \-i ,
|
|---|
| 267 | to cancel its effects because the two options override each other.
|
|---|
| 268 | .TP
|
|---|
| 269 | .BR \-v ", " \-\^\-invert\-match
|
|---|
| 270 | Invert the sense of matching, to select non-matching lines.
|
|---|
| 271 | .TP
|
|---|
| 272 | .BR \-w ", " \-\^\-word\-regexp
|
|---|
| 273 | Select only those lines containing matches that form whole words.
|
|---|
| 274 | The test is that the matching substring must either be at the
|
|---|
| 275 | beginning of the line, or preceded by a non-word constituent
|
|---|
| 276 | character.
|
|---|
| 277 | Similarly, it must be either at the end of the line
|
|---|
| 278 | or followed by a non-word constituent character.
|
|---|
| 279 | Word-constituent characters are letters, digits, and the underscore.
|
|---|
| 280 | This option has no effect if
|
|---|
| 281 | .B \-x
|
|---|
| 282 | is also specified.
|
|---|
| 283 | .TP
|
|---|
| 284 | .BR \-x ", " \-\^\-line\-regexp
|
|---|
| 285 | Select only those matches that exactly match the whole line.
|
|---|
| 286 | For a regular expression pattern, this is like parenthesizing the
|
|---|
| 287 | pattern and then surrounding it with
|
|---|
| 288 | .B ^
|
|---|
| 289 | and
|
|---|
| 290 | .BR $ .
|
|---|
| 291 | .TP
|
|---|
| 292 | .B \-y
|
|---|
| 293 | Obsolete synonym for
|
|---|
| 294 | .BR \-i .
|
|---|
| 295 | .SS "General Output Control"
|
|---|
| 296 | .TP
|
|---|
| 297 | .BR \-c ", " \-\^\-count
|
|---|
| 298 | Suppress normal output; instead print a count of
|
|---|
| 299 | matching lines for each input file.
|
|---|
| 300 | With the
|
|---|
| 301 | .BR \-v ", " \-\^\-invert\-match
|
|---|
| 302 | option (see below), count non-matching lines.
|
|---|
| 303 | .TP
|
|---|
| 304 | .BR \-\^\-color [ =\fIWHEN\fP "], " \-\^\-colour [ =\fIWHEN\fP ]
|
|---|
| 305 | Surround the matched (non-empty) strings, matching lines, context lines,
|
|---|
| 306 | file names, line numbers, byte offsets, and separators (for fields and
|
|---|
| 307 | groups of context lines) with escape sequences to display them in color
|
|---|
| 308 | on the terminal.
|
|---|
| 309 | The colors are defined by the environment variable
|
|---|
| 310 | .BR GREP_COLORS .
|
|---|
| 311 | The deprecated environment variable
|
|---|
| 312 | .B GREP_COLOR
|
|---|
| 313 | is still supported, but its setting does not have priority.
|
|---|
| 314 | .I WHEN
|
|---|
| 315 | is
|
|---|
| 316 | .BR never ", " always ", or " auto .
|
|---|
| 317 | .TP
|
|---|
| 318 | .BR \-L ", " \-\^\-files\-without\-match
|
|---|
| 319 | Suppress normal output; instead print the name
|
|---|
| 320 | of each input file from which no output would
|
|---|
| 321 | normally have been printed.
|
|---|
| 322 | .TP
|
|---|
| 323 | .BR \-l ", " \-\^\-files\-with\-matches
|
|---|
| 324 | Suppress normal output; instead print
|
|---|
| 325 | the name of each input file from which output
|
|---|
| 326 | would normally have been printed.
|
|---|
| 327 | Scanning each input file stops upon first match.
|
|---|
| 328 | .TP
|
|---|
| 329 | .BI \-m " NUM" "\fR,\fP \-\^\-max\-count=" NUM
|
|---|
| 330 | Stop reading a file after
|
|---|
| 331 | .I NUM
|
|---|
| 332 | matching lines.
|
|---|
| 333 | If the input is standard input from a regular file,
|
|---|
| 334 | and
|
|---|
| 335 | .I NUM
|
|---|
| 336 | matching lines are output,
|
|---|
| 337 | .B grep
|
|---|
| 338 | ensures that the standard input is positioned to just after the last
|
|---|
| 339 | matching line before exiting, regardless of the presence of trailing
|
|---|
| 340 | context lines.
|
|---|
| 341 | This enables a calling process to resume a search.
|
|---|
| 342 | When
|
|---|
| 343 | .B grep
|
|---|
| 344 | stops after
|
|---|
| 345 | .I NUM
|
|---|
| 346 | matching lines, it outputs any trailing context lines.
|
|---|
| 347 | When the
|
|---|
| 348 | .B \-c
|
|---|
| 349 | or
|
|---|
| 350 | .B \-\^\-count
|
|---|
| 351 | option is also used,
|
|---|
| 352 | .B grep
|
|---|
| 353 | does not output a count greater than
|
|---|
| 354 | .IR NUM .
|
|---|
| 355 | When the
|
|---|
| 356 | .B \-v
|
|---|
| 357 | or
|
|---|
| 358 | .B \-\^\-invert\-match
|
|---|
| 359 | option is also used,
|
|---|
| 360 | .B grep
|
|---|
| 361 | stops after outputting
|
|---|
| 362 | .I NUM
|
|---|
| 363 | non-matching lines.
|
|---|
| 364 | .TP
|
|---|
| 365 | .BR \-o ", " \-\^\-only\-matching
|
|---|
| 366 | Print only the matched (non-empty) parts of a matching line,
|
|---|
| 367 | with each such part on a separate output line.
|
|---|
| 368 | .TP
|
|---|
| 369 | .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
|
|---|
| 370 | Quiet; do not write anything to standard output.
|
|---|
| 371 | Exit immediately with zero status if any match is found,
|
|---|
| 372 | even if an error was detected.
|
|---|
| 373 | Also see the
|
|---|
| 374 | .B \-s
|
|---|
| 375 | or
|
|---|
| 376 | .B \-\^\-no\-messages
|
|---|
| 377 | option.
|
|---|
| 378 | .TP
|
|---|
| 379 | .BR \-s ", " \-\^\-no\-messages
|
|---|
| 380 | Suppress error messages about nonexistent or unreadable files.
|
|---|
| 381 | .SS "Output Line Prefix Control"
|
|---|
| 382 | .TP
|
|---|
| 383 | .BR \-b ", " \-\^\-byte\-offset
|
|---|
| 384 | Print the 0-based byte offset within the input file
|
|---|
| 385 | before each line of output.
|
|---|
| 386 | If
|
|---|
| 387 | .B \-o
|
|---|
| 388 | .RB ( \-\^\-only\-matching )
|
|---|
| 389 | is specified,
|
|---|
| 390 | print the offset of the matching part itself.
|
|---|
| 391 | .TP
|
|---|
| 392 | .BR \-H ", " \-\^\-with\-filename
|
|---|
| 393 | Print the file name for each match.
|
|---|
| 394 | This is the default when there is more than one file to search.
|
|---|
| 395 | This is a GNU extension.
|
|---|
| 396 | .TP
|
|---|
| 397 | .BR \-h ", " \-\^\-no\-filename
|
|---|
| 398 | Suppress the prefixing of file names on output.
|
|---|
| 399 | This is the default when there is only one file
|
|---|
| 400 | (or only standard input) to search.
|
|---|
| 401 | .TP
|
|---|
| 402 | .BI \-\^\-label= LABEL
|
|---|
| 403 | Display input actually coming from standard input as input coming from file
|
|---|
| 404 | .IR LABEL .
|
|---|
| 405 | This can be useful for commands that transform a file's contents
|
|---|
| 406 | before searching,
|
|---|
| 407 | e.g.,
|
|---|
| 408 | .BR "gzip \-cd foo.gz | grep \-\^\-label=foo \-H 'some pattern'" .
|
|---|
| 409 | See also the
|
|---|
| 410 | .B \-H
|
|---|
| 411 | option.
|
|---|
| 412 | .TP
|
|---|
| 413 | .BR \-n ", " \-\^\-line\-number
|
|---|
| 414 | Prefix each line of output with the 1-based line number
|
|---|
| 415 | within its input file.
|
|---|
| 416 | .TP
|
|---|
| 417 | .BR \-T ", " \-\^\-initial\-tab
|
|---|
| 418 | Make sure that the first character of actual line content lies on a
|
|---|
| 419 | tab stop, so that the alignment of tabs looks normal.
|
|---|
| 420 | This is useful with options that prefix their output to the actual content:
|
|---|
| 421 | .BR \-H , \-n ,
|
|---|
| 422 | and
|
|---|
| 423 | .BR \-b .
|
|---|
| 424 | In order to improve the probability that lines
|
|---|
| 425 | from a single file will all start at the same column,
|
|---|
| 426 | this also causes the line number and byte offset (if present)
|
|---|
| 427 | to be printed in a minimum size field width.
|
|---|
| 428 | .TP
|
|---|
| 429 | .BR \-Z ", " \-\^\-null
|
|---|
| 430 | Output a zero byte (the ASCII
|
|---|
| 431 | .B NUL
|
|---|
| 432 | character) instead of the character that normally follows a file name.
|
|---|
| 433 | For example,
|
|---|
| 434 | .B "grep \-lZ"
|
|---|
| 435 | outputs a zero byte after each file name instead of the usual newline.
|
|---|
| 436 | This option makes the output unambiguous, even in the presence of file
|
|---|
| 437 | names containing unusual characters like newlines.
|
|---|
| 438 | This option can be used with commands like
|
|---|
| 439 | .BR "find \-print0" ,
|
|---|
| 440 | .BR "perl \-0" ,
|
|---|
| 441 | .BR "sort \-z" ,
|
|---|
| 442 | and
|
|---|
| 443 | .B "xargs \-0"
|
|---|
| 444 | to process arbitrary file names,
|
|---|
| 445 | even those that contain newline characters.
|
|---|
| 446 | .SS "Context Line Control"
|
|---|
| 447 | .TP
|
|---|
| 448 | .BI \-A " NUM" "\fR,\fP \-\^\-after\-context=" NUM
|
|---|
| 449 | Print
|
|---|
| 450 | .I NUM
|
|---|
| 451 | lines of trailing context after matching lines.
|
|---|
| 452 | Places a line containing a group separator
|
|---|
| 453 | .RB ( \-\^\- )
|
|---|
| 454 | between contiguous groups of matches.
|
|---|
| 455 | With the
|
|---|
| 456 | .B \-o
|
|---|
| 457 | or
|
|---|
| 458 | .B \-\^\-only\-matching
|
|---|
| 459 | option, this has no effect and a warning is given.
|
|---|
| 460 | .TP
|
|---|
| 461 | .BI \-B " NUM" "\fR,\fP \-\^\-before\-context=" NUM
|
|---|
| 462 | Print
|
|---|
| 463 | .I NUM
|
|---|
| 464 | lines of leading context before matching lines.
|
|---|
| 465 | Places a line containing a group separator
|
|---|
| 466 | .RB ( \-\^\- )
|
|---|
| 467 | between contiguous groups of matches.
|
|---|
| 468 | With the
|
|---|
| 469 | .B \-o
|
|---|
| 470 | or
|
|---|
| 471 | .B \-\^\-only\-matching
|
|---|
| 472 | option, this has no effect and a warning is given.
|
|---|
| 473 | .TP
|
|---|
| 474 | .BI \-C " NUM" "\fR,\fP \-" NUM "\fR,\fP \-\^\-context=" NUM
|
|---|
| 475 | Print
|
|---|
| 476 | .I NUM
|
|---|
| 477 | lines of output context.
|
|---|
| 478 | Places a line containing a group separator
|
|---|
| 479 | .RB ( \-\^\- )
|
|---|
| 480 | between contiguous groups of matches.
|
|---|
| 481 | With the
|
|---|
| 482 | .B \-o
|
|---|
| 483 | or
|
|---|
| 484 | .B \-\^\-only\-matching
|
|---|
| 485 | option, this has no effect and a warning is given.
|
|---|
| 486 | .TP
|
|---|
| 487 | .BI \-\^\-group\-separator= SEP
|
|---|
| 488 | When
|
|---|
| 489 | .BR \-A ,
|
|---|
| 490 | .BR \-B ,
|
|---|
| 491 | or
|
|---|
| 492 | .B \-C
|
|---|
| 493 | are in use, print
|
|---|
| 494 | .I SEP
|
|---|
| 495 | instead of
|
|---|
| 496 | .B \-\^\-
|
|---|
| 497 | between groups of lines.
|
|---|
| 498 | .TP
|
|---|
| 499 | .B \-\^\-no\-group\-separator
|
|---|
| 500 | When
|
|---|
| 501 | .BR \-A ,
|
|---|
| 502 | .BR \-B ,
|
|---|
| 503 | or
|
|---|
| 504 | .B \-C
|
|---|
| 505 | are in use, do not print a separator between groups of lines.
|
|---|
| 506 | .SS "File and Directory Selection"
|
|---|
| 507 | .TP
|
|---|
| 508 | .BR \-a ", " \-\^\-text
|
|---|
| 509 | Process a binary file as if it were text; this is equivalent to the
|
|---|
| 510 | .B \-\^\-binary\-files=text
|
|---|
| 511 | option.
|
|---|
| 512 | .TP
|
|---|
| 513 | .BI \-\^\-binary\-files= TYPE
|
|---|
| 514 | If a file's data or metadata
|
|---|
| 515 | indicate that the file contains binary data,
|
|---|
| 516 | assume that the file is of type
|
|---|
| 517 | .IR TYPE .
|
|---|
| 518 | Non-text bytes indicate binary data; these are either output bytes that are
|
|---|
| 519 | improperly encoded for the current locale, or null input bytes when the
|
|---|
| 520 | .B \-z
|
|---|
| 521 | option is not given.
|
|---|
| 522 | .IP
|
|---|
| 523 | By default,
|
|---|
| 524 | .I TYPE
|
|---|
| 525 | is
|
|---|
| 526 | .BR binary ,
|
|---|
| 527 | and
|
|---|
| 528 | .B grep
|
|---|
| 529 | suppresses output after null input binary data is discovered,
|
|---|
| 530 | and suppresses output lines that contain improperly encoded data.
|
|---|
| 531 | When some output is suppressed,
|
|---|
| 532 | .B grep
|
|---|
| 533 | follows any output
|
|---|
| 534 | with a one-line message saying that a binary file matches.
|
|---|
| 535 | .IP
|
|---|
| 536 | If
|
|---|
| 537 | .I TYPE
|
|---|
| 538 | is
|
|---|
| 539 | .BR without\-match ,
|
|---|
| 540 | when
|
|---|
| 541 | .B grep
|
|---|
| 542 | discovers null input binary data it assumes that the rest of the file
|
|---|
| 543 | does not match; this is equivalent to the
|
|---|
| 544 | .B \-I
|
|---|
| 545 | option.
|
|---|
| 546 | .IP
|
|---|
| 547 | If
|
|---|
| 548 | .I TYPE
|
|---|
| 549 | is
|
|---|
| 550 | .BR text ,
|
|---|
| 551 | .B grep
|
|---|
| 552 | processes a binary file as if it were text; this is equivalent to the
|
|---|
| 553 | .B \-a
|
|---|
| 554 | option.
|
|---|
| 555 | .IP
|
|---|
| 556 | When
|
|---|
| 557 | .I type
|
|---|
| 558 | is
|
|---|
| 559 | .BR binary ,
|
|---|
| 560 | .B grep
|
|---|
| 561 | may treat non-text bytes as line terminators even without the
|
|---|
| 562 | .B \-z
|
|---|
| 563 | option. This means choosing
|
|---|
| 564 | .B binary
|
|---|
| 565 | versus
|
|---|
| 566 | .B text
|
|---|
| 567 | can affect whether a pattern matches a file. For
|
|---|
| 568 | example, when
|
|---|
| 569 | .I type
|
|---|
| 570 | is
|
|---|
| 571 | .B binary
|
|---|
| 572 | the pattern
|
|---|
| 573 | .B q$ might
|
|---|
| 574 | match
|
|---|
| 575 | .B q
|
|---|
| 576 | immediately followed by a null byte, even though this
|
|---|
| 577 | is not matched when
|
|---|
| 578 | .I type
|
|---|
| 579 | is
|
|---|
| 580 | .BR text .
|
|---|
| 581 | Conversely, when
|
|---|
| 582 | .I type
|
|---|
| 583 | is
|
|---|
| 584 | .B binary
|
|---|
| 585 | the pattern
|
|---|
| 586 | .B .\&
|
|---|
| 587 | (period) might not match a null byte.
|
|---|
| 588 | .IP
|
|---|
| 589 | .I Warning:
|
|---|
| 590 | The
|
|---|
| 591 | .B \-a
|
|---|
| 592 | option might output binary garbage,
|
|---|
| 593 | which can have nasty side effects if the output is a terminal and if the
|
|---|
| 594 | terminal driver interprets some of it as commands.
|
|---|
| 595 | On the other hand, when reading files whose text encodings are
|
|---|
| 596 | unknown, it can be helpful to use
|
|---|
| 597 | .B \-a
|
|---|
| 598 | or to set
|
|---|
| 599 | .B LC_ALL='C'
|
|---|
| 600 | in the environment, in order to find more matches even if the matches
|
|---|
| 601 | are unsafe for direct display.
|
|---|
| 602 | .TP
|
|---|
| 603 | .BI \-D " ACTION" "\fR,\fP \-\^\-devices=" ACTION
|
|---|
| 604 | If an input file is a device, FIFO or socket, use
|
|---|
| 605 | .I ACTION
|
|---|
| 606 | to process it.
|
|---|
| 607 | By default,
|
|---|
| 608 | .I ACTION
|
|---|
| 609 | is
|
|---|
| 610 | .BR read ,
|
|---|
| 611 | which means that devices are read just as if they were ordinary files.
|
|---|
| 612 | If
|
|---|
| 613 | .I ACTION
|
|---|
| 614 | is
|
|---|
| 615 | .BR skip ,
|
|---|
| 616 | devices are silently skipped.
|
|---|
| 617 | .TP
|
|---|
| 618 | .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
|
|---|
| 619 | If an input file is a directory, use
|
|---|
| 620 | .I ACTION
|
|---|
| 621 | to process it.
|
|---|
| 622 | By default,
|
|---|
| 623 | .I ACTION
|
|---|
| 624 | is
|
|---|
| 625 | .BR read ,
|
|---|
| 626 | i.e., read directories just as if they were ordinary files.
|
|---|
| 627 | If
|
|---|
| 628 | .I ACTION
|
|---|
| 629 | is
|
|---|
| 630 | .BR skip ,
|
|---|
| 631 | silently skip directories.
|
|---|
| 632 | If
|
|---|
| 633 | .I ACTION
|
|---|
| 634 | is
|
|---|
| 635 | .BR recurse ,
|
|---|
| 636 | read all files under each directory, recursively,
|
|---|
| 637 | following symbolic links only if they are on the command line.
|
|---|
| 638 | This is equivalent to the
|
|---|
| 639 | .B \-r
|
|---|
| 640 | option.
|
|---|
| 641 | .TP
|
|---|
| 642 | .BI \-\^\-exclude= GLOB
|
|---|
| 643 | Skip any command-line file with a name suffix that matches the pattern
|
|---|
| 644 | .IR GLOB ,
|
|---|
| 645 | using wildcard matching; a name suffix is either the whole
|
|---|
| 646 | name, or a trailing part that starts with a non-slash character
|
|---|
| 647 | immediately after a slash
|
|---|
| 648 | .RB ( / )
|
|---|
| 649 | in the name.
|
|---|
| 650 | When searching recursively, skip any subfile whose base name matches
|
|---|
| 651 | .IR GLOB ;
|
|---|
| 652 | the base name is the part after the last slash.
|
|---|
| 653 | A pattern can use
|
|---|
| 654 | .BR * ,
|
|---|
| 655 | .BR ? ,
|
|---|
| 656 | and
|
|---|
| 657 | .BR [ .\|.\|. ]\&
|
|---|
| 658 | as wildcards, and
|
|---|
| 659 | .B \e
|
|---|
| 660 | to quote a wildcard or backslash character literally.
|
|---|
| 661 | .TP
|
|---|
| 662 | .BI \-\^\-exclude\-from= FILE
|
|---|
| 663 | Skip files whose base name matches any of the file-name globs read from
|
|---|
| 664 | .I FILE
|
|---|
| 665 | (using wildcard matching as described under
|
|---|
| 666 | .BR \-\^\-exclude ).
|
|---|
| 667 | .TP
|
|---|
| 668 | .BI \-\^\-exclude\-dir= GLOB
|
|---|
| 669 | Skip any command-line directory with a name suffix that matches the
|
|---|
| 670 | pattern
|
|---|
| 671 | .IR GLOB .
|
|---|
| 672 | When searching recursively, skip any subdirectory
|
|---|
| 673 | whose base name matches
|
|---|
| 674 | .IR GLOB .
|
|---|
| 675 | Ignore any redundant trailing slashes in
|
|---|
| 676 | .IR GLOB .
|
|---|
| 677 | .TP
|
|---|
| 678 | .BR \-I
|
|---|
| 679 | Process a binary file as if it did not contain matching data; this is
|
|---|
| 680 | equivalent to the
|
|---|
| 681 | .B \-\^\-binary\-files=without\-match
|
|---|
| 682 | option.
|
|---|
| 683 | .TP
|
|---|
| 684 | .BI \-\^\-include= GLOB
|
|---|
| 685 | Search only files whose base name matches
|
|---|
| 686 | .I GLOB
|
|---|
| 687 | (using wildcard matching as described under
|
|---|
| 688 | .BR \-\^\-exclude ).
|
|---|
| 689 | If contradictory
|
|---|
| 690 | .B \-\^\-include
|
|---|
| 691 | and
|
|---|
| 692 | .B \-\^\-exclude
|
|---|
| 693 | options are given, the last matching one wins.
|
|---|
| 694 | If no
|
|---|
| 695 | .B \-\^\-include
|
|---|
| 696 | or
|
|---|
| 697 | .B \-\^\-exclude
|
|---|
| 698 | options match, a file is included unless the first such option is
|
|---|
| 699 | .BR \-\^\-include .
|
|---|
| 700 | .TP
|
|---|
| 701 | .BR \-r ", " \-\^\-recursive
|
|---|
| 702 | Read all files under each directory, recursively,
|
|---|
| 703 | following symbolic links only if they are on the command line.
|
|---|
| 704 | Note that if no file operand is given, B<grep> searches the working directory.
|
|---|
| 705 | This is equivalent to the
|
|---|
| 706 | .B "\-d recurse"
|
|---|
| 707 | option.
|
|---|
| 708 | .TP
|
|---|
| 709 | .BR \-R ", " \-\^\-dereference\-recursive
|
|---|
| 710 | Read all files under each directory, recursively.
|
|---|
| 711 | Follow all symbolic links, unlike
|
|---|
| 712 | .BR \-r .
|
|---|
| 713 | .SS "Other Options"
|
|---|
| 714 | .TP
|
|---|
| 715 | .B \-\^\-line\-buffered
|
|---|
| 716 | Use line buffering on output.
|
|---|
| 717 | This can cause a performance penalty.
|
|---|
| 718 | .TP
|
|---|
| 719 | .BR \-U ", " \-\^\-binary
|
|---|
| 720 | Treat the file(s) as binary.
|
|---|
| 721 | By default, under MS-DOS and MS-Windows,
|
|---|
| 722 | .BR grep
|
|---|
| 723 | guesses whether a file is text or binary as described for the
|
|---|
| 724 | .B \-\^\-binary\-files
|
|---|
| 725 | option.
|
|---|
| 726 | If
|
|---|
| 727 | .BR grep
|
|---|
| 728 | decides the file is a text file, it strips the CR characters from the
|
|---|
| 729 | original file contents (to make regular expressions with
|
|---|
| 730 | .B ^
|
|---|
| 731 | and
|
|---|
| 732 | .B $
|
|---|
| 733 | work correctly).
|
|---|
| 734 | Specifying
|
|---|
| 735 | .B \-U
|
|---|
| 736 | overrules this guesswork, causing all files to be read and passed to the
|
|---|
| 737 | matching mechanism verbatim; if the file is a text file with CR/LF
|
|---|
| 738 | pairs at the end of each line, this will cause some regular
|
|---|
| 739 | expressions to fail.
|
|---|
| 740 | This option has no effect on platforms
|
|---|
| 741 | other than MS-DOS and MS-Windows.
|
|---|
| 742 | .TP
|
|---|
| 743 | .BR \-z ", " \-\^\-null\-data
|
|---|
| 744 | Treat input and output data as sequences of lines, each terminated by
|
|---|
| 745 | a zero byte (the ASCII NUL character) instead of a newline.
|
|---|
| 746 | Like the
|
|---|
| 747 | .B \-Z
|
|---|
| 748 | or
|
|---|
| 749 | .B \-\^\-null
|
|---|
| 750 | option, this option can be used with commands like
|
|---|
| 751 | .B sort -z
|
|---|
| 752 | to process arbitrary file names.
|
|---|
| 753 | .
|
|---|
| 754 | .SH "REGULAR EXPRESSIONS"
|
|---|
| 755 | A regular expression is a pattern that describes a set of strings.
|
|---|
| 756 | Regular expressions are constructed analogously to arithmetic
|
|---|
| 757 | expressions, by using various operators to combine smaller expressions.
|
|---|
| 758 | .PP
|
|---|
| 759 | .B grep
|
|---|
| 760 | understands three different versions of regular expression syntax:
|
|---|
| 761 | \*(lqbasic\*(rq (BRE), \*(lqextended\*(rq (ERE) and \*(lqperl\*(rq (PCRE).
|
|---|
| 762 | In GNU
|
|---|
| 763 | .B grep
|
|---|
| 764 | there is no difference in available functionality between basic and
|
|---|
| 765 | extended syntaxes.
|
|---|
| 766 | In other implementations, basic regular expressions are less powerful.
|
|---|
| 767 | The following description applies to extended regular expressions;
|
|---|
| 768 | differences for basic regular expressions are summarized afterwards.
|
|---|
| 769 | Perl-compatible regular expressions give additional functionality, and are
|
|---|
| 770 | documented in B<pcresyntax>(3) and B<pcrepattern>(3), but work only if
|
|---|
| 771 | PCRE support is enabled.
|
|---|
| 772 | .PP
|
|---|
| 773 | The fundamental building blocks are the regular expressions
|
|---|
| 774 | that match a single character.
|
|---|
| 775 | Most characters, including all letters and digits,
|
|---|
| 776 | are regular expressions that match themselves.
|
|---|
| 777 | Any meta-character with special meaning
|
|---|
| 778 | may be quoted by preceding it with a backslash.
|
|---|
| 779 | .PP
|
|---|
| 780 | The period
|
|---|
| 781 | .B .\&
|
|---|
| 782 | matches any single character.
|
|---|
| 783 | It is unspecified whether it matches an encoding error.
|
|---|
| 784 | .SS "Character Classes and Bracket Expressions"
|
|---|
| 785 | A
|
|---|
| 786 | .I "bracket expression"
|
|---|
| 787 | is a list of characters enclosed by
|
|---|
| 788 | .B [
|
|---|
| 789 | and
|
|---|
| 790 | .BR ] .
|
|---|
| 791 | It matches any single
|
|---|
| 792 | character in that list.
|
|---|
| 793 | If the first character of the list
|
|---|
| 794 | is the caret
|
|---|
| 795 | .B ^
|
|---|
| 796 | then it matches any character
|
|---|
| 797 | .I not
|
|---|
| 798 | in the list; it is unspecified whether it matches an encoding error.
|
|---|
| 799 | For example, the regular expression
|
|---|
| 800 | .B [0123456789]
|
|---|
| 801 | matches any single digit.
|
|---|
| 802 | .PP
|
|---|
| 803 | Within a bracket expression, a
|
|---|
| 804 | .I "range expression"
|
|---|
| 805 | consists of two characters separated by a hyphen.
|
|---|
| 806 | It matches any single character that sorts between the two characters,
|
|---|
| 807 | inclusive, using the locale's collating sequence and character set.
|
|---|
| 808 | For example, in the default C locale,
|
|---|
| 809 | .B [a\-d]
|
|---|
| 810 | is equivalent to
|
|---|
| 811 | .BR [abcd] .
|
|---|
| 812 | Many locales sort characters in dictionary order, and in these locales
|
|---|
| 813 | .B [a\-d]
|
|---|
| 814 | is typically not equivalent to
|
|---|
| 815 | .BR [abcd] ;
|
|---|
| 816 | it might be equivalent to
|
|---|
| 817 | .BR [aBbCcDd] ,
|
|---|
| 818 | for example.
|
|---|
| 819 | To obtain the traditional interpretation of bracket expressions,
|
|---|
| 820 | you can use the C locale by setting the
|
|---|
| 821 | .B LC_ALL
|
|---|
| 822 | environment variable to the value
|
|---|
| 823 | .BR C .
|
|---|
| 824 | .PP
|
|---|
| 825 | Finally, certain named classes of characters are predefined within
|
|---|
| 826 | bracket expressions, as follows.
|
|---|
| 827 | Their names are self explanatory, and they are
|
|---|
| 828 | .BR [:alnum:] ,
|
|---|
| 829 | .BR [:alpha:] ,
|
|---|
| 830 | .BR [:blank:] ,
|
|---|
| 831 | .BR [:cntrl:] ,
|
|---|
| 832 | .BR [:digit:] ,
|
|---|
| 833 | .BR [:graph:] ,
|
|---|
| 834 | .BR [:lower:] ,
|
|---|
| 835 | .BR [:print:] ,
|
|---|
| 836 | .BR [:punct:] ,
|
|---|
| 837 | .BR [:space:] ,
|
|---|
| 838 | .BR [:upper:] ,
|
|---|
| 839 | and
|
|---|
| 840 | .BR [:xdigit:] .
|
|---|
| 841 | For example,
|
|---|
| 842 | .B [[:alnum:]]
|
|---|
| 843 | means the character class of numbers and
|
|---|
| 844 | letters in the current locale.
|
|---|
| 845 | In the C locale and ASCII
|
|---|
| 846 | character set encoding, this is the same as
|
|---|
| 847 | .BR [0\-9A\-Za\-z] .
|
|---|
| 848 | (Note that the brackets in these class names are part of the symbolic
|
|---|
| 849 | names, and must be included in addition to the brackets delimiting
|
|---|
| 850 | the bracket expression.)
|
|---|
| 851 | Most meta-characters lose their special meaning inside bracket expressions.
|
|---|
| 852 | To include a literal
|
|---|
| 853 | .B ]
|
|---|
| 854 | place it first in the list.
|
|---|
| 855 | Similarly, to include a literal
|
|---|
| 856 | .B ^
|
|---|
| 857 | place it anywhere but first.
|
|---|
| 858 | Finally, to include a literal
|
|---|
| 859 | .B \-
|
|---|
| 860 | place it last.
|
|---|
| 861 | .SS Anchoring
|
|---|
| 862 | The caret
|
|---|
| 863 | .B ^
|
|---|
| 864 | and the dollar sign
|
|---|
| 865 | .B $
|
|---|
| 866 | are meta-characters that respectively match the empty string at the
|
|---|
| 867 | beginning and end of a line.
|
|---|
| 868 | .SS "The Backslash Character and Special Expressions"
|
|---|
| 869 | The symbols
|
|---|
| 870 | .B \e<
|
|---|
| 871 | and
|
|---|
| 872 | .B \e>
|
|---|
| 873 | respectively match the empty string at the beginning and end of a word.
|
|---|
| 874 | The symbol
|
|---|
| 875 | .B \eb
|
|---|
| 876 | matches the empty string at the edge of a word,
|
|---|
| 877 | and
|
|---|
| 878 | .B \eB
|
|---|
| 879 | matches the empty string provided it's
|
|---|
| 880 | .I not
|
|---|
| 881 | at the edge of a word.
|
|---|
| 882 | The symbol
|
|---|
| 883 | .B \ew
|
|---|
| 884 | is a synonym for
|
|---|
| 885 | .B [_[:alnum:]]
|
|---|
| 886 | and
|
|---|
| 887 | .B \eW
|
|---|
| 888 | is a synonym for
|
|---|
| 889 | .BR [^_[:alnum:]] .
|
|---|
| 890 | .SS Repetition
|
|---|
| 891 | A regular expression may be followed by one of several repetition operators:
|
|---|
| 892 | .PD 0
|
|---|
| 893 | .TP
|
|---|
| 894 | .B ?
|
|---|
| 895 | The preceding item is optional and matched at most once.
|
|---|
| 896 | .TP
|
|---|
| 897 | .B *
|
|---|
| 898 | The preceding item will be matched zero or more times.
|
|---|
| 899 | .TP
|
|---|
| 900 | .B +
|
|---|
| 901 | The preceding item will be matched one or more times.
|
|---|
| 902 | .TP
|
|---|
| 903 | .BI { n }
|
|---|
| 904 | The preceding item is matched exactly
|
|---|
| 905 | .I n
|
|---|
| 906 | times.
|
|---|
| 907 | .TP
|
|---|
| 908 | .BI { n ,}
|
|---|
| 909 | The preceding item is matched
|
|---|
| 910 | .I n
|
|---|
| 911 | or more times.
|
|---|
| 912 | .TP
|
|---|
| 913 | .BI {, m }
|
|---|
| 914 | The preceding item is matched at most
|
|---|
| 915 | .I m
|
|---|
| 916 | times.
|
|---|
| 917 | This is a GNU extension.
|
|---|
| 918 | .TP
|
|---|
| 919 | .BI { n , m }
|
|---|
| 920 | The preceding item is matched at least
|
|---|
| 921 | .I n
|
|---|
| 922 | times, but not more than
|
|---|
| 923 | .I m
|
|---|
| 924 | times.
|
|---|
| 925 | .PD
|
|---|
| 926 | .SS Concatenation
|
|---|
| 927 | Two regular expressions may be concatenated; the resulting
|
|---|
| 928 | regular expression matches any string formed by concatenating
|
|---|
| 929 | two substrings that respectively match the concatenated
|
|---|
| 930 | expressions.
|
|---|
| 931 | .SS Alternation
|
|---|
| 932 | Two regular expressions may be joined by the infix operator
|
|---|
| 933 | .BR | ;
|
|---|
| 934 | the resulting regular expression matches any string matching
|
|---|
| 935 | either alternate expression.
|
|---|
| 936 | .SS Precedence
|
|---|
| 937 | Repetition takes precedence over concatenation, which in turn
|
|---|
| 938 | takes precedence over alternation.
|
|---|
| 939 | A whole expression may be enclosed in parentheses
|
|---|
| 940 | to override these precedence rules and form a subexpression.
|
|---|
| 941 | .SS "Back-references and Subexpressions"
|
|---|
| 942 | The back-reference
|
|---|
| 943 | .BI \e n\c
|
|---|
| 944 | \&, where
|
|---|
| 945 | .I n
|
|---|
| 946 | is a single digit, matches the substring
|
|---|
| 947 | previously matched by the
|
|---|
| 948 | .IR n th
|
|---|
| 949 | parenthesized subexpression of the regular expression.
|
|---|
| 950 | .SS "Basic vs Extended Regular Expressions"
|
|---|
| 951 | In basic regular expressions the meta-characters
|
|---|
| 952 | .BR ? ,
|
|---|
| 953 | .BR + ,
|
|---|
| 954 | .BR { ,
|
|---|
| 955 | .BR | ,
|
|---|
| 956 | .BR ( ,
|
|---|
| 957 | and
|
|---|
| 958 | .BR )
|
|---|
| 959 | lose their special meaning; instead use the backslashed
|
|---|
| 960 | versions
|
|---|
| 961 | .BR \e? ,
|
|---|
| 962 | .BR \e+ ,
|
|---|
| 963 | .BR \e{ ,
|
|---|
| 964 | .BR \e| ,
|
|---|
| 965 | .BR \e( ,
|
|---|
| 966 | and
|
|---|
| 967 | .BR \e) .
|
|---|
| 968 | .
|
|---|
| 969 | .SH "EXIT STATUS"
|
|---|
| 970 | Normally the exit status is 0 if a line is selected, 1 if no lines
|
|---|
| 971 | were selected, and 2 if an error occurred. However, if the
|
|---|
| 972 | .B \-q
|
|---|
| 973 | or
|
|---|
| 974 | .B \-\^\-quiet
|
|---|
| 975 | or
|
|---|
| 976 | .B \-\^\-silent
|
|---|
| 977 | is used and a line is selected, the exit status is 0 even if an error
|
|---|
| 978 | occurred.
|
|---|
| 979 | .
|
|---|
| 980 | .SH ENVIRONMENT
|
|---|
| 981 | The behavior of
|
|---|
| 982 | .B grep
|
|---|
| 983 | is affected by the following environment variables.
|
|---|
| 984 | .PP
|
|---|
| 985 | The locale for category
|
|---|
| 986 | .BI LC_ foo
|
|---|
| 987 | is specified by examining the three environment variables
|
|---|
| 988 | .BR LC_ALL ,
|
|---|
| 989 | .BR LC_\fIfoo\fP ,
|
|---|
| 990 | .BR LANG ,
|
|---|
| 991 | in that order.
|
|---|
| 992 | The first of these variables that is set specifies the locale.
|
|---|
| 993 | For example, if
|
|---|
| 994 | .B LC_ALL
|
|---|
| 995 | is not set, but
|
|---|
| 996 | .B LC_MESSAGES
|
|---|
| 997 | is set to
|
|---|
| 998 | .BR pt_BR ,
|
|---|
| 999 | then the Brazilian Portuguese locale is used for the
|
|---|
| 1000 | .B LC_MESSAGES
|
|---|
| 1001 | category.
|
|---|
| 1002 | The C locale is used if none of these environment variables are set,
|
|---|
| 1003 | if the locale catalog is not installed, or if
|
|---|
| 1004 | .B grep
|
|---|
| 1005 | was not compiled with national language support (NLS).
|
|---|
| 1006 | The shell command
|
|---|
| 1007 | .B "locale \-a"
|
|---|
| 1008 | lists locales that are currently available.
|
|---|
| 1009 | .TP
|
|---|
| 1010 | .B GREP_COLOR
|
|---|
| 1011 | This variable specifies the color used to highlight matched (non-empty) text.
|
|---|
| 1012 | It is deprecated in favor of
|
|---|
| 1013 | .BR GREP_COLORS ,
|
|---|
| 1014 | but still supported.
|
|---|
| 1015 | The
|
|---|
| 1016 | .BR mt ,
|
|---|
| 1017 | .BR ms ,
|
|---|
| 1018 | and
|
|---|
| 1019 | .B mc
|
|---|
| 1020 | capabilities of
|
|---|
| 1021 | .B GREP_COLORS
|
|---|
| 1022 | have priority over it.
|
|---|
| 1023 | It can only specify the color used to highlight
|
|---|
| 1024 | the matching non-empty text in any matching line
|
|---|
| 1025 | (a selected line when the
|
|---|
| 1026 | .B \-v
|
|---|
| 1027 | command-line option is omitted,
|
|---|
| 1028 | or a context line when
|
|---|
| 1029 | .B \-v
|
|---|
| 1030 | is specified).
|
|---|
| 1031 | The default is
|
|---|
| 1032 | .BR 01;31 ,
|
|---|
| 1033 | which means a bold red foreground text on the terminal's default background.
|
|---|
| 1034 | .TP
|
|---|
| 1035 | .B GREP_COLORS
|
|---|
| 1036 | Specifies the colors and other attributes
|
|---|
| 1037 | used to highlight various parts of the output.
|
|---|
| 1038 | Its value is a colon-separated list of capabilities
|
|---|
| 1039 | that defaults to
|
|---|
| 1040 | .B ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36
|
|---|
| 1041 | with the
|
|---|
| 1042 | .B rv
|
|---|
| 1043 | and
|
|---|
| 1044 | .B ne
|
|---|
| 1045 | boolean capabilities omitted (i.e., false).
|
|---|
| 1046 | Supported capabilities are as follows.
|
|---|
| 1047 | .RS
|
|---|
| 1048 | .TP
|
|---|
| 1049 | .B sl=
|
|---|
| 1050 | SGR substring for whole selected lines
|
|---|
| 1051 | (i.e.,
|
|---|
| 1052 | matching lines when the
|
|---|
| 1053 | .B \-v
|
|---|
| 1054 | command-line option is omitted,
|
|---|
| 1055 | or non-matching lines when
|
|---|
| 1056 | .B \-v
|
|---|
| 1057 | is specified).
|
|---|
| 1058 | If however the boolean
|
|---|
| 1059 | .B rv
|
|---|
| 1060 | capability
|
|---|
| 1061 | and the
|
|---|
| 1062 | .B \-v
|
|---|
| 1063 | command-line option are both specified,
|
|---|
| 1064 | it applies to context matching lines instead.
|
|---|
| 1065 | The default is empty (i.e., the terminal's default color pair).
|
|---|
| 1066 | .TP
|
|---|
| 1067 | .B cx=
|
|---|
| 1068 | SGR substring for whole context lines
|
|---|
| 1069 | (i.e.,
|
|---|
| 1070 | non-matching lines when the
|
|---|
| 1071 | .B \-v
|
|---|
| 1072 | command-line option is omitted,
|
|---|
| 1073 | or matching lines when
|
|---|
| 1074 | .B \-v
|
|---|
| 1075 | is specified).
|
|---|
| 1076 | If however the boolean
|
|---|
| 1077 | .B rv
|
|---|
| 1078 | capability
|
|---|
| 1079 | and the
|
|---|
| 1080 | .B \-v
|
|---|
| 1081 | command-line option are both specified,
|
|---|
| 1082 | it applies to selected non-matching lines instead.
|
|---|
| 1083 | The default is empty (i.e., the terminal's default color pair).
|
|---|
| 1084 | .TP
|
|---|
| 1085 | .B rv
|
|---|
| 1086 | Boolean value that reverses (swaps) the meanings of
|
|---|
| 1087 | the
|
|---|
| 1088 | .B sl=
|
|---|
| 1089 | and
|
|---|
| 1090 | .B cx=
|
|---|
| 1091 | capabilities
|
|---|
| 1092 | when the
|
|---|
| 1093 | .B \-v
|
|---|
| 1094 | command-line option is specified.
|
|---|
| 1095 | The default is false (i.e., the capability is omitted).
|
|---|
| 1096 | .TP
|
|---|
| 1097 | .B mt=01;31
|
|---|
| 1098 | SGR substring for matching non-empty text in any matching line
|
|---|
| 1099 | (i.e.,
|
|---|
| 1100 | a selected line when the
|
|---|
| 1101 | .B \-v
|
|---|
| 1102 | command-line option is omitted,
|
|---|
| 1103 | or a context line when
|
|---|
| 1104 | .B \-v
|
|---|
| 1105 | is specified).
|
|---|
| 1106 | Setting this is equivalent to setting both
|
|---|
| 1107 | .B ms=
|
|---|
| 1108 | and
|
|---|
| 1109 | .B mc=
|
|---|
| 1110 | at once to the same value.
|
|---|
| 1111 | The default is a bold red text foreground over the current line background.
|
|---|
| 1112 | .TP
|
|---|
| 1113 | .B ms=01;31
|
|---|
| 1114 | SGR substring for matching non-empty text in a selected line.
|
|---|
| 1115 | (This is only used when the
|
|---|
| 1116 | .B \-v
|
|---|
| 1117 | command-line option is omitted.)
|
|---|
| 1118 | The effect of the
|
|---|
| 1119 | .B sl=
|
|---|
| 1120 | (or
|
|---|
| 1121 | .B cx=
|
|---|
| 1122 | if
|
|---|
| 1123 | .BR rv )
|
|---|
| 1124 | capability remains active when this kicks in.
|
|---|
| 1125 | The default is a bold red text foreground over the current line background.
|
|---|
| 1126 | .TP
|
|---|
| 1127 | .B mc=01;31
|
|---|
| 1128 | SGR substring for matching non-empty text in a context line.
|
|---|
| 1129 | (This is only used when the
|
|---|
| 1130 | .B \-v
|
|---|
| 1131 | command-line option is specified.)
|
|---|
| 1132 | The effect of the
|
|---|
| 1133 | .B cx=
|
|---|
| 1134 | (or
|
|---|
| 1135 | .B sl=
|
|---|
| 1136 | if
|
|---|
| 1137 | .BR rv )
|
|---|
| 1138 | capability remains active when this kicks in.
|
|---|
| 1139 | The default is a bold red text foreground over the current line background.
|
|---|
| 1140 | .TP
|
|---|
| 1141 | .B fn=35
|
|---|
| 1142 | SGR substring for file names prefixing any content line.
|
|---|
| 1143 | The default is a magenta text foreground over the terminal's default background.
|
|---|
| 1144 | .TP
|
|---|
| 1145 | .B ln=32
|
|---|
| 1146 | SGR substring for line numbers prefixing any content line.
|
|---|
| 1147 | The default is a green text foreground over the terminal's default background.
|
|---|
| 1148 | .TP
|
|---|
| 1149 | .B bn=32
|
|---|
| 1150 | SGR substring for byte offsets prefixing any content line.
|
|---|
| 1151 | The default is a green text foreground over the terminal's default background.
|
|---|
| 1152 | .TP
|
|---|
| 1153 | .B se=36
|
|---|
| 1154 | SGR substring for separators that are inserted
|
|---|
| 1155 | between selected line fields
|
|---|
| 1156 | .RB ( : ),
|
|---|
| 1157 | between context line fields,
|
|---|
| 1158 | .RB ( \- ),
|
|---|
| 1159 | and between groups of adjacent lines when nonzero context is specified
|
|---|
| 1160 | .RB ( \-\^\- ).
|
|---|
| 1161 | The default is a cyan text foreground over the terminal's default background.
|
|---|
| 1162 | .TP
|
|---|
| 1163 | .B ne
|
|---|
| 1164 | Boolean value that prevents clearing to the end of line
|
|---|
| 1165 | using Erase in Line (EL) to Right
|
|---|
| 1166 | .RB ( \e33[K )
|
|---|
| 1167 | each time a colorized item ends.
|
|---|
| 1168 | This is needed on terminals on which EL is not supported.
|
|---|
| 1169 | It is otherwise useful on terminals
|
|---|
| 1170 | for which the
|
|---|
| 1171 | .B back_color_erase
|
|---|
| 1172 | .RB ( bce )
|
|---|
| 1173 | boolean terminfo capability does not apply,
|
|---|
| 1174 | when the chosen highlight colors do not affect the background,
|
|---|
| 1175 | or when EL is too slow or causes too much flicker.
|
|---|
| 1176 | The default is false (i.e., the capability is omitted).
|
|---|
| 1177 | .PP
|
|---|
| 1178 | Note that boolean capabilities have no
|
|---|
| 1179 | .BR = .\|.\|.\&
|
|---|
| 1180 | part.
|
|---|
| 1181 | They are omitted (i.e., false) by default and become true when specified.
|
|---|
| 1182 | .PP
|
|---|
| 1183 | See the Select Graphic Rendition (SGR) section
|
|---|
| 1184 | in the documentation of the text terminal that is used
|
|---|
| 1185 | for permitted values and their meaning as character attributes.
|
|---|
| 1186 | These substring values are integers in decimal representation
|
|---|
| 1187 | and can be concatenated with semicolons.
|
|---|
| 1188 | .B grep
|
|---|
| 1189 | takes care of assembling the result
|
|---|
| 1190 | into a complete SGR sequence
|
|---|
| 1191 | .RB ( \e33[ .\|.\|. m ).
|
|---|
| 1192 | Common values to concatenate include
|
|---|
| 1193 | .B 1
|
|---|
| 1194 | for bold,
|
|---|
| 1195 | .B 4
|
|---|
| 1196 | for underline,
|
|---|
| 1197 | .B 5
|
|---|
| 1198 | for blink,
|
|---|
| 1199 | .B 7
|
|---|
| 1200 | for inverse,
|
|---|
| 1201 | .B 39
|
|---|
| 1202 | for default foreground color,
|
|---|
| 1203 | .B 30
|
|---|
| 1204 | to
|
|---|
| 1205 | .B 37
|
|---|
| 1206 | for foreground colors,
|
|---|
| 1207 | .B 90
|
|---|
| 1208 | to
|
|---|
| 1209 | .B 97
|
|---|
| 1210 | for 16-color mode foreground colors,
|
|---|
| 1211 | .B 38;5;0
|
|---|
| 1212 | to
|
|---|
| 1213 | .B 38;5;255
|
|---|
| 1214 | for 88-color and 256-color modes foreground colors,
|
|---|
| 1215 | .B 49
|
|---|
| 1216 | for default background color,
|
|---|
| 1217 | .B 40
|
|---|
| 1218 | to
|
|---|
| 1219 | .B 47
|
|---|
| 1220 | for background colors,
|
|---|
| 1221 | .B 100
|
|---|
| 1222 | to
|
|---|
| 1223 | .B 107
|
|---|
| 1224 | for 16-color mode background colors, and
|
|---|
| 1225 | .B 48;5;0
|
|---|
| 1226 | to
|
|---|
| 1227 | .B 48;5;255
|
|---|
| 1228 | for 88-color and 256-color modes background colors.
|
|---|
| 1229 | .RE
|
|---|
| 1230 | .TP
|
|---|
| 1231 | \fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fP
|
|---|
| 1232 | These variables specify the locale for the
|
|---|
| 1233 | .B LC_COLLATE
|
|---|
| 1234 | category,
|
|---|
| 1235 | which determines the collating sequence
|
|---|
| 1236 | used to interpret range expressions like
|
|---|
| 1237 | .BR [a\-z] .
|
|---|
| 1238 | .TP
|
|---|
| 1239 | \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
|
|---|
| 1240 | These variables specify the locale for the
|
|---|
| 1241 | .B LC_CTYPE
|
|---|
| 1242 | category,
|
|---|
| 1243 | which determines the type of characters,
|
|---|
| 1244 | e.g., which characters are whitespace.
|
|---|
| 1245 | This category also determines the character encoding, that is, whether
|
|---|
| 1246 | text is encoded in UTF-8, ASCII, or some other encoding. In the C or
|
|---|
| 1247 | POSIX locale, all characters are encoded as a single byte and every
|
|---|
| 1248 | byte is a valid character.
|
|---|
| 1249 | .TP
|
|---|
| 1250 | \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
|
|---|
| 1251 | These variables specify the locale for the
|
|---|
| 1252 | .B LC_MESSAGES
|
|---|
| 1253 | category,
|
|---|
| 1254 | which determines the language that
|
|---|
| 1255 | .B grep
|
|---|
| 1256 | uses for messages.
|
|---|
| 1257 | The default C locale uses American English messages.
|
|---|
| 1258 | .TP
|
|---|
| 1259 | .B POSIXLY_CORRECT
|
|---|
| 1260 | If set,
|
|---|
| 1261 | .B grep
|
|---|
| 1262 | behaves as POSIX requires; otherwise,
|
|---|
| 1263 | .B grep
|
|---|
| 1264 | behaves more like other GNU programs.
|
|---|
| 1265 | POSIX requires that options that follow file names must be
|
|---|
| 1266 | treated as file names; by default, such options are permuted to the
|
|---|
| 1267 | front of the operand list and are treated as options.
|
|---|
| 1268 | Also, POSIX requires that unrecognized options be diagnosed as
|
|---|
| 1269 | \*(lqillegal\*(rq, but since they are not really against the law the default
|
|---|
| 1270 | is to diagnose them as \*(lqinvalid\*(rq.
|
|---|
| 1271 | .B POSIXLY_CORRECT
|
|---|
| 1272 | also disables \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP,
|
|---|
| 1273 | described below.
|
|---|
| 1274 | .TP
|
|---|
| 1275 | \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP
|
|---|
| 1276 | (Here
|
|---|
| 1277 | .I N
|
|---|
| 1278 | is
|
|---|
| 1279 | .BR grep 's
|
|---|
| 1280 | numeric process ID.) If the
|
|---|
| 1281 | .IR i th
|
|---|
| 1282 | character of this environment variable's value is
|
|---|
| 1283 | .BR 1 ,
|
|---|
| 1284 | do not consider the
|
|---|
| 1285 | .IR i th
|
|---|
| 1286 | operand of
|
|---|
| 1287 | .B grep
|
|---|
| 1288 | to be an option, even if it appears to be one.
|
|---|
| 1289 | A shell can put this variable in the environment for each command it runs,
|
|---|
| 1290 | specifying which operands are the results of file name wildcard
|
|---|
| 1291 | expansion and therefore should not be treated as options.
|
|---|
| 1292 | This behavior is available only with the GNU C library, and only
|
|---|
| 1293 | when
|
|---|
| 1294 | .B POSIXLY_CORRECT
|
|---|
| 1295 | is not set.
|
|---|
| 1296 | .
|
|---|
| 1297 | .SH NOTES
|
|---|
| 1298 | This man page is maintained only fitfully;
|
|---|
| 1299 | the full documentation is often more up-to-date.
|
|---|
| 1300 | .
|
|---|
| 1301 | .SH COPYRIGHT
|
|---|
| 1302 | Copyright 1998-2000, 2002, 2005-2021 Free Software Foundation, Inc.
|
|---|
| 1303 | .PP
|
|---|
| 1304 | This is free software;
|
|---|
| 1305 | see the source for copying conditions.
|
|---|
| 1306 | There is NO warranty;
|
|---|
| 1307 | not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 1308 | .
|
|---|
| 1309 | .SH BUGS
|
|---|
| 1310 | .SS "Reporting Bugs"
|
|---|
| 1311 | Email bug reports to
|
|---|
| 1312 | .MT bug-grep@gnu.org
|
|---|
| 1313 | the bug-reporting address
|
|---|
| 1314 | .ME .
|
|---|
| 1315 | An
|
|---|
| 1316 | .UR https://lists.gnu.org/mailman/listinfo/bug-grep
|
|---|
| 1317 | email archive
|
|---|
| 1318 | .UE
|
|---|
| 1319 | and a
|
|---|
| 1320 | .UR https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep
|
|---|
| 1321 | bug tracker
|
|---|
| 1322 | .UE
|
|---|
| 1323 | are available.
|
|---|
| 1324 | .SS "Known Bugs"
|
|---|
| 1325 | Large repetition counts in the
|
|---|
| 1326 | .BI { n , m }
|
|---|
| 1327 | construct may cause
|
|---|
| 1328 | .B grep
|
|---|
| 1329 | to use lots of memory.
|
|---|
| 1330 | In addition,
|
|---|
| 1331 | certain other obscure regular expressions require exponential time
|
|---|
| 1332 | and space, and may cause
|
|---|
| 1333 | .B grep
|
|---|
| 1334 | to run out of memory.
|
|---|
| 1335 | .PP
|
|---|
| 1336 | Back-references are very slow, and may require exponential time.
|
|---|
| 1337 | .
|
|---|
| 1338 | .SH EXAMPLE
|
|---|
| 1339 | The following example outputs the location and contents of any line
|
|---|
| 1340 | containing \*(lqf\*(rq and ending in \*(lq.c\*(rq,
|
|---|
| 1341 | within all files in the current directory whose names
|
|---|
| 1342 | contain \*(lqg\*(rq and end in \*(lq.h\*(rq.
|
|---|
| 1343 | The
|
|---|
| 1344 | .B \-n
|
|---|
| 1345 | option outputs line numbers, the
|
|---|
| 1346 | .B \-\-
|
|---|
| 1347 | argument treats expansions of \*(lq*g*.h\*(rq starting with \*(lq\-\*(rq
|
|---|
| 1348 | as file names not options,
|
|---|
| 1349 | and the empty file /dev/null causes file names to be output
|
|---|
| 1350 | even if only one file name happens to be of the form \*(lq*g*.h\*(rq.
|
|---|
| 1351 | .PP
|
|---|
| 1352 | .in +2n
|
|---|
| 1353 | .EX
|
|---|
| 1354 | $ \fBgrep\fP \-n \-\- 'f.*\e.c$' *g*.h /dev/null
|
|---|
| 1355 | argmatch.h:1:/* definitions and prototypes for argmatch.c
|
|---|
| 1356 | .EE
|
|---|
| 1357 | .in
|
|---|
| 1358 | .PP
|
|---|
| 1359 | The only line that matches is line 1 of argmatch.h.
|
|---|
| 1360 | Note that the regular expression syntax used in the pattern differs
|
|---|
| 1361 | from the globbing syntax that the shell uses to match file names.
|
|---|
| 1362 | .
|
|---|
| 1363 | .SH "SEE ALSO"
|
|---|
| 1364 | .SS "Regular Manual Pages"
|
|---|
| 1365 | .BR awk (1),
|
|---|
| 1366 | .BR cmp (1),
|
|---|
| 1367 | .BR diff (1),
|
|---|
| 1368 | .BR find (1),
|
|---|
| 1369 | .BR perl (1),
|
|---|
| 1370 | .BR sed (1),
|
|---|
| 1371 | .BR sort (1),
|
|---|
| 1372 | .BR xargs (1),
|
|---|
| 1373 | .BR read (2),
|
|---|
| 1374 | .BR pcre (3),
|
|---|
| 1375 | .BR pcresyntax (3),
|
|---|
| 1376 | .BR pcrepattern (3),
|
|---|
| 1377 | .BR terminfo (5),
|
|---|
| 1378 | .BR glob (7),
|
|---|
| 1379 | .BR regex (7)
|
|---|
| 1380 | .SS "Full Documentation"
|
|---|
| 1381 | A
|
|---|
| 1382 | .UR https://www.gnu.org/software/grep/manual/
|
|---|
| 1383 | complete manual
|
|---|
| 1384 | .UE
|
|---|
| 1385 | is available.
|
|---|
| 1386 | If the
|
|---|
| 1387 | .B info
|
|---|
| 1388 | and
|
|---|
| 1389 | .B grep
|
|---|
| 1390 | programs are properly installed at your site, the command
|
|---|
| 1391 | .IP
|
|---|
| 1392 | .B info grep
|
|---|
| 1393 | .PP
|
|---|
| 1394 | should give you access to the complete manual.
|
|---|
| 1395 | .
|
|---|
| 1396 | .\" Work around problems with some troff -man implementations.
|
|---|
| 1397 | .br
|
|---|
| 1398 | .
|
|---|
| 1399 | .\" Format for Emacs-maintained Dt string defined at this file's start.
|
|---|
| 1400 | .\" Local variables:
|
|---|
| 1401 | .\" time-stamp-format: "%:y-%02m-%02d"
|
|---|
| 1402 | .\" End:
|
|---|