1 | .\" grep man page
|
---|
2 | .if !\n(.g \{\
|
---|
3 | . if !\w|\*(lq| \{\
|
---|
4 | . ds lq ``
|
---|
5 | . if \w'\(lq' .ds lq "\(lq
|
---|
6 | . \}
|
---|
7 | . if !\w|\*(rq| \{\
|
---|
8 | . ds rq ''
|
---|
9 | . if \w'\(rq' .ds rq "\(rq
|
---|
10 | . \}
|
---|
11 | .\}
|
---|
12 | .de Id
|
---|
13 | .ds Dt \\$4
|
---|
14 | ..
|
---|
15 | .Id $Id: grep.1,v 1.23 2002/01/22 13:20:04 bero Exp $
|
---|
16 | .TH GREP 1 \*(Dt "GNU Project"
|
---|
17 | .SH NAME
|
---|
18 | grep, egrep, fgrep \- print lines matching a pattern
|
---|
19 | .SH SYNOPSIS
|
---|
20 | .B grep
|
---|
21 | .RI [ options ]
|
---|
22 | .I PATTERN
|
---|
23 | .RI [ FILE .\|.\|.]
|
---|
24 | .br
|
---|
25 | .B grep
|
---|
26 | .RI [ options ]
|
---|
27 | .RB [ \-e
|
---|
28 | .I PATTERN
|
---|
29 | |
|
---|
30 | .B \-f
|
---|
31 | .IR FILE ]
|
---|
32 | .RI [ FILE .\|.\|.]
|
---|
33 | .SH DESCRIPTION
|
---|
34 | .PP
|
---|
35 | .B Grep
|
---|
36 | searches the named input
|
---|
37 | .IR FILE s
|
---|
38 | (or standard input if no files are named, or
|
---|
39 | the file name
|
---|
40 | .B \-
|
---|
41 | is given)
|
---|
42 | for lines containing a match to the given
|
---|
43 | .IR PATTERN .
|
---|
44 | By default,
|
---|
45 | .B grep
|
---|
46 | prints the matching lines.
|
---|
47 | .PP
|
---|
48 | In addition, two variant programs
|
---|
49 | .B egrep
|
---|
50 | and
|
---|
51 | .B fgrep
|
---|
52 | are available.
|
---|
53 | .B Egrep
|
---|
54 | is the same as
|
---|
55 | .BR "grep\ \-E" .
|
---|
56 | .B Fgrep
|
---|
57 | is the same as
|
---|
58 | .BR "grep\ \-F" .
|
---|
59 | .SH OPTIONS
|
---|
60 | .TP
|
---|
61 | .BI \-A " NUM" "\fR,\fP \-\^\-after-context=" NUM
|
---|
62 | Print
|
---|
63 | .I NUM
|
---|
64 | lines of trailing context after matching lines.
|
---|
65 | Places a line containing
|
---|
66 | .B \-\^\-
|
---|
67 | between contiguous groups of matches.
|
---|
68 | .TP
|
---|
69 | .BR \-a ", " \-\^\-text
|
---|
70 | Process a binary file as if it were text; this is equivalent to the
|
---|
71 | .B \-\^\-binary-files=text
|
---|
72 | option.
|
---|
73 | .TP
|
---|
74 | .BI \-B " NUM" "\fR,\fP \-\^\-before-context=" NUM
|
---|
75 | Print
|
---|
76 | .I NUM
|
---|
77 | lines of leading context before matching lines.
|
---|
78 | Places a line containing
|
---|
79 | .B \-\^\-
|
---|
80 | between contiguous groups of matches.
|
---|
81 | .TP
|
---|
82 | .BI \-C " NUM" "\fR,\fP \-\^\-context=" NUM
|
---|
83 | Print
|
---|
84 | .I NUM
|
---|
85 | lines of output context.
|
---|
86 | Places a line containing
|
---|
87 | .B \-\^\-
|
---|
88 | between contiguous groups of matches.
|
---|
89 | .TP
|
---|
90 | .BR \-b ", " \-\^\-byte-offset
|
---|
91 | Print the byte offset within the input file before
|
---|
92 | each line of output.
|
---|
93 | .TP
|
---|
94 | .BI \-\^\-binary-files= TYPE
|
---|
95 | If the first few bytes of a file indicate that the file contains binary
|
---|
96 | data, assume that the file is of type
|
---|
97 | .IR TYPE .
|
---|
98 | By default,
|
---|
99 | .I TYPE
|
---|
100 | is
|
---|
101 | .BR binary ,
|
---|
102 | and
|
---|
103 | .B grep
|
---|
104 | normally outputs either
|
---|
105 | a one-line message saying that a binary file matches, or no message if
|
---|
106 | there is no match.
|
---|
107 | If
|
---|
108 | .I TYPE
|
---|
109 | is
|
---|
110 | .BR without-match ,
|
---|
111 | .B grep
|
---|
112 | assumes that a binary file does not match; this is equivalent to the
|
---|
113 | .B \-I
|
---|
114 | option.
|
---|
115 | If
|
---|
116 | .I TYPE
|
---|
117 | is
|
---|
118 | .BR text ,
|
---|
119 | .B grep
|
---|
120 | processes a binary file as if it were text; this is equivalent to the
|
---|
121 | .B \-a
|
---|
122 | option.
|
---|
123 | .I Warning:
|
---|
124 | .B "grep \-\^\-binary-files=text"
|
---|
125 | might output binary garbage,
|
---|
126 | which can have nasty side effects if the output is a terminal and if the
|
---|
127 | terminal driver interprets some of it as commands.
|
---|
128 | .TP
|
---|
129 | .BI \-\^\-colour[=\fIWHEN\fR] ", " \-\^\-color[=\fIWHEN\fR]
|
---|
130 | Surround the matching string with the marker find in
|
---|
131 | .B GREP_COLOR
|
---|
132 | environment variable. WHEN may be `never', `always', or `auto'
|
---|
133 | .TP
|
---|
134 | .BR \-c ", " \-\^\-count
|
---|
135 | Suppress normal output; instead print a count of
|
---|
136 | matching lines for each input file.
|
---|
137 | With the
|
---|
138 | .BR \-v ", " \-\^\-invert-match
|
---|
139 | option (see below), count non-matching lines.
|
---|
140 | .TP
|
---|
141 | .BI \-D " ACTION" "\fR,\fP \-\^\-devices=" ACTION
|
---|
142 | If an input file is a device, FIFO or socket, use
|
---|
143 | .I ACTION
|
---|
144 | to process it. By default,
|
---|
145 | .I ACTION
|
---|
146 | is
|
---|
147 | .BR read ,
|
---|
148 | which means that devices are read just as if they were ordinary files.
|
---|
149 | If
|
---|
150 | .I ACTION
|
---|
151 | is
|
---|
152 | .BR skip ,
|
---|
153 | devices are silently skipped.
|
---|
154 | .TP
|
---|
155 | .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
|
---|
156 | If an input file is a directory, use
|
---|
157 | .I ACTION
|
---|
158 | to process it. By default,
|
---|
159 | .I ACTION
|
---|
160 | is
|
---|
161 | .BR read ,
|
---|
162 | which means that directories are read just as if they were ordinary files.
|
---|
163 | If
|
---|
164 | .I ACTION
|
---|
165 | is
|
---|
166 | .BR skip ,
|
---|
167 | directories are silently skipped.
|
---|
168 | If
|
---|
169 | .I ACTION
|
---|
170 | is
|
---|
171 | .BR recurse ,
|
---|
172 | .B grep
|
---|
173 | reads all files under each directory, recursively;
|
---|
174 | this is equivalent to the
|
---|
175 | .B \-r
|
---|
176 | option.
|
---|
177 | .TP
|
---|
178 | .BR \-E ", " \-\^\-extended-regexp
|
---|
179 | Interpret
|
---|
180 | .I PATTERN
|
---|
181 | as an extended regular expression (see below).
|
---|
182 | .TP
|
---|
183 | .BI \-e " PATTERN" "\fR,\fP \-\^\-regexp=" PATTERN
|
---|
184 | Use
|
---|
185 | .I PATTERN
|
---|
186 | as the pattern; useful to protect patterns beginning with
|
---|
187 | .BR \- .
|
---|
188 | .TP
|
---|
189 | .BR \-F ", " \-\^\-fixed-strings
|
---|
190 | Interpret
|
---|
191 | .I PATTERN
|
---|
192 | as a list of fixed strings, separated by newlines,
|
---|
193 | any of which is to be matched.
|
---|
194 | .BR \-P ", " \-\^\-perl-regexp
|
---|
195 | Interpret
|
---|
196 | .I PATTERN
|
---|
197 | as a Perl regular expression.
|
---|
198 | .TP
|
---|
199 | .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
|
---|
200 | Obtain patterns from
|
---|
201 | .IR FILE ,
|
---|
202 | one per line.
|
---|
203 | The empty file contains zero patterns, and therefore matches nothing.
|
---|
204 | .TP
|
---|
205 | .BR \-G ", " \-\^\-basic-regexp
|
---|
206 | Interpret
|
---|
207 | .I PATTERN
|
---|
208 | as a basic regular expression (see below). This is the default.
|
---|
209 | .TP
|
---|
210 | .BR \-H ", " \-\^\-with-filename
|
---|
211 | Print the filename for each match.
|
---|
212 | .TP
|
---|
213 | .BR \-h ", " \-\^\-no-filename
|
---|
214 | Suppress the prefixing of filenames on output
|
---|
215 | when multiple files are searched.
|
---|
216 | .TP
|
---|
217 | .B \-\^\-help
|
---|
218 | Output a brief help message.
|
---|
219 | .TP
|
---|
220 | .BR \-I
|
---|
221 | Process a binary file as if it did not contain matching data; this is
|
---|
222 | equivalent to the
|
---|
223 | .B \-\^\-binary-files=without-match
|
---|
224 | option.
|
---|
225 | .TP
|
---|
226 | .BR \-i ", " \-\^\-ignore-case
|
---|
227 | Ignore case distinctions in both the
|
---|
228 | .I PATTERN
|
---|
229 | and the input files.
|
---|
230 | .TP
|
---|
231 | .BR \-L ", " \-\^\-files-without-match
|
---|
232 | Suppress normal output; instead print the name
|
---|
233 | of each input file from which no output would
|
---|
234 | normally have been printed. The scanning will stop
|
---|
235 | on the first match.
|
---|
236 | .TP
|
---|
237 | .BR \-l ", " \-\^\-files-with-matches
|
---|
238 | Suppress normal output; instead print
|
---|
239 | the name of each input file from which output
|
---|
240 | would normally have been printed. The scanning will
|
---|
241 | stop on the first match.
|
---|
242 | .TP
|
---|
243 | .BI \-m " NUM" "\fR,\fP \-\^\-max-count=" NUM
|
---|
244 | Stop reading a file after
|
---|
245 | .I NUM
|
---|
246 | matching lines. If the input is standard input from a regular file,
|
---|
247 | and
|
---|
248 | .I NUM
|
---|
249 | matching lines are output,
|
---|
250 | .B grep
|
---|
251 | ensures that the standard input is positioned to just after the last
|
---|
252 | matching line before exiting, regardless of the presence of trailing
|
---|
253 | context lines. This enables a calling process to resume a search.
|
---|
254 | When
|
---|
255 | .B grep
|
---|
256 | stops after
|
---|
257 | .I NUM
|
---|
258 | matching lines, it outputs any trailing context lines. When the
|
---|
259 | .B \-c
|
---|
260 | or
|
---|
261 | .B \-\^\-count
|
---|
262 | option is also used,
|
---|
263 | .B grep
|
---|
264 | does not output a count greater than
|
---|
265 | .IR NUM .
|
---|
266 | When the
|
---|
267 | .B \-v
|
---|
268 | or
|
---|
269 | .B \-\^\-invert-match
|
---|
270 | option is also used,
|
---|
271 | .B grep
|
---|
272 | stops after outputting
|
---|
273 | .I NUM
|
---|
274 | non-matching lines.
|
---|
275 | .TP
|
---|
276 | .B \-\^\-mmap
|
---|
277 | If possible, use the
|
---|
278 | .BR mmap (2)
|
---|
279 | system call to read input, instead of
|
---|
280 | the default
|
---|
281 | .BR read (2)
|
---|
282 | system call. In some situations,
|
---|
283 | .B \-\^\-mmap
|
---|
284 | yields better performance. However,
|
---|
285 | .B \-\^\-mmap
|
---|
286 | can cause undefined behavior (including core dumps)
|
---|
287 | if an input file shrinks while
|
---|
288 | .B grep
|
---|
289 | is operating, or if an I/O error occurs.
|
---|
290 | .TP
|
---|
291 | .BR \-n ", " \-\^\-line-number
|
---|
292 | Prefix each line of output with the line number
|
---|
293 | within its input file.
|
---|
294 | .TP
|
---|
295 | .BR \-o ", " \-\^\-only-matching
|
---|
296 | Show only the part of a matching line that matches
|
---|
297 | .I PATTERN.
|
---|
298 | .TP
|
---|
299 | .BI \-\^\-label= LABEL
|
---|
300 | Displays input actually coming from standard input as input coming from file
|
---|
301 | .I LABEL.
|
---|
302 | This is especially useful for tools like zgrep, e.g.
|
---|
303 | .B "gzip -cd foo.gz |grep --label=foo something"
|
---|
304 | .TP
|
---|
305 | .BR \-\^\-line-buffering
|
---|
306 | Use line buffering, it can be a performance penality.
|
---|
307 | .TP
|
---|
308 | .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
|
---|
309 | Quiet; do not write anything to standard output.
|
---|
310 | Exit immediately with zero status if any match is found,
|
---|
311 | even if an error was detected.
|
---|
312 | Also see the
|
---|
313 | .B \-s
|
---|
314 | or
|
---|
315 | .B \-\^\-no-messages
|
---|
316 | option.
|
---|
317 | .TP
|
---|
318 | .BR \-R ", " \-r ", " \-\^\-recursive
|
---|
319 | Read all files under each directory, recursively;
|
---|
320 | this is equivalent to the
|
---|
321 | .B "\-d recurse"
|
---|
322 | option.
|
---|
323 | .TP
|
---|
324 | .BR "\fR \fP \-\^\-include=" PATTERN
|
---|
325 | Recurse in directories only searching file matching
|
---|
326 | .I PATTERN.
|
---|
327 | .TP
|
---|
328 | .BR "\fR \fP \-\^\-exclude=" PATTERN
|
---|
329 | Recurse in directories skip file matching
|
---|
330 | .I PATTERN.
|
---|
331 | .TP
|
---|
332 | .BR \-s ", " \-\^\-no-messages
|
---|
333 | Suppress error messages about nonexistent or unreadable files.
|
---|
334 | Portability note: unlike \s-1GNU\s0
|
---|
335 | .BR grep ,
|
---|
336 | traditional
|
---|
337 | .B grep
|
---|
338 | did not conform to \s-1POSIX.2\s0, because traditional
|
---|
339 | .B grep
|
---|
340 | lacked a
|
---|
341 | .B \-q
|
---|
342 | option and its
|
---|
343 | .B \-s
|
---|
344 | option behaved like \s-1GNU\s0
|
---|
345 | .BR grep 's
|
---|
346 | .B \-q
|
---|
347 | option.
|
---|
348 | Shell scripts intended to be portable to traditional
|
---|
349 | .B grep
|
---|
350 | should avoid both
|
---|
351 | .B \-q
|
---|
352 | and
|
---|
353 | .B \-s
|
---|
354 | and should redirect output to /dev/null instead.
|
---|
355 | .TP
|
---|
356 | .BR \-U ", " \-\^\-binary
|
---|
357 | Treat the file(s) as binary. By default, under MS-DOS and MS-Windows,
|
---|
358 | .BR grep
|
---|
359 | guesses the file type by looking at the contents of the first 32KB
|
---|
360 | read from the file. If
|
---|
361 | .BR grep
|
---|
362 | decides the file is a text file, it strips the CR characters from the
|
---|
363 | original file contents (to make regular expressions with
|
---|
364 | .B ^
|
---|
365 | and
|
---|
366 | .B $
|
---|
367 | work correctly). Specifying
|
---|
368 | .B \-U
|
---|
369 | overrules this guesswork, causing all files to be read and passed to the
|
---|
370 | matching mechanism verbatim; if the file is a text file with CR/LF
|
---|
371 | pairs at the end of each line, this will cause some regular
|
---|
372 | expressions to fail.
|
---|
373 | This option has no effect on platforms other than MS-DOS and
|
---|
374 | MS-Windows.
|
---|
375 | .TP
|
---|
376 | .BR \-u ", " \-\^\-unix-byte-offsets
|
---|
377 | Report Unix-style byte offsets. This switch causes
|
---|
378 | .B grep
|
---|
379 | to report byte offsets as if the file were Unix-style text file, i.e. with
|
---|
380 | CR characters stripped off. This will produce results identical to running
|
---|
381 | .B grep
|
---|
382 | on a Unix machine. This option has no effect unless
|
---|
383 | .B \-b
|
---|
384 | option is also used;
|
---|
385 | it has no effect on platforms other than MS-DOS and MS-Windows.
|
---|
386 | .TP
|
---|
387 | .BR \-V ", " \-\^\-version
|
---|
388 | Print the version number of
|
---|
389 | .B grep
|
---|
390 | to standard error. This version number should
|
---|
391 | be included in all bug reports (see below).
|
---|
392 | .TP
|
---|
393 | .BR \-v ", " \-\^\-invert-match
|
---|
394 | Invert the sense of matching, to select non-matching lines.
|
---|
395 | .TP
|
---|
396 | .BR \-w ", " \-\^\-word-regexp
|
---|
397 | Select only those lines containing matches that form whole words.
|
---|
398 | The test is that the matching substring must either be at the
|
---|
399 | beginning of the line, or preceded by a non-word constituent
|
---|
400 | character. Similarly, it must be either at the end of the line
|
---|
401 | or followed by a non-word constituent character. Word-constituent
|
---|
402 | characters are letters, digits, and the underscore.
|
---|
403 | .TP
|
---|
404 | .BR \-x ", " \-\^\-line-regexp
|
---|
405 | Select only those matches that exactly match the whole line.
|
---|
406 | .TP
|
---|
407 | .B \-y
|
---|
408 | Obsolete synonym for
|
---|
409 | .BR \-i .
|
---|
410 | .TP
|
---|
411 | .BR \-Z ", " \-\^\-null
|
---|
412 | Output a zero byte (the \s-1ASCII\s0
|
---|
413 | .B NUL
|
---|
414 | character) instead of the character that normally follows a file name.
|
---|
415 | For example,
|
---|
416 | .B "grep \-lZ"
|
---|
417 | outputs a zero byte after each file name instead of the usual newline.
|
---|
418 | This option makes the output unambiguous, even in the presence of file
|
---|
419 | names containing unusual characters like newlines. This option can be
|
---|
420 | used with commands like
|
---|
421 | .BR "find \-print0" ,
|
---|
422 | .BR "perl \-0" ,
|
---|
423 | .BR "sort \-z" ,
|
---|
424 | and
|
---|
425 | .B "xargs \-0"
|
---|
426 | to process arbitrary file names,
|
---|
427 | even those that contain newline characters.
|
---|
428 | .SH "REGULAR EXPRESSIONS"
|
---|
429 | .PP
|
---|
430 | A regular expression is a pattern that describes a set of strings.
|
---|
431 | Regular expressions are constructed analogously to arithmetic
|
---|
432 | expressions, by using various operators to combine smaller expressions.
|
---|
433 | .PP
|
---|
434 | .B Grep
|
---|
435 | understands two different versions of regular expression syntax:
|
---|
436 | \*(lqbasic\*(rq and \*(lqextended.\*(rq In
|
---|
437 | .RB "\s-1GNU\s0\ " grep ,
|
---|
438 | there is no difference in available functionality using either syntax.
|
---|
439 | In other implementations, basic regular expressions are less powerful.
|
---|
440 | The following description applies to extended regular expressions;
|
---|
441 | differences for basic regular expressions are summarized afterwards.
|
---|
442 | .PP
|
---|
443 | The fundamental building blocks are the regular expressions that match
|
---|
444 | a single character. Most characters, including all letters and digits,
|
---|
445 | are regular expressions that match themselves. Any metacharacter with
|
---|
446 | special meaning may be quoted by preceding it with a backslash.
|
---|
447 | .PP
|
---|
448 | A
|
---|
449 | .I "bracket expression"
|
---|
450 | is a list of characters enclosed by
|
---|
451 | .B [
|
---|
452 | and
|
---|
453 | .BR ] .
|
---|
454 | It matches any single
|
---|
455 | character in that list; if the first character of the list
|
---|
456 | is the caret
|
---|
457 | .B ^
|
---|
458 | then it matches any character
|
---|
459 | .I not
|
---|
460 | in the list.
|
---|
461 | For example, the regular expression
|
---|
462 | .B [0123456789]
|
---|
463 | matches any single digit.
|
---|
464 | .PP
|
---|
465 | Within a bracket expression, a
|
---|
466 | .I "range expression"
|
---|
467 | consists of two characters separated by a hyphen.
|
---|
468 | It matches any single character that sorts between the two characters,
|
---|
469 | inclusive, using the locale's collating sequence and character set.
|
---|
470 | For example, in the default C locale,
|
---|
471 | .B [a\-d]
|
---|
472 | is equivalent to
|
---|
473 | .BR [abcd] .
|
---|
474 | Many locales sort characters in dictionary order, and in these locales
|
---|
475 | .B [a\-d]
|
---|
476 | is typically not equivalent to
|
---|
477 | .BR [abcd] ;
|
---|
478 | it might be equivalent to
|
---|
479 | .BR [aBbCcDd] ,
|
---|
480 | for example.
|
---|
481 | To obtain the traditional interpretation of bracket expressions,
|
---|
482 | you can use the C locale by setting the
|
---|
483 | .B LC_ALL
|
---|
484 | environment variable to the value
|
---|
485 | .BR C .
|
---|
486 | .PP
|
---|
487 | Finally, certain named classes of characters are predefined within
|
---|
488 | bracket expressions, as follows.
|
---|
489 | Their names are self explanatory, and they are
|
---|
490 | .BR [:alnum:] ,
|
---|
491 | .BR [:alpha:] ,
|
---|
492 | .BR [:cntrl:] ,
|
---|
493 | .BR [:digit:] ,
|
---|
494 | .BR [:graph:] ,
|
---|
495 | .BR [:lower:] ,
|
---|
496 | .BR [:print:] ,
|
---|
497 | .BR [:punct:] ,
|
---|
498 | .BR [:space:] ,
|
---|
499 | .BR [:upper:] ,
|
---|
500 | and
|
---|
501 | .BR [:xdigit:].
|
---|
502 | For example,
|
---|
503 | .B [[:alnum:]]
|
---|
504 | means
|
---|
505 | .BR [0\-9A\-Za\-z] ,
|
---|
506 | except the latter form depends upon the C locale and the
|
---|
507 | \s-1ASCII\s0 character encoding, whereas the former is independent
|
---|
508 | of locale and character set.
|
---|
509 | (Note that the brackets in these class names are part of the symbolic
|
---|
510 | names, and must be included in addition to the brackets delimiting
|
---|
511 | the bracket list.) Most metacharacters lose their special meaning
|
---|
512 | inside lists. To include a literal
|
---|
513 | .B ]
|
---|
514 | place it first in the list. Similarly, to include a literal
|
---|
515 | .B ^
|
---|
516 | place it anywhere but first. Finally, to include a literal
|
---|
517 | .B \-
|
---|
518 | place it last.
|
---|
519 | .PP
|
---|
520 | The period
|
---|
521 | .B .
|
---|
522 | matches any single character.
|
---|
523 | The symbol
|
---|
524 | .B \ew
|
---|
525 | is a synonym for
|
---|
526 | .B [[:alnum:]]
|
---|
527 | and
|
---|
528 | .B \eW
|
---|
529 | is a synonym for
|
---|
530 | .BR [^[:alnum]] .
|
---|
531 | .PP
|
---|
532 | The caret
|
---|
533 | .B ^
|
---|
534 | and the dollar sign
|
---|
535 | .B $
|
---|
536 | are metacharacters that respectively match the empty string at the
|
---|
537 | beginning and end of a line.
|
---|
538 | The symbols
|
---|
539 | .B \e<
|
---|
540 | and
|
---|
541 | .B \e>
|
---|
542 | respectively match the empty string at the beginning and end of a word.
|
---|
543 | The symbol
|
---|
544 | .B \eb
|
---|
545 | matches the empty string at the edge of a word,
|
---|
546 | and
|
---|
547 | .B \eB
|
---|
548 | matches the empty string provided it's
|
---|
549 | .I not
|
---|
550 | at the edge of a word.
|
---|
551 | .PP
|
---|
552 | A regular expression may be followed by one of several repetition operators:
|
---|
553 | .PD 0
|
---|
554 | .TP
|
---|
555 | .B ?
|
---|
556 | The preceding item is optional and matched at most once.
|
---|
557 | .TP
|
---|
558 | .B *
|
---|
559 | The preceding item will be matched zero or more times.
|
---|
560 | .TP
|
---|
561 | .B +
|
---|
562 | The preceding item will be matched one or more times.
|
---|
563 | .TP
|
---|
564 | .BI { n }
|
---|
565 | The preceding item is matched exactly
|
---|
566 | .I n
|
---|
567 | times.
|
---|
568 | .TP
|
---|
569 | .BI { n ,}
|
---|
570 | The preceding item is matched
|
---|
571 | .I n
|
---|
572 | or more times.
|
---|
573 | .TP
|
---|
574 | .BI { n , m }
|
---|
575 | The preceding item is matched at least
|
---|
576 | .I n
|
---|
577 | times, but not more than
|
---|
578 | .I m
|
---|
579 | times.
|
---|
580 | .PD
|
---|
581 | .PP
|
---|
582 | Two regular expressions may be concatenated; the resulting
|
---|
583 | regular expression matches any string formed by concatenating
|
---|
584 | two substrings that respectively match the concatenated
|
---|
585 | subexpressions.
|
---|
586 | .PP
|
---|
587 | Two regular expressions may be joined by the infix operator
|
---|
588 | .BR | ;
|
---|
589 | the resulting regular expression matches any string matching
|
---|
590 | either subexpression.
|
---|
591 | .PP
|
---|
592 | Repetition takes precedence over concatenation, which in turn
|
---|
593 | takes precedence over alternation. A whole subexpression may be
|
---|
594 | enclosed in parentheses to override these precedence rules.
|
---|
595 | .PP
|
---|
596 | The backreference
|
---|
597 | .BI \e n\c
|
---|
598 | \&, where
|
---|
599 | .I n
|
---|
600 | is a single digit, matches the substring
|
---|
601 | previously matched by the
|
---|
602 | .IR n th
|
---|
603 | parenthesized subexpression of the regular expression.
|
---|
604 | .PP
|
---|
605 | In basic regular expressions the metacharacters
|
---|
606 | .BR ? ,
|
---|
607 | .BR + ,
|
---|
608 | .BR { ,
|
---|
609 | .BR | ,
|
---|
610 | .BR ( ,
|
---|
611 | and
|
---|
612 | .BR )
|
---|
613 | lose their special meaning; instead use the backslashed
|
---|
614 | versions
|
---|
615 | .BR \e? ,
|
---|
616 | .BR \e+ ,
|
---|
617 | .BR \e{ ,
|
---|
618 | .BR \e| ,
|
---|
619 | .BR \e( ,
|
---|
620 | and
|
---|
621 | .BR \e) .
|
---|
622 | .PP
|
---|
623 | Traditional
|
---|
624 | .B egrep
|
---|
625 | did not support the
|
---|
626 | .B {
|
---|
627 | metacharacter, and some
|
---|
628 | .B egrep
|
---|
629 | implementations support
|
---|
630 | .B \e{
|
---|
631 | instead, so portable scripts should avoid
|
---|
632 | .B {
|
---|
633 | in
|
---|
634 | .B egrep
|
---|
635 | patterns and should use
|
---|
636 | .B [{]
|
---|
637 | to match a literal
|
---|
638 | .BR { .
|
---|
639 | .PP
|
---|
640 | \s-1GNU\s0
|
---|
641 | .B egrep
|
---|
642 | attempts to support traditional usage by assuming that
|
---|
643 | .B {
|
---|
644 | is not special if it would be the start of an invalid interval
|
---|
645 | specification. For example, the shell command
|
---|
646 | .B "egrep '{1'"
|
---|
647 | searches for the two-character string
|
---|
648 | .B {1
|
---|
649 | instead of reporting a syntax error in the regular expression.
|
---|
650 | \s-1POSIX.2\s0 allows this behavior as an extension, but portable scripts
|
---|
651 | should avoid it.
|
---|
652 | .SH "ENVIRONMENT VARIABLES"
|
---|
653 | Grep's behavior is affected by the following environment variables.
|
---|
654 | .PP
|
---|
655 | A locale
|
---|
656 | .BI LC_ foo
|
---|
657 | is specified by examining the three environment variables
|
---|
658 | .BR LC_ALL ,
|
---|
659 | .BR LC_\fIfoo\fP ,
|
---|
660 | .BR LANG ,
|
---|
661 | in that order.
|
---|
662 | The first of these variables that is set specifies the locale.
|
---|
663 | For example, if
|
---|
664 | .B LC_ALL
|
---|
665 | is not set, but
|
---|
666 | .B LC_MESSAGES
|
---|
667 | is set to
|
---|
668 | .BR pt_BR ,
|
---|
669 | then Brazilian Portuguese is used for the
|
---|
670 | .B LC_MESSAGES
|
---|
671 | locale.
|
---|
672 | The C locale is used if none of these environment variables are set,
|
---|
673 | or if the locale catalog is not installed, or if
|
---|
674 | .B grep
|
---|
675 | was not compiled with national language support (\s-1NLS\s0).
|
---|
676 | .TP
|
---|
677 | .B GREP_OPTIONS
|
---|
678 | This variable specifies default options to be placed in front of any
|
---|
679 | explicit options. For example, if
|
---|
680 | .B GREP_OPTIONS
|
---|
681 | is
|
---|
682 | .BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,
|
---|
683 | .B grep
|
---|
684 | behaves as if the two options
|
---|
685 | .B \-\^\-binary-files=without-match
|
---|
686 | and
|
---|
687 | .B \-\^\-directories=skip
|
---|
688 | had been specified before any explicit options.
|
---|
689 | Option specifications are separated by whitespace.
|
---|
690 | A backslash escapes the next character,
|
---|
691 | so it can be used to specify an option containing whitespace or a backslash.
|
---|
692 | .TP
|
---|
693 | .B GREP_COLOR
|
---|
694 | Specifies the marker for highlighting.
|
---|
695 | .TP
|
---|
696 | \fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fP
|
---|
697 | These variables specify the
|
---|
698 | .B LC_COLLATE
|
---|
699 | locale, which determines the collating sequence used to interpret
|
---|
700 | range expressions like
|
---|
701 | .BR [a\-z] .
|
---|
702 | .TP
|
---|
703 | \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
|
---|
704 | These variables specify the
|
---|
705 | .B LC_CTYPE
|
---|
706 | locale, which determines the type of characters, e.g., which
|
---|
707 | characters are whitespace.
|
---|
708 | .TP
|
---|
709 | \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
|
---|
710 | These variables specify the
|
---|
711 | .B LC_MESSAGES
|
---|
712 | locale, which determines the language that
|
---|
713 | .B grep
|
---|
714 | uses for messages.
|
---|
715 | The default C locale uses American English messages.
|
---|
716 | .TP
|
---|
717 | .B POSIXLY_CORRECT
|
---|
718 | If set,
|
---|
719 | .B grep
|
---|
720 | behaves as \s-1POSIX.2\s0 requires; otherwise,
|
---|
721 | .B grep
|
---|
722 | behaves more like other \s-1GNU\s0 programs.
|
---|
723 | \s-1POSIX.2\s0 requires that options that follow file names must be
|
---|
724 | treated as file names; by default, such options are permuted to the
|
---|
725 | front of the operand list and are treated as options.
|
---|
726 | Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as
|
---|
727 | \*(lqillegal\*(rq, but since they are not really against the law the default
|
---|
728 | is to diagnose them as \*(lqinvalid\*(rq.
|
---|
729 | .B POSIXLY_CORRECT
|
---|
730 | also disables \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP,
|
---|
731 | described below.
|
---|
732 | .TP
|
---|
733 | \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP
|
---|
734 | (Here
|
---|
735 | .I N
|
---|
736 | is
|
---|
737 | .BR grep 's
|
---|
738 | numeric process ID.) If the
|
---|
739 | .IR i th
|
---|
740 | character of this environment variable's value is
|
---|
741 | .BR 1 ,
|
---|
742 | do not consider the
|
---|
743 | .IR i th
|
---|
744 | operand of
|
---|
745 | .B grep
|
---|
746 | to be an option, even if it appears to be one.
|
---|
747 | A shell can put this variable in the environment for each command it runs,
|
---|
748 | specifying which operands are the results of file name wildcard
|
---|
749 | expansion and therefore should not be treated as options.
|
---|
750 | This behavior is available only with the \s-1GNU\s0 C library, and only
|
---|
751 | when
|
---|
752 | .B POSIXLY_CORRECT
|
---|
753 | is not set.
|
---|
754 | .SH DIAGNOSTICS
|
---|
755 | .PP
|
---|
756 | Normally, exit status is 0 if selected lines are found and 1 otherwise.
|
---|
757 | But the exit status is 2 if an error occurred, unless the
|
---|
758 | .B \-q
|
---|
759 | or
|
---|
760 | .B \-\^\-quiet
|
---|
761 | or
|
---|
762 | .B \-\^\-silent
|
---|
763 | option is used and a selected line is found.
|
---|
764 | .SH BUGS
|
---|
765 | .PP
|
---|
766 | Email bug reports to
|
---|
767 | .BR bug-grep@gnu.org .
|
---|
768 | .PP
|
---|
769 | Large repetition counts in the
|
---|
770 | .BI { n , m }
|
---|
771 | construct may cause grep to use lots of memory.
|
---|
772 | In addition,
|
---|
773 | certain other obscure regular expressions require exponential time
|
---|
774 | and space, and may cause
|
---|
775 | .B grep
|
---|
776 | to run out of memory.
|
---|
777 | .PP
|
---|
778 | Backreferences are very slow, and may require exponential time.
|
---|
779 | .\" Work around problems with some troff -man implementations.
|
---|
780 | .br
|
---|