/[pcre]/code/trunk/doc/pcregrep.txt
ViewVC logotype

Contents of /code/trunk/doc/pcregrep.txt

Parent Directory Parent Directory | Revision Log Revision Log


Revision 392 - (hide annotations) (download)
Tue Mar 17 21:30:30 2009 UTC (4 years, 2 months ago) by ph10
File MIME type: text/plain
File size: 27823 byte(s)
Update after detrailing for a test release.

1 nigel 73 PCREGREP(1) PCREGREP(1)
2 nigel 49
3    
4 nigel 73 NAME
5     pcregrep - a grep with Perl-compatible regular expressions.
6    
7 nigel 79
8 nigel 49 SYNOPSIS
9 nigel 87 pcregrep [options] [long options] [pattern] [path1 path2 ...]
10 nigel 49
11    
12 nigel 63 DESCRIPTION
13 nigel 49
14 nigel 73 pcregrep searches files for character patterns, in the same way as
15     other grep commands do, but it uses the PCRE regular expression library
16     to support patterns that are compatible with the regular expressions of
17 nigel 93 Perl 5. See pcrepattern(3) for a full description of syntax and seman-
18     tics of the regular expressions that PCRE supports.
19 nigel 49
20 nigel 87 Patterns, whether supplied on the command line or in a separate file,
21     are given without delimiters. For example:
22 nigel 63
23 nigel 87 pcregrep Thursday /etc/motd
24    
25     If you attempt to use delimiters (for example, by surrounding a pattern
26     with slashes, as is common in Perl scripts), they are interpreted as
27 ph10 286 part of the pattern. Quotes can of course be used to delimit patterns
28     on the command line because they are interpreted by the shell, and
29     indeed they are required if a pattern contains white space or shell
30     metacharacters.
31 nigel 87
32 ph10 286 The first argument that follows any option settings is treated as the
33     single pattern to be matched when neither -e nor -f is present. Con-
34     versely, when one or both of these options are used to specify pat-
35 nigel 87 terns, all arguments are treated as path names. At least one of -e, -f,
36     or an argument pattern must be provided.
37    
38 nigel 77 If no files are specified, pcregrep reads the standard input. The stan-
39 ph10 286 dard input can also be referenced by a name consisting of a single
40 nigel 77 hyphen. For example:
41 nigel 49
42 nigel 77 pcregrep some-pattern /file1 - /file3
43 nigel 49
44 ph10 286 By default, each line that matches a pattern is copied to the standard
45     output, and if there is more than one file, the file name is output at
46     the start of each line, followed by a colon. However, there are options
47     that can change how pcregrep behaves. In particular, the -M option
48     makes it possible to search for patterns that span line boundaries.
49     What defines a line boundary is controlled by the -N (--newline)
50     option.
51 nigel 49
52 nigel 91 Patterns are limited to 8K or BUFSIZ characters, whichever is the
53 ph10 286 greater. BUFSIZ is defined in <stdio.h>. When there is more than one
54     pattern (specified by the use of -e and/or -f), each pattern is applied
55     to each line in the order in which they are defined, except that all
56 ph10 392 the -e patterns are tried before the -f patterns.
57 nigel 77
58 ph10 392 By default, as soon as one pattern matches (or fails to match when -v
59     is used), no further patterns are considered. However, if --colour (or
60     --color) is used to colour the matching substrings, or if --only-match-
61     ing, --file-offsets, or --line-offsets is used to output only the part
62     of the line that matched (either shown literally, or as an offset),
63     scanning resumes immediately following the match, so that further
64     matches on the same line can be found. If there are multiple patterns,
65     they are all tried on the remainder of the line, but patterns that fol-
66     low the one that matched are not tried on the earlier part of the line.
67 ph10 286
68 ph10 392 This is the same behaviour as GNU grep, but it does mean that the order
69     in which multiple patterns are specified can affect the output when one
70     of the above options is used.
71    
72     Patterns that can match an empty string are accepted, but empty string
73     matches are not recognized. An example is the pattern "(super)?(man)?",
74     in which all components are optional. This pattern finds all occur-
75     rences of both "super" and "man"; the output differs from matching with
76     "super|man" when only the matching substrings are being shown.
77    
78 ph10 286 If the LC_ALL or LC_CTYPE environment variable is set, pcregrep uses
79     the value to set a locale when calling the PCRE library. The --locale
80 nigel 87 option can be used to override this.
81 nigel 77
82 nigel 87
83 ph10 286 SUPPORT FOR COMPRESSED FILES
84    
85     It is possible to compile pcregrep so that it uses libz or libbz2 to
86     read files whose names end in .gz or .bz2, respectively. You can find
87     out whether your binary has support for one or both of these file types
88     by running it with the --help option. If the appropriate support is not
89     present, files are treated as plain text. The standard input is always
90     so treated.
91    
92    
93 nigel 63 OPTIONS
94 nigel 49
95 nigel 91 -- This terminate the list of options. It is useful if the next
96     item on the command line starts with a hyphen but is not an
97     option. This allows for the processing of patterns and file-
98 nigel 87 names that start with hyphens.
99 nigel 63
100 nigel 87 -A number, --after-context=number
101 nigel 91 Output number lines of context after each matching line. If
102 nigel 87 filenames and/or line numbers are being output, a hyphen sep-
103 nigel 91 arator is used instead of a colon for the context lines. A
104     line containing "--" is output between each group of lines,
105     unless they are in fact contiguous in the input file. The
106     value of number is expected to be relatively small. However,
107 nigel 87 pcregrep guarantees to have up to 8K of following text avail-
108     able for context output.
109    
110     -B number, --before-context=number
111 nigel 91 Output number lines of context before each matching line. If
112 nigel 87 filenames and/or line numbers are being output, a hyphen sep-
113 nigel 91 arator is used instead of a colon for the context lines. A
114     line containing "--" is output between each group of lines,
115     unless they are in fact contiguous in the input file. The
116     value of number is expected to be relatively small. However,
117 nigel 77 pcregrep guarantees to have up to 8K of preceding text avail-
118 nigel 87 able for context output.
119 nigel 77
120 nigel 87 -C number, --context=number
121 nigel 91 Output number lines of context both before and after each
122     matching line. This is equivalent to setting both -A and -B
123 nigel 77 to the same value.
124    
125 nigel 87 -c, --count
126 nigel 91 Do not output individual lines; instead just output a count
127 nigel 87 of the number of lines that would otherwise have been output.
128 nigel 91 If several files are given, a count is output for each of
129 nigel 87 them. In this mode, the -A, -B, and -C options are ignored.
130 nigel 49
131 nigel 87 --colour, --color
132     If this option is given without any data, it is equivalent to
133 nigel 91 "--colour=auto". If data is required, it must be given in
134 nigel 87 the same shell item, separated by an equals sign.
135    
136     --colour=value, --color=value
137 ph10 392 This option specifies under what circumstances the parts of a
138 nigel 87 line that matched a pattern should be coloured in the output.
139 ph10 392 By default, the output is not coloured. The value (which is
140     optional, see above) may be "never", "always", or "auto". In
141     the latter case, colouring happens only if the standard out-
142     put is connected to a terminal. More resources are used when
143     colouring is enabled, because pcregrep has to search for all
144     possible matches in a line, not just one, in order to colour
145     them all.
146 nigel 87
147 ph10 392 The colour that is used can be specified by setting the envi-
148     ronment variable PCREGREP_COLOUR or PCREGREP_COLOR. The value
149     of this variable should be a string of two numbers, separated
150     by a semicolon. They are copied directly into the control
151     string for setting colour on a terminal, so it is your
152     responsibility to ensure that they make sense. If neither of
153     the environment variables is set, the default is "1;31",
154     which gives red.
155    
156 nigel 87 -D action, --devices=action
157 nigel 91 If an input path is not a regular file or a directory,
158     "action" specifies how it is to be processed. Valid values
159 ph10 392 are "read" (the default) or "skip" (silently skip the path).
160 nigel 87
161     -d action, --directories=action
162     If an input path is a directory, "action" specifies how it is
163 ph10 392 to be processed. Valid values are "read" (the default),
164     "recurse" (equivalent to the -r option), or "skip" (silently
165     skip the path). In the default case, directories are read as
166     if they were ordinary files. In some operating systems the
167     effect of reading a directory like this is an immediate end-
168 nigel 87 of-file.
169    
170 ph10 286 -e pattern, --regex=pattern, --regexp=pattern
171     Specify a pattern to be matched. This option can be used mul-
172     tiple times in order to specify several patterns. It can also
173 ph10 392 be used as a way of specifying a single pattern that starts
174     with a hyphen. When -e is used, no argument pattern is taken
175     from the command line; all arguments are treated as file
176     names. There is an overall maximum of 100 patterns. They are
177     applied to each line in the order in which they are defined
178 ph10 286 until one matches (or fails to match if -v is used). If -f is
179 ph10 392 used with -e, the command line patterns are matched first,
180     followed by the patterns from the file, independent of the
181     order in which these options are specified. Note that multi-
182 ph10 286 ple use of -e is not the same as a single pattern with alter-
183     natives. For example, X|Y finds the first character in a line
184 ph10 392 that is X or Y, whereas if the two patterns are given sepa-
185 ph10 286 rately, pcregrep finds X if it is present, even if it follows
186 ph10 392 Y in the line. It finds Y only if there is no X in the line.
187     This really matters only if you are using -o to show the
188 ph10 286 part(s) of the line that matched.
189 nigel 87
190 nigel 77 --exclude=pattern
191     When pcregrep is searching the files in a directory as a con-
192 ph10 392 sequence of the -r (recursive search) option, any regular
193 ph10 345 files whose names match the pattern are excluded. Subdirecto-
194 ph10 392 ries are not excluded by this option; they are searched
195     recursively, subject to the --exclude_dir and --include_dir
196     options. The pattern is a PCRE regular expression, and is
197 ph10 345 matched against the final component of the file name (not the
198 ph10 392 entire path). If a file name matches both --include and
199     --exclude, it is excluded. There is no short form for this
200 nigel 77 option.
201    
202 ph10 345 --exclude_dir=pattern
203 ph10 392 When pcregrep is searching the contents of a directory as a
204     consequence of the -r (recursive search) option, any subdi-
205     rectories whose names match the pattern are excluded. (Note
206     that the --exclude option does not affect subdirectories.)
207     The pattern is a PCRE regular expression, and is matched
208     against the final component of the name (not the entire
209     path). If a subdirectory name matches both --include_dir and
210     --exclude_dir, it is excluded. There is no short form for
211 ph10 345 this option.
212    
213 nigel 87 -F, --fixed-strings
214 ph10 392 Interpret each pattern as a list of fixed strings, separated
215     by newlines, instead of as a regular expression. The -w
216     (match as a word) and -x (match whole line) options can be
217 nigel 87 used with -F. They apply to each of the fixed strings. A line
218     is selected if any of the fixed strings are found in it (sub-
219     ject to -w or -x, if present).
220    
221     -f filename, --file=filename
222 ph10 392 Read a number of patterns from the file, one per line, and
223     match them against each line of input. A data line is output
224 nigel 87 if any of the patterns match it. The filename can be given as
225     "-" to refer to the standard input. When -f is used, patterns
226 ph10 392 specified on the command line using -e may also be present;
227 nigel 87 they are tested before the file's patterns. However, no other
228 ph10 392 pattern is taken from the command line; all arguments are
229     treated as file names. There is an overall maximum of 100
230 nigel 87 patterns. Trailing white space is removed from each line, and
231 ph10 392 blank lines are ignored. An empty file contains no patterns
232     and therefore matches nothing. See also the comments about
233     multiple patterns versus a single pattern with alternatives
234 ph10 286 in the description of -e above.
235 nigel 53
236 ph10 286 --file-offsets
237 ph10 392 Instead of showing lines or parts of lines that match, show
238     each match as an offset from the start of the file and a
239     length, separated by a comma. In this mode, no context is
240     shown. That is, the -A, -B, and -C options are ignored. If
241 ph10 286 there is more than one match in a line, each of them is shown
242 ph10 392 separately. This option is mutually exclusive with --line-
243 ph10 286 offsets and --only-matching.
244    
245 nigel 87 -H, --with-filename
246 ph10 392 Force the inclusion of the filename at the start of output
247     lines when searching a single file. By default, the filename
248     is not shown in this case. For matching lines, the filename
249     is followed by a colon; for context lines, a hyphen separator
250     is used. If a line number is also being output, it follows
251     the file name.
252 nigel 49
253 nigel 87 -h, --no-filename
254 ph10 392 Suppress the output filenames when searching multiple files.
255     By default, filenames are shown when multiple files are
256     searched. For matching lines, the filename is followed by a
257     colon; for context lines, a hyphen separator is used. If a
258     line number is also being output, it follows the file name.
259 nigel 49
260 ph10 286 --help Output a help message, giving brief details of the command
261     options and file type support, and then exit.
262 nigel 87
263     -i, --ignore-case
264     Ignore upper/lower case distinctions during comparisons.
265    
266 nigel 77 --include=pattern
267     When pcregrep is searching the files in a directory as a con-
268 ph10 345 sequence of the -r (recursive search) option, only those reg-
269     ular files whose names match the pattern are included. Subdi-
270     rectories are always included and searched recursively, sub-
271     ject to the --include_dir and --exclude_dir options. The pat-
272     tern is a PCRE regular expression, and is matched against the
273     final component of the file name (not the entire path). If a
274     file name matches both --include and --exclude, it is
275     excluded. There is no short form for this option.
276 nigel 49
277 ph10 345 --include_dir=pattern
278     When pcregrep is searching the contents of a directory as a
279     consequence of the -r (recursive search) option, only those
280     subdirectories whose names match the pattern are included.
281     (Note that the --include option does not affect subdirecto-
282     ries.) The pattern is a PCRE regular expression, and is
283     matched against the final component of the name (not the
284     entire path). If a subdirectory name matches both
285     --include_dir and --exclude_dir, it is excluded. There is no
286     short form for this option.
287    
288 nigel 87 -L, --files-without-match
289 ph10 345 Instead of outputting lines from the files, just output the
290     names of the files that do not contain any lines that would
291     have been output. Each file name is output once, on a sepa-
292 nigel 77 rate line.
293    
294 nigel 87 -l, --files-with-matches
295 ph10 345 Instead of outputting lines from the files, just output the
296 nigel 87 names of the files containing lines that would have been out-
297 ph10 345 put. Each file name is output once, on a separate line.
298     Searching stops as soon as a matching line is found in a
299 nigel 87 file.
300 nigel 77
301     --label=name
302     This option supplies a name to be used for the standard input
303 nigel 87 when file names are being output. If not supplied, "(standard
304     input)" is used. There is no short form for this option.
305 nigel 77
306 ph10 286 --line-offsets
307 ph10 345 Instead of showing lines or parts of lines that match, show
308 ph10 286 each match as a line number, the offset from the start of the
309 ph10 345 line, and a length. The line number is terminated by a colon
310     (as usual; see the -n option), and the offset and length are
311     separated by a comma. In this mode, no context is shown.
312     That is, the -A, -B, and -C options are ignored. If there is
313     more than one match in a line, each of them is shown sepa-
314 ph10 286 rately. This option is mutually exclusive with --file-offsets
315     and --only-matching.
316    
317 nigel 87 --locale=locale-name
318 ph10 345 This option specifies a locale to be used for pattern match-
319     ing. It overrides the value in the LC_ALL or LC_CTYPE envi-
320     ronment variables. If no locale is specified, the PCRE
321     library's default (usually the "C" locale) is used. There is
322 nigel 87 no short form for this option.
323    
324     -M, --multiline
325 ph10 345 Allow patterns to match more than one line. When this option
326 nigel 77 is given, patterns may usefully contain literal newline char-
327 ph10 345 acters and internal occurrences of ^ and $ characters. The
328     output for any one match may consist of more than one line.
329     When this option is set, the PCRE library is called in "mul-
330     tiline" mode. There is a limit to the number of lines that
331     can be matched, imposed by the way that pcregrep buffers the
332     input file as it scans it. However, pcregrep ensures that at
333 nigel 77 least 8K characters or the rest of the document (whichever is
334 ph10 345 the shorter) are available for forward matching, and simi-
335 nigel 77 larly the previous 8K characters (or all the previous charac-
336 ph10 345 ters, if fewer than 8K) are guaranteed to be available for
337 nigel 77 lookbehind assertions.
338    
339 nigel 91 -N newline-type, --newline=newline-type
340 ph10 345 The PCRE library supports five different conventions for
341     indicating the ends of lines. They are the single-character
342     sequences CR (carriage return) and LF (linefeed), the two-
343     character sequence CRLF, an "anycrlf" convention, which rec-
344     ognizes any of the preceding three types, and an "any" con-
345 ph10 150 vention, in which any Unicode line ending sequence is assumed
346 ph10 345 to end a line. The Unicode sequences are the three just men-
347     tioned, plus VT (vertical tab, U+000B), FF (formfeed,
348     U+000C), NEL (next line, U+0085), LS (line separator,
349 ph10 150 U+2028), and PS (paragraph separator, U+2029).
350 nigel 91
351 nigel 93 When the PCRE library is built, a default line-ending
352 ph10 345 sequence is specified. This is normally the standard
353 nigel 93 sequence for the operating system. Unless otherwise specified
354 ph10 345 by this option, pcregrep uses the library's default. The
355 ph10 150 possible values for this option are CR, LF, CRLF, ANYCRLF, or
356 ph10 345 ANY. This makes it possible to use pcregrep on files that
357     have come from other environments without having to modify
358     their line endings. If the data that is being scanned does
359     not agree with the convention set by this option, pcregrep
360 ph10 150 may behave in strange ways.
361 nigel 93
362 nigel 87 -n, --line-number
363     Precede each output line by its line number in the file, fol-
364 ph10 392 lowed by a colon for matching lines or a hyphen for context
365     lines. If the filename is also being output, it precedes the
366     line number. This option is forced if --line-offsets is used.
367 nigel 49
368 nigel 87 -o, --only-matching
369 ph10 392 Show only the part of the line that matched a pattern. In
370     this mode, no context is shown. That is, the -A, -B, and -C
371     options are ignored. If there is more than one match in a
372     line, each of them is shown separately. If -o is combined
373     with -v (invert the sense of the match to find non-matching
374     lines), no output is generated, but the return code is set
375 ph10 286 appropriately. This option is mutually exclusive with --file-
376     offsets and --line-offsets.
377 nigel 87
378     -q, --quiet
379     Work quietly, that is, display nothing except error messages.
380 ph10 392 The exit status indicates whether or not any matches were
381 nigel 73 found.
382 nigel 49
383 nigel 87 -r, --recursive
384 ph10 392 If any given path is a directory, recursively scan the files
385     it contains, taking note of any --include and --exclude set-
386     tings. By default, a directory is read as a normal file; in
387     some operating systems this gives an immediate end-of-file.
388     This option is a shorthand for setting the -d option to
389 nigel 87 "recurse".
390 nigel 77
391 nigel 87 -s, --no-messages
392 ph10 392 Suppress error messages about non-existent or unreadable
393     files. Such files are quietly skipped. However, the return
394 nigel 77 code is still 2, even if matches were found in other files.
395    
396 nigel 87 -u, --utf-8
397 ph10 392 Operate in UTF-8 mode. This option is available only if PCRE
398     has been compiled with UTF-8 support. Both patterns and sub-
399 nigel 87 ject lines must be valid strings of UTF-8 characters.
400 nigel 63
401 nigel 87 -V, --version
402 ph10 392 Write the version numbers of pcregrep and the PCRE library
403 nigel 77 that is being used to the standard error stream.
404 nigel 49
405 nigel 87 -v, --invert-match
406 ph10 392 Invert the sense of the match, so that lines which do not
407 nigel 87 match any of the patterns are the ones that are found.
408 nigel 77
409 nigel 87 -w, --word-regex, --word-regexp
410     Force the patterns to match only whole words. This is equiva-
411 nigel 77 lent to having \b at the start and end of the pattern.
412    
413 nigel 87 -x, --line-regex, --line-regexp
414 ph10 392 Force the patterns to be anchored (each must start matching
415     at the beginning of a line) and in addition, require them to
416     match entire lines. This is equivalent to having ^ and $
417 nigel 73 characters at the start and end of each alternative branch in
418 nigel 87 every pattern.
419 nigel 49
420    
421 nigel 87 ENVIRONMENT VARIABLES
422 nigel 49
423 ph10 392 The environment variables LC_ALL and LC_CTYPE are examined, in that
424     order, for a locale. The first one that is set is used. This can be
425     overridden by the --locale option. If no locale is set, the PCRE
426 nigel 87 library's default (usually the "C" locale) is used.
427 nigel 49
428    
429 nigel 91 NEWLINES
430    
431 ph10 392 The -N (--newline) option allows pcregrep to scan files with different
432     newline conventions from the default. However, the setting of this
433     option does not affect the way in which pcregrep writes information to
434     the standard error and output streams. It uses the string "\n" in C
435     printf() calls to indicate newlines, relying on the C I/O library to
436     convert this to an appropriate sequence if the output is sent to a
437 nigel 91 file.
438    
439    
440 nigel 87 OPTIONS COMPATIBILITY
441 nigel 49
442 nigel 87 The majority of short and long forms of pcregrep's options are the same
443 ph10 392 as in the GNU grep program. Any long option of the form --xxx-regexp
444     (GNU terminology) is also available as --xxx-regex (PCRE terminology).
445     However, the --locale, -M, --multiline, -u, and --utf-8 options are
446 nigel 87 specific to pcregrep.
447    
448    
449 nigel 77 OPTIONS WITH DATA
450 nigel 49
451 nigel 77 There are four different ways in which an option with data can be spec-
452 ph10 392 ified. If a short form option is used, the data may follow immedi-
453 nigel 77 ately, or in the next command line item. For example:
454    
455     -f/some/file
456     -f /some/file
457    
458 ph10 392 If a long form option is used, the data may appear in the same command
459 nigel 87 line item, separated by an equals character, or (with one exception) it
460     may appear in the next command line item. For example:
461 nigel 77
462     --file=/some/file
463     --file /some/file
464    
465 ph10 392 Note, however, that if you want to supply a file name beginning with ~
466     as data in a shell command, and have the shell expand ~ to a home
467 nigel 87 directory, you must separate the file name from the option, because the
468 ph10 392 shell does not treat ~ specially unless it is at the start of an item.
469 nigel 77
470 ph10 345 The exception to the above is the --colour (or --color) option, for
471     which the data is optional. If this option does have data, it must be
472     given in the first form, using an equals character. Otherwise it will
473 nigel 87 be assumed that it has no data.
474    
475    
476     MATCHING ERRORS
477    
478 ph10 345 It is possible to supply a regular expression that takes a very long
479     time to fail to match certain lines. Such patterns normally involve
480     nested indefinite repeats, for example: (a+)*\d when matched against a
481     line of a's with no final digit. The PCRE matching function has a
482     resource limit that causes it to abort in these circumstances. If this
483 nigel 87 happens, pcregrep outputs an error message and the line that caused the
484 ph10 345 problem to the standard error stream. If there are more than 20 such
485 nigel 87 errors, pcregrep gives up.
486    
487    
488 nigel 63 DIAGNOSTICS
489 nigel 49
490 nigel 73 Exit status is 0 if any matches were found, 1 if no matches were found,
491 ph10 345 and 2 for syntax errors and non-existent or inacessible files (even if
492     matches were found in other files) or too many matching errors. Using
493     the -s option to suppress error messages about inaccessble files does
494 nigel 87 not affect the return code.
495 nigel 49
496    
497 nigel 93 SEE ALSO
498    
499     pcrepattern(3), pcretest(1).
500    
501    
502 nigel 49 AUTHOR
503 nigel 63
504 nigel 77 Philip Hazel
505 nigel 73 University Computing Service
506 nigel 93 Cambridge CB2 3QH, England.
507 nigel 49
508 ph10 99
509     REVISION
510    
511 ph10 392 Last updated: 01 March 2009
512     Copyright (c) 1997-2009 University of Cambridge.

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12