Penguin
Annotated edit history of grep(1) version 1, including all changes. View license author blame.
Rev Author # Line
1 perry 1 GREP
2 !!!GREP
3 NAME
4 SYNOPSIS
5 DESCRIPTION
6 OPTIONS
7 REGULAR EXPRESSIONS
8 ENVIRONMENT VARIABLES
9 DIAGNOSTICS
10 BUGS
11 ----
12 !!NAME
13
14
15 grep, egrep, fgrep - print lines matching a pattern
16 !!SYNOPSIS
17
18
19 __grep__ [[''options''] ''PATTERN''
20 [[''FILE''...]__
21 grep__ [[''options''] [[__-e__ ''PATTERN'' |
22 __-f__ ''FILE''] [[''FILE''...]
23 !!DESCRIPTION
24
25
26 __Grep__ searches the named input ''FILE''s (or
27 standard input if no files are named, or the file name
28 __-__ is given) for lines containing a match to the given
29 ''PATTERN''. By default, __grep__ prints the matching
30 lines.
31
32
33 In addition, two variant programs __egrep__ and
34 __fgrep__ are available. __Egrep__ is the same as
35 __grep -E__. __Fgrep__ is the same as __grep
36 -F__.
37 !!OPTIONS
38
39
40 __-A__ ''NUM'',
41 __--after-context=__''NUM''
42
43
44 Print ''NUM'' lines of trailing context after matching
45 lines.
46
47
48 __-a__, __--text__
49
50
51 Process a binary file as if it were text; this is equivalent
52 to the __--binary-files=text__ option.
53
54
55 __-B__ ''NUM'',
56 __--before-context=__''NUM''
57
58
59 Print ''NUM'' lines of leading context before matching
60 lines.
61
62
63 __-C__ [[''NUM''], __-__''NUM'',
64 __--context__[[__=__''NUM'']
65
66
67 Print ''NUM'' lines (default 2) of output
68 context.
69
70
71 __-b__, __--byte-offset__
72
73
74 Print the byte offset within the input file before each line
75 of output.
76
77
78 __--binary-files=__''TYPE''
79
80
81 If the first few bytes of a file indicate that the file
82 contains binary data, assume that the file is of type
83 ''TYPE''. By default, ''TYPE'' is __binary__, and
84 __grep__ normally outputs either a one-line message
85 saying that a binary file matches, or no message if there is
86 no match. If ''TYPE'' is __without-match__,
87 __grep__ assumes that a binary file does not match; this
88 is equivalent to the __-I__ option. If ''TYPE'' is
89 __text__, __grep__ processes a binary file as if it
90 were text; this is equivalent to the __-a__ option.
91 ''Warning:'' __grep --binary-files=text__ might output
92 binary garbage, which can have nasty side effects if the
93 output is a terminal and if the terminal driver interprets
94 some of it as commands.
95
96
97 __-c__, __--count__
98
99
100 Suppress normal output; instead print a count of matching
101 lines for each input file. With the __-v__,
102 __--invert-match__ option (see below), count non-matching
103 lines.
104
105
106 __-d__ ''ACTION'',
107 __--directories=__''ACTION''
108
109
110 If an input file is a directory, use ''ACTION'' to
111 process it. By default, ''ACTION'' is __read__, which
112 means that directories are read just as if they were
113 ordinary files. If ''ACTION'' is __skip__, directories
114 are silently skipped. If ''ACTION'' is __recurse__,
115 __grep__ reads all files under each directory,
116 recursively; this is equivalent to the __-r__
117 option.
118
119
120 __-E__, __--extended-regexp__
121
122
123 Interpret ''PATTERN'' as an extended regular expression
124 (see below).
125
126
127 __-e__ ''PATTERN'',
128 __--regexp=__''PATTERN''
129
130
131 Use ''PATTERN'' as the pattern; useful to protect
132 patterns beginning with __-__.
133
134
135 __-F__, __--fixed-strings__
136
137
138 Interpret ''PATTERN'' as a list of fixed strings,
139 separated by newlines, any of which is to be
140 matched.
141
142
143 __-f__ ''FILE'',
144 __--file=__''FILE''
145
146
147 Obtain patterns from ''FILE'', one per line. The empty
148 file contains zero patterns, and therefore matches
149 nothing.
150
151
152 __-G__, __--basic-regexp__
153
154
155 Interpret ''PATTERN'' as a basic regular expression (see
156 below). This is the default.
157
158
159 __-H__, __--with-filename__
160
161
162 Print the filename for each match.
163
164
165 __-h__, __--no-filename__
166
167
168 Suppress the prefixing of filenames on output when multiple
169 files are searched.
170
171
172 __--help__
173
174
175 Output a brief help message.
176
177
178 __-I__
179
180
181 Process a binary file as if it did not contain matching
182 data; this is equivalent to the
183 __--binary-files=without-match__ option.
184
185
186 __-i__, __--ignore-case__
187
188
189 Ignore case distinctions in both the ''PATTERN'' and the
190 input files.
191
192
193 __-L__, __--files-without-match__
194
195
196 Suppress normal output; instead print the name of each input
197 file from which no output would normally have been printed.
198 The scanning will stop on the first match.
199
200
201 __-l__, __--files-with-matches__
202
203
204 Suppress normal output; instead print the name of each input
205 file from which output would normally have been printed. The
206 scanning will stop on the first match.
207
208
209 __--mmap__
210
211
212 If possible, use the mmap(2) system call to read
213 input, instead of the default read(2) system call. In
214 some situations, __--mmap__ yields better performance.
215 However, __--mmap__ can cause undefined behavior
216 (including core dumps) if an input file shrinks while
217 __grep__ is operating, or if an I/O error
218 occurs.
219
220
221 __-n__, __--line-number__
222
223
224 Prefix each line of output with the line number within its
225 input file.
226
227
228 __-q__, __--quiet__, __--silent__
229
230
231 Quiet; suppress normal output. The scanning will stop on the
232 first match. Also see the __-s__ or __--no-messages__
233 option below.
234
235
236 __-r__, __--recursive__
237
238
239 Read all files under each directory, recursively; this is
240 equivalent to the __-d recurse__ option.
241
242
243 __-s__, __--no-messages__
244
245
246 Suppress error messages about nonexistent or unreadable
247 files. Portability note: unlike GNU
248 __grep__, traditional __grep__ did not conform to
249 POSIX.2 , because traditional __grep__
250 lacked a __-q__ option and its __-s__ option behaved
251 like GNU __grep__'s __-q__ option.
252 Shell scripts intended to be portable to traditional
253 __grep__ should avoid both __-q__ and __-s__ and
254 should redirect output to /dev/null instead.
255
256
257 __-U__, __--binary__
258
259
260 Treat the file(s) as binary. By default, under MS-DOS and
261 MS-Windows, __grep__ guesses the file type by looking at
262 the contents of the first 32KB read from the file. If
263 __grep__ decides the file is a text file, it strips the
264 CR characters from the original file contents (to make
265 regular expressions with __^__ and __$__ work
266 correctly). Specifying __-U__ overrules this guesswork,
267 causing all files to be read and passed to the matching
268 mechanism verbatim; if the file is a text file with CR/LF
269 pairs at the end of each line, this will cause some regular
270 expressions to fail. This option has no effect on platforms
271 other than MS-DOS and MS-Windows.
272
273
274 __-u__, __--unix-byte-offsets__
275
276
277 Report Unix-style byte offsets. This switch causes
278 __grep__ to report byte offsets as if the file were
279 Unix-style text file, i.e. with CR characters stripped off.
280 This will produce results identical to running __grep__
281 on a Unix machine. This option has no effect unless
282 __-b__ option is also used; it has no effect on platforms
283 other than MS-DOS and MS-Windows.
284
285
286 __-V__, __--version__
287
288
289 Print the version number of __grep__ to standard error.
290 This version number should be included in all bug reports
291 (see below).
292
293
294 __-v__, __--invert-match__
295
296
297 Invert the sense of matching, to select non-matching
298 lines.
299
300
301 __-w__, __--word-regexp__
302
303
304 Select only those lines containing matches that form whole
305 words. The test is that the matching substring must either
306 be at the beginning of the line, or preceded by a non-word
307 constituent character. Similarly, it must be either at the
308 end of the line or followed by a non-word constituent
309 character. Word-constituent characters are letters, digits,
310 and the underscore.
311
312
313 __-x__, __--line-regexp__
314
315
316 Select only those matches that exactly match the whole
317 line.
318
319
320 __-y__
321
322
323 Obsolete synonym for __-i__.
324
325
326 __-Z__, __--null__
327
328
329 Output a zero byte (the ASCII __NUL__
330 character) instead of the character that normally follows a
331 file name. For example, __grep -lZ__ outputs a zero byte
332 after each file name instead of the usual newline. This
333 option makes the output unambiguous, even in the presence of
334 file names containing unusual characters like newlines. This
335 option can be used with commands like __find -print0__,
336 __perl -0__, __sort -z__, and __xargs -0__ to
337 process arbitrary file names, even those that contain
338 newline characters.
339 !!REGULAR EXPRESSIONS
340
341
342 A regular expression is a pattern that describes a set of
343 strings. Regular expressions are constructed analogously to
344 arithmetic expressions, by using various operators to
345 combine smaller expressions.
346
347
348 __Grep__ understands two different versions of regular
349 expression syntax: ``basic'' and ``extended.'' In
350 GNU __grep__, there is no difference in
351 available functionality using either syntax. In other
352 implementations, basic regular expressions are less
353 powerful. The following description applies to extended
354 regular expressions; differences for basic regular
355 expressions are summarized afterwards.
356
357
358 The fundamental building blocks are the regular expressions
359 that match a single character. Most characters, including
360 all letters and digits, are regular expressions that match
361 themselves. Any metacharacter with special meaning may be
362 quoted by preceding it with a backslash.
363
364
365 A list of characters enclosed by __[[__ and __]__
366 matches any single character in that list; if the first
367 character of the list is the caret __^__ then it matches
368 any character ''not'' in the list. For example, the
369 regular expression __[[0123456789]__ matches any single
370 digit. A range of characters may be specified by giving the
371 first and last characters, separated by a hyphen. Finally,
372 certain named classes of characters are predefined. Their
373 names are self explanatory, and they are __[[:alnum:]__,
374 __[[:alpha:]__, __[[:cntrl:]__, __[[:digit:]__,
375 __[[:graph:]__, __[[:lower:]__, __[[:print:]__,
376 __[[:punct:]__, __[[:space:]__, __[[:upper:]__, and
377 __[[:xdigit:].__ For example, __[[[[:alnum:]]__ means
378 __[[0-9A-Za-z]__, except the latter form depends upon the
379 POSIX locale and the ASCII
380 character encoding, whereas the former is independent of
381 locale and character set. (Note that the brackets in these
382 class names are part of the symbolic names, and must be
383 included in addition to the brackets delimiting the bracket
384 list.) Most metacharacters lose their special meaning inside
385 lists. To include a literal __]__ place it first in the
386 list. Similarly, to include a literal __^__ place it
387 anywhere but first. Finally, to include a literal __-__
388 place it last.
389
390
391 The period __.__ matches any single character. The symbol
392 __w__ is a synonym for __[[[[:alnum:]]__ and __W__ is
393 a synonym for __[[^[[:alnum]]__.
394
395
396 The caret __^__ and the dollar sign __$__ are
397 metacharacters that respectively match the empty string at
398 the beginning and end of a line. The symbols ____ and
399 ____ respectively match the empty string at the
400 beginning and end of a word. The symbol __b__ matches the
401 empty string at the edge of a word, and __B__ matches the
402 empty string provided it's ''not'' at the edge of a
403 word.
404
405
406 A regular expression may be followed by one of several
407 repetition operators:
408
409
410 __?__
411
412
413 The preceding item is optional and matched at most
414 once.
415
416
417 __*__
418
419
420 The preceding item will be matched zero or more
421 times.
422
423
424 __+__
425
426
427 The preceding item will be matched one or more
428 times.
429
430
431 __{__''n''__}__
432
433
434 The preceding item is matched exactly ''n''
435 times.
436
437
438 __{__''n''__,}__
439
440
441 The preceding item is matched ''n'' or more
442 times.
443
444
445 __{__''n''__,__''m''__}__
446
447
448 The preceding item is matched at least ''n'' times, but
449 not more than ''m'' times.
450
451
452 Two regular expressions may be concatenated; the resulting
453 regular expression matches any string formed by
454 concatenating two substrings that respectively match the
455 concatenated subexpressions.
456
457
458 Two regular expressions may be joined by the infix operator
459 __|__; the resulting regular expression matches any
460 string matching either subexpression.
461
462
463 Repetition takes precedence over concatenation, which in
464 turn takes precedence over alternation. A whole
465 subexpression may be enclosed in parentheses to override
466 these precedence rules.
467
468
469 The backreference __\__''n'', where ''n'' is a
470 single digit, matches the substring previously matched by
471 the ''n''th parenthesized subexpression of the regular
472 expression.
473
474
475 In basic regular expressions the metacharacters __?__,
476 __+__, __{__, __}__, __|__, __(__, and
477 __)__ lose their special meaning; instead use the
478 backslashed versions __?__, __+__, __{__, __}__,
479 __|__, ____, and __)__.
480
481
482 Traditional __egrep__ did not support the __{__
483 metacharacter, and some __egrep__ implementations support
484 __{__ instead, so portable scripts should avoid __{__
485 in __egrep__ patterns and should use __[[{]__ to match
486 a literal __{__.
487
488
489 GNU __egrep__ attempts to support
490 traditional usage by assuming that __{__ is not special
491 if it would be the start of an invalid interval
492 specification. For example, the shell command __egrep
493 '{1'__ searches for the two-character string __{1__
494 instead of reporting a syntax error in the regular
495 expression. POSIX.2 allows this behavior as
496 an extension, but portable scripts should avoid
497 it.
498 !!ENVIRONMENT VARIABLES
499
500
501 __GREP_OPTIONS__
502
503
504 This variable specifies default options to be placed in
505 front of any explicit options. For example, if
506 __GREP_OPTIONS__ is __'--binary-files=without-match
507 --directories=skip'__, __grep__ behaves as if the two
508 options __--binary-files=without-match__ and
509 __--directories=skip__ had been specified before any
510 explicit options. Option specifications are separated by
511 whitespace. A backslash escapes the next character, so it
512 can be used to specify an option containing whitespace or a
513 backslash.
514
515
516 __LC_ALL__, __LC_MESSAGES__, __LANG__
517
518
519 These variables specify the __LC_MESSAGES__ locale, which
520 determines the language that __grep__ uses for messages.
521 The locale is determined by the first of these variables
522 that is set. American English is used if none of these
523 environment variables are set, or if the message catalog is
524 not installed, or if __grep__ was not compiled with
525 national language support ( NLS
526 ).
527
528
529 __LC_ALL__, __LC_CTYPE__, __LANG__
530
531
532 These variables specify the __LC_CTYPE__ locale, which
533 determines the type of characters, e.g., which characters
534 are whitespace. The locale is determined by the first of
535 these variables that is set. The POSIX locale
536 is used if none of these environment variables are set, or
537 if the locale catalog is not installed, or if __grep__
538 was not compiled with national language support (
539 NLS ).
540
541
542 __POSIXLY_CORRECT__
543
544
545 If set, __grep__ behaves as POSIX.2
546 requires; otherwise, __grep__ behaves more like other
547 GNU programs. POSIX.2 requires
548 that options that follow file names must be treated as file
549 names; by default, such options are permuted to the front of
550 the operand list and are treated as options. Also,
551 POSIX.2 requires that unrecognized options be
552 diagnosed as ``illegal'', but since they are not really
553 against the law the default is to diagnose them as
554 ``invalid''. __POSIXLY_CORRECT__ also disables
555 _____''N''___GNU_nonoption_argv_flags___, described
556 below.
557
558
559 _____''N''___GNU_nonoption_argv_flags___
560
561
562 (Here ''N'' is __grep__'s numeric process ID.) If the
563 ''i''th character of this environment variable's value is
564 __1__, do not consider the ''i''th operand of
565 __grep__ to be an option, even if it appears to be one. A
566 shell can put this variable in the environment for each
567 command it runs, specifying which operands are the results
568 of file name wildcard expansion and therefore should not be
569 treated as options. This behavior is available only with the
570 GNU C library, and only when
571 __POSIXLY_CORRECT__ is not set.
572 !!DIAGNOSTICS
573
574
575 Normally, exit status is 0 if matches were found, and 1 if
576 no matches were found. (The __-v__ option inverts the
577 sense of the exit status.) Exit status is 2 if there were
578 syntax errors in the pattern, inaccessible input files, or
579 other system errors.
580 !!BUGS
581
582
583 Email bug reports to __bug-gnu-utils@gnu.org__. Be sure
584 to include the word ``grep'' somewhere in the ``Subject:''
585 field.
586
587
588 Large repetition counts in the
589 __{__''m''__,__''n''__}__ construct may cause
590 grep to use lots of memory. In addition, certain other
591 obscure regular expressions require exponential time and
592 space, and may cause __grep__ to run out of
593 memory.
594
595
596 Backreferences are very slow, and may require exponential
597 time.
598 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.