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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1470 - (hide annotations) (download)
Fri Apr 4 13:41:58 2014 UTC (8 months, 2 weeks ago) by ph10
File MIME type: text/plain
File size: 42257 byte(s)
Final tidies for 8.35 release.

1 ph10 1298 PCREGREP(1) General Commands Manual PCREGREP(1)
2 nigel 49
3    
4 ph10 1298
5 nigel 73 NAME
6     pcregrep - a grep with Perl-compatible regular expressions.
7    
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 ph10 1470 Perl 5. See pcresyntax(3) for a quick-reference summary of pattern syn-
18     tax, or pcrepattern(3) for a full description of the syntax and seman-
19 nigel 93 tics of the regular expressions that PCRE supports.
20 nigel 49
21 ph10 1470 Patterns, whether supplied on the command line or in a separate file,
22 nigel 87 are given without delimiters. For example:
23 nigel 63
24 nigel 87 pcregrep Thursday /etc/motd
25    
26     If you attempt to use delimiters (for example, by surrounding a pattern
27 ph10 1470 with slashes, as is common in Perl scripts), they are interpreted as
28     part of the pattern. Quotes can of course be used to delimit patterns
29     on the command line because they are interpreted by the shell, and
30     indeed quotes are required if a pattern contains white space or shell
31 ph10 286 metacharacters.
32 nigel 87
33 ph10 1470 The first argument that follows any option settings is treated as the
34     single pattern to be matched when neither -e nor -f is present. Con-
35     versely, when one or both of these options are used to specify pat-
36 nigel 87 terns, all arguments are treated as path names. At least one of -e, -f,
37     or an argument pattern must be provided.
38    
39 nigel 77 If no files are specified, pcregrep reads the standard input. The stan-
40 ph10 1470 dard input can also be referenced by a name consisting of a single
41 nigel 77 hyphen. For example:
42 nigel 49
43 nigel 77 pcregrep some-pattern /file1 - /file3
44 nigel 49
45 ph10 1470 By default, each line that matches a pattern is copied to the standard
46     output, and if there is more than one file, the file name is output at
47 ph10 286 the start of each line, followed by a colon. However, there are options
48 ph10 1470 that can change how pcregrep behaves. In particular, the -M option
49     makes it possible to search for patterns that span line boundaries.
50     What defines a line boundary is controlled by the -N (--newline)
51 ph10 286 option.
52 nigel 49
53 ph10 654 The amount of memory used for buffering files that are being scanned is
54 ph10 1470 controlled by a parameter that can be set by the --buffer-size option.
55     The default value for this parameter is specified when pcregrep is
56     built, with the default default being 20K. A block of memory three
57     times this size is used (to allow for buffering "before" and "after"
58 ph10 654 lines). An error occurs if a line overflows the buffer.
59 nigel 77
60 ph10 1470 Patterns can be no longer than 8K or BUFSIZ bytes, whichever is the
61     greater. BUFSIZ is defined in <stdio.h>. When there is more than one
62 ph10 1194 pattern (specified by the use of -e and/or -f), each pattern is applied
63 ph10 1470 to each line in the order in which they are defined, except that all
64 ph10 1194 the -e patterns are tried before the -f patterns.
65 ph10 654
66 ph10 1470 By default, as soon as one pattern matches a line, no further patterns
67 ph10 1194 are considered. However, if --colour (or --color) is used to colour the
68 ph10 1470 matching substrings, or if --only-matching, --file-offsets, or --line-
69     offsets is used to output only the part of the line that matched
70 ph10 1194 (either shown literally, or as an offset), scanning resumes immediately
71 ph10 1470 following the match, so that further matches on the same line can be
72     found. If there are multiple patterns, they are all tried on the
73     remainder of the line, but patterns that follow the one that matched
74 ph10 1194 are not tried on the earlier part of the line.
75 ph10 286
76 ph10 1470 This behaviour means that the order in which multiple patterns are
77     specified can affect the output when one of the above options is used.
78     This is no longer the same behaviour as GNU grep, which now manages to
79     display earlier matches for later patterns (as long as there is no
80 ph10 1194 overlap).
81 ph10 392
82 ph10 1470 Patterns that can match an empty string are accepted, but empty string
83 ph10 453 matches are never recognized. An example is the pattern
84 ph10 1470 "(super)?(man)?", in which all components are optional. This pattern
85     finds all occurrences of both "super" and "man"; the output differs
86     from matching with "super|man" when only the matching substrings are
87 ph10 453 being shown.
88 ph10 392
89 ph10 1470 If the LC_ALL or LC_CTYPE environment variable is set, pcregrep uses
90     the value to set a locale when calling the PCRE library. The --locale
91 nigel 87 option can be used to override this.
92 nigel 77
93 nigel 87
94 ph10 286 SUPPORT FOR COMPRESSED FILES
95    
96 ph10 1470 It is possible to compile pcregrep so that it uses libz or libbz2 to
97     read files whose names end in .gz or .bz2, respectively. You can find
98 ph10 286 out whether your binary has support for one or both of these file types
99     by running it with the --help option. If the appropriate support is not
100 ph10 1470 present, files are treated as plain text. The standard input is always
101 ph10 286 so treated.
102    
103    
104 ph10 954 BINARY FILES
105    
106 ph10 1470 By default, a file that contains a binary zero byte within the first
107     1024 bytes is identified as a binary file, and is processed specially.
108     (GNU grep also identifies binary files in this manner.) See the
109     --binary-files option for a means of changing the way binary files are
110 ph10 954 handled.
111    
112    
113 nigel 63 OPTIONS
114 nigel 49
115 ph10 1470 The order in which some of the options appear can affect the output.
116     For example, both the -h and -l options affect the printing of file
117     names. Whichever comes later in the command line will be the one that
118     takes effect. Similarly, except where noted below, if an option is
119     given twice, the later setting is used. Numerical values for options
120     may be followed by K or M, to signify multiplication by 1024 or
121 ph10 1194 1024*1024 respectively.
122 ph10 429
123 ph10 654 -- This terminates the list of options. It is useful if the next
124 ph10 1470 item on the command line starts with a hyphen but is not an
125     option. This allows for the processing of patterns and file-
126 nigel 87 names that start with hyphens.
127 nigel 63
128 nigel 87 -A number, --after-context=number
129 ph10 1470 Output number lines of context after each matching line. If
130 nigel 87 filenames and/or line numbers are being output, a hyphen sep-
131 ph10 1470 arator is used instead of a colon for the context lines. A
132     line containing "--" is output between each group of lines,
133     unless they are in fact contiguous in the input file. The
134     value of number is expected to be relatively small. However,
135 nigel 87 pcregrep guarantees to have up to 8K of following text avail-
136     able for context output.
137    
138 ph10 954 -a, --text
139 ph10 1470 Treat binary files as text. This is equivalent to --binary-
140 ph10 954 files=text.
141    
142 nigel 87 -B number, --before-context=number
143 ph10 1470 Output number lines of context before each matching line. If
144 nigel 87 filenames and/or line numbers are being output, a hyphen sep-
145 ph10 1470 arator is used instead of a colon for the context lines. A
146     line containing "--" is output between each group of lines,
147     unless they are in fact contiguous in the input file. The
148     value of number is expected to be relatively small. However,
149 nigel 77 pcregrep guarantees to have up to 8K of preceding text avail-
150 nigel 87 able for context output.
151 nigel 77
152 ph10 954 --binary-files=word
153 ph10 1470 Specify how binary files are to be processed. If the word is
154     "binary" (the default), pattern matching is performed on
155     binary files, but the only output is "Binary file <name>
156     matches" when a match succeeds. If the word is "text", which
157     is equivalent to the -a or --text option, binary files are
158     processed in the same way as any other file. In this case,
159     when a match succeeds, the output may be binary garbage,
160     which can have nasty effects if sent to a terminal. If the
161     word is "without-match", which is equivalent to the -I
162     option, binary files are not processed at all; they are
163 ph10 954 assumed not to be of interest.
164    
165 ph10 654 --buffer-size=number
166 ph10 1470 Set the parameter that controls how much memory is used for
167 ph10 654 buffering files that are being scanned.
168    
169 nigel 87 -C number, --context=number
170 ph10 1470 Output number lines of context both before and after each
171     matching line. This is equivalent to setting both -A and -B
172 nigel 77 to the same value.
173    
174 nigel 87 -c, --count
175 ph10 1470 Do not output individual lines from the files that are being
176 ph10 429 scanned; instead output the number of lines that would other-
177 ph10 1470 wise have been shown. If no lines are selected, the number
178     zero is output. If several files are are being scanned, a
179     count is output for each of them. However, if the --files-
180     with-matches option is also used, only those files whose
181 ph10 429 counts are greater than zero are listed. When -c is used, the
182     -A, -B, and -C options are ignored.
183 nigel 49
184 nigel 87 --colour, --color
185     If this option is given without any data, it is equivalent to
186 ph10 1470 "--colour=auto". If data is required, it must be given in
187 nigel 87 the same shell item, separated by an equals sign.
188    
189     --colour=value, --color=value
190 ph10 392 This option specifies under what circumstances the parts of a
191 nigel 87 line that matched a pattern should be coloured in the output.
192 ph10 1470 By default, the output is not coloured. The value (which is
193     optional, see above) may be "never", "always", or "auto". In
194     the latter case, colouring happens only if the standard out-
195     put is connected to a terminal. More resources are used when
196     colouring is enabled, because pcregrep has to search for all
197     possible matches in a line, not just one, in order to colour
198 ph10 392 them all.
199 nigel 87
200 ph10 392 The colour that is used can be specified by setting the envi-
201     ronment variable PCREGREP_COLOUR or PCREGREP_COLOR. The value
202     of this variable should be a string of two numbers, separated
203 ph10 1470 by a semicolon. They are copied directly into the control
204     string for setting colour on a terminal, so it is your
205     responsibility to ensure that they make sense. If neither of
206     the environment variables is set, the default is "1;31",
207 ph10 392 which gives red.
208    
209 nigel 87 -D action, --devices=action
210 ph10 1470 If an input path is not a regular file or a directory,
211     "action" specifies how it is to be processed. Valid values
212 ph10 392 are "read" (the default) or "skip" (silently skip the path).
213 nigel 87
214     -d action, --directories=action
215     If an input path is a directory, "action" specifies how it is
216 ph10 1470 to be processed. Valid values are "read" (the default in
217     non-Windows environments, for compatibility with GNU grep),
218     "recurse" (equivalent to the -r option), or "skip" (silently
219     skip the path, the default in Windows environments). In the
220     "read" case, directories are read as if they were ordinary
221     files. In some operating systems the effect of reading a
222 ph10 1194 directory like this is an immediate end-of-file; in others it
223     may provoke an error.
224 nigel 87
225 ph10 286 -e pattern, --regex=pattern, --regexp=pattern
226     Specify a pattern to be matched. This option can be used mul-
227     tiple times in order to specify several patterns. It can also
228 ph10 1470 be used as a way of specifying a single pattern that starts
229     with a hyphen. When -e is used, no argument pattern is taken
230     from the command line; all arguments are treated as file
231     names. There is no limit to the number of patterns. They are
232     applied to each line in the order in which they are defined
233 ph10 1194 until one matches.
234 nigel 87
235 ph10 1470 If -f is used with -e, the command line patterns are matched
236 ph10 1194 first, followed by the patterns from the file(s), independent
237 ph10 1470 of the order in which these options are specified. Note that
238     multiple use of -e is not the same as a single pattern with
239 ph10 1194 alternatives. For example, X|Y finds the first character in a
240 ph10 1470 line that is X or Y, whereas if the two patterns are given
241     separately, with X first, pcregrep finds X if it is present,
242 ph10 1194 even if it follows Y in the line. It finds Y only if there is
243 ph10 1470 no X in the line. This matters only if you are using -o or
244 ph10 1194 --colo(u)r to show the part(s) of the line that matched.
245    
246 nigel 77 --exclude=pattern
247 ph10 1194 Files (but not directories) whose names match the pattern are
248 ph10 1470 skipped without being processed. This applies to all files,
249     whether listed on the command line, obtained from --file-
250 ph10 1194 list, or by scanning a directory. The pattern is a PCRE regu-
251     lar expression, and is matched against the final component of
252 ph10 1470 the file name, not the entire path. The -F, -w, and -x
253 ph10 1194 options do not apply to this pattern. The option may be given
254     any number of times in order to specify multiple patterns. If
255 ph10 1470 a file name matches both an --include and an --exclude pat-
256 ph10 1194 tern, it is excluded. There is no short form for this option.
257 nigel 77
258 ph10 1194 --exclude-from=filename
259 ph10 1470 Treat each non-empty line of the file as the data for an
260 ph10 1194 --exclude option. What constitutes a newline when reading the
261 ph10 1470 file is the operating system's default. The --newline option
262     has no effect on this option. This option may be given more
263 ph10 1194 than once in order to specify a number of files to read.
264    
265 ph10 572 --exclude-dir=pattern
266 ph10 1194 Directories whose names match the pattern are skipped without
267 ph10 1470 being processed, whatever the setting of the --recursive
268     option. This applies to all directories, whether listed on
269 ph10 1194 the command line, obtained from --file-list, or by scanning a
270 ph10 1470 parent directory. The pattern is a PCRE regular expression,
271     and is matched against the final component of the directory
272     name, not the entire path. The -F, -w, and -x options do not
273     apply to this pattern. The option may be given any number of
274     times in order to specify more than one pattern. If a direc-
275     tory matches both --include-dir and --exclude-dir, it is
276 ph10 1194 excluded. There is no short form for this option.
277 ph10 345
278 nigel 87 -F, --fixed-strings
279 ph10 1470 Interpret each data-matching pattern as a list of fixed
280     strings, separated by newlines, instead of as a regular
281     expression. What constitutes a newline for this purpose is
282     controlled by the --newline option. The -w (match as a word)
283     and -x (match whole line) options can be used with -F. They
284 ph10 1194 apply to each of the fixed strings. A line is selected if any
285     of the fixed strings are found in it (subject to -w or -x, if
286 ph10 1470 present). This option applies only to the patterns that are
287     matched against the contents of files; it does not apply to
288     patterns specified by any of the --include or --exclude
289 ph10 1194 options.
290 nigel 87
291     -f filename, --file=filename
292 ph10 1470 Read patterns from the file, one per line, and match them
293     against each line of input. What constitutes a newline when
294     reading the file is the operating system's default. The
295 ph10 1194 --newline option has no effect on this option. Trailing white
296     space is removed from each line, and blank lines are ignored.
297 ph10 1470 An empty file contains no patterns and therefore matches
298 ph10 1194 nothing. See also the comments about multiple patterns versus
299 ph10 1470 a single pattern with alternatives in the description of -e
300 ph10 1194 above.
301 nigel 53
302 ph10 1470 If this option is given more than once, all the specified
303     files are read. A data line is output if any of the patterns
304     match it. A filename can be given as "-" to refer to the
305     standard input. When -f is used, patterns specified on the
306     command line using -e may also be present; they are tested
307     before the file's patterns. However, no other pattern is
308 ph10 1194 taken from the command line; all arguments are treated as the
309     names of paths to be searched.
310    
311 ph10 954 --file-list=filename
312 ph10 1470 Read a list of files and/or directories that are to be
313     scanned from the given file, one per line. Trailing white
314 ph10 1194 space is removed from each line, and blank lines are ignored.
315 ph10 1470 These paths are processed before any that are listed on the
316     command line. The filename can be given as "-" to refer to
317 ph10 1194 the standard input. If --file and --file-list are both spec-
318 ph10 1470 ified as "-", patterns are read first. This is useful only
319     when the standard input is a terminal, from which further
320     lines (the list of files) can be read after an end-of-file
321     indication. If this option is given more than once, all the
322 ph10 1194 specified files are read.
323 ph10 954
324 ph10 286 --file-offsets
325 ph10 1470 Instead of showing lines or parts of lines that match, show
326     each match as an offset from the start of the file and a
327     length, separated by a comma. In this mode, no context is
328     shown. That is, the -A, -B, and -C options are ignored. If
329 ph10 286 there is more than one match in a line, each of them is shown
330 ph10 1470 separately. This option is mutually exclusive with --line-
331 ph10 286 offsets and --only-matching.
332    
333 nigel 87 -H, --with-filename
334 ph10 1470 Force the inclusion of the filename at the start of output
335     lines when searching a single file. By default, the filename
336     is not shown in this case. For matching lines, the filename
337 ph10 392 is followed by a colon; for context lines, a hyphen separator
338 ph10 1470 is used. If a line number is also being output, it follows
339 ph10 392 the file name.
340 nigel 49
341 nigel 87 -h, --no-filename
342 ph10 1470 Suppress the output filenames when searching multiple files.
343     By default, filenames are shown when multiple files are
344     searched. For matching lines, the filename is followed by a
345     colon; for context lines, a hyphen separator is used. If a
346 ph10 392 line number is also being output, it follows the file name.
347 nigel 49
348 ph10 1470 --help Output a help message, giving brief details of the command
349     options and file type support, and then exit. Anything else
350 ph10 1194 on the command line is ignored.
351 nigel 87
352 ph10 1470 -I Treat binary files as never matching. This is equivalent to
353 ph10 954 --binary-files=without-match.
354    
355 nigel 87 -i, --ignore-case
356     Ignore upper/lower case distinctions during comparisons.
357    
358 nigel 77 --include=pattern
359 ph10 1470 If any --include patterns are specified, the only files that
360     are processed are those that match one of the patterns (and
361     do not match an --exclude pattern). This option does not
362     affect directories, but it applies to all files, whether
363     listed on the command line, obtained from --file-list, or by
364     scanning a directory. The pattern is a PCRE regular expres-
365     sion, and is matched against the final component of the file
366     name, not the entire path. The -F, -w, and -x options do not
367     apply to this pattern. The option may be given any number of
368     times. If a file name matches both an --include and an
369     --exclude pattern, it is excluded. There is no short form
370 ph10 1194 for this option.
371 nigel 49
372 ph10 1194 --include-from=filename
373 ph10 1470 Treat each non-empty line of the file as the data for an
374 ph10 1194 --include option. What constitutes a newline for this purpose
375 ph10 1470 is the operating system's default. The --newline option has
376 ph10 1194 no effect on this option. This option may be given any number
377     of times; all the files are read.
378    
379 ph10 572 --include-dir=pattern
380 ph10 1470 If any --include-dir patterns are specified, the only direc-
381     tories that are processed are those that match one of the
382     patterns (and do not match an --exclude-dir pattern). This
383     applies to all directories, whether listed on the command
384     line, obtained from --file-list, or by scanning a parent
385     directory. The pattern is a PCRE regular expression, and is
386     matched against the final component of the directory name,
387     not the entire path. The -F, -w, and -x options do not apply
388 ph10 1194 to this pattern. The option may be given any number of times.
389 ph10 1470 If a directory matches both --include-dir and --exclude-dir,
390 ph10 1194 it is excluded. There is no short form for this option.
391 ph10 345
392 nigel 87 -L, --files-without-match
393 ph10 1470 Instead of outputting lines from the files, just output the
394     names of the files that do not contain any lines that would
395     have been output. Each file name is output once, on a sepa-
396 nigel 77 rate line.
397    
398 nigel 87 -l, --files-with-matches
399 ph10 1470 Instead of outputting lines from the files, just output the
400 nigel 87 names of the files containing lines that would have been out-
401 ph10 1470 put. Each file name is output once, on a separate line.
402     Searching normally stops as soon as a matching line is found
403     in a file. However, if the -c (count) option is also used,
404     matching continues in order to obtain the correct count, and
405     those files that have at least one match are listed along
406 ph10 429 with their counts. Using this option with -c is a way of sup-
407     pressing the listing of files with no matches.
408 nigel 77
409     --label=name
410     This option supplies a name to be used for the standard input
411 nigel 87 when file names are being output. If not supplied, "(standard
412     input)" is used. There is no short form for this option.
413 nigel 77
414 ph10 535 --line-buffered
415 ph10 1470 When this option is given, input is read and processed line
416     by line, and the output is flushed after each write. By
417     default, input is read in large chunks, unless pcregrep can
418     determine that it is reading from a terminal (which is cur-
419     rently possible only in Unix-like environments). Output to
420     terminal is normally automatically flushed by the operating
421 ph10 1194 system. This option can be useful when the input or output is
422 ph10 1470 attached to a pipe and you do not want pcregrep to buffer up
423     large amounts of data. However, its use will affect perfor-
424 ph10 535 mance, and the -M (multiline) option ceases to work.
425    
426 ph10 286 --line-offsets
427 ph10 1470 Instead of showing lines or parts of lines that match, show
428 ph10 286 each match as a line number, the offset from the start of the
429 ph10 1470 line, and a length. The line number is terminated by a colon
430     (as usual; see the -n option), and the offset and length are
431     separated by a comma. In this mode, no context is shown.
432     That is, the -A, -B, and -C options are ignored. If there is
433     more than one match in a line, each of them is shown sepa-
434 ph10 286 rately. This option is mutually exclusive with --file-offsets
435     and --only-matching.
436    
437 nigel 87 --locale=locale-name
438 ph10 1470 This option specifies a locale to be used for pattern match-
439     ing. It overrides the value in the LC_ALL or LC_CTYPE envi-
440     ronment variables. If no locale is specified, the PCRE
441     library's default (usually the "C" locale) is used. There is
442 nigel 87 no short form for this option.
443    
444 ph10 567 --match-limit=number
445 ph10 1470 Processing some regular expression patterns can require a
446     very large amount of memory, leading in some cases to a pro-
447     gram crash if not enough is available. Other patterns may
448     take a very long time to search for all possible matching
449     strings. The pcre_exec() function that is called by pcregrep
450     to do the matching has two parameters that can limit the
451 ph10 567 resources that it uses.
452    
453 ph10 1470 The --match-limit option provides a means of limiting
454 ph10 567 resource usage when processing patterns that are not going to
455     match, but which have a very large number of possibilities in
456 ph10 1470 their search trees. The classic example is a pattern that
457     uses nested unlimited repeats. Internally, PCRE uses a func-
458     tion called match() which it calls repeatedly (sometimes
459     recursively). The limit set by --match-limit is imposed on
460     the number of times this function is called during a match,
461     which has the effect of limiting the amount of backtracking
462 ph10 567 that can take place.
463    
464     The --recursion-limit option is similar to --match-limit, but
465     instead of limiting the total number of times that match() is
466     called, it limits the depth of recursive calls, which in turn
467 ph10 1470 limits the amount of memory that can be used. The recursion
468     depth is a smaller number than the total number of calls,
469 ph10 567 because not all calls to match() are recursive. This limit is
470     of use only if it is set smaller than --match-limit.
471    
472 ph10 1470 There are no short forms for these options. The default set-
473     tings are specified when the PCRE library is compiled, with
474 ph10 567 the default default being 10 million.
475    
476 nigel 87 -M, --multiline
477 ph10 1470 Allow patterns to match more than one line. When this option
478 nigel 77 is given, patterns may usefully contain literal newline char-
479 ph10 1470 acters and internal occurrences of ^ and $ characters. The
480     output for a successful match may consist of more than one
481     line, the last of which is the one in which the match ended.
482 ph10 589 If the matched string ends with a newline sequence the output
483     ends at the end of that line.
484    
485 ph10 1470 When this option is set, the PCRE library is called in "mul-
486     tiline" mode. There is a limit to the number of lines that
487     can be matched, imposed by the way that pcregrep buffers the
488     input file as it scans it. However, pcregrep ensures that at
489 nigel 77 least 8K characters or the rest of the document (whichever is
490 ph10 1470 the shorter) are available for forward matching, and simi-
491 nigel 77 larly the previous 8K characters (or all the previous charac-
492 ph10 1470 ters, if fewer than 8K) are guaranteed to be available for
493     lookbehind assertions. This option does not work when input
494 ph10 535 is read line by line (see --line-buffered.)
495 nigel 77
496 nigel 91 -N newline-type, --newline=newline-type
497 ph10 1470 The PCRE library supports five different conventions for
498     indicating the ends of lines. They are the single-character
499     sequences CR (carriage return) and LF (linefeed), the two-
500     character sequence CRLF, an "anycrlf" convention, which rec-
501     ognizes any of the preceding three types, and an "any" con-
502 ph10 150 vention, in which any Unicode line ending sequence is assumed
503 ph10 1470 to end a line. The Unicode sequences are the three just men-
504     tioned, plus VT (vertical tab, U+000B), FF (form feed,
505     U+000C), NEL (next line, U+0085), LS (line separator,
506 ph10 150 U+2028), and PS (paragraph separator, U+2029).
507 nigel 91
508 nigel 93 When the PCRE library is built, a default line-ending
509 ph10 1470 sequence is specified. This is normally the standard
510 nigel 93 sequence for the operating system. Unless otherwise specified
511 ph10 1470 by this option, pcregrep uses the library's default. The
512 ph10 150 possible values for this option are CR, LF, CRLF, ANYCRLF, or
513 ph10 1470 ANY. This makes it possible to use pcregrep to scan files
514 ph10 1194 that have come from other environments without having to mod-
515 ph10 1470 ify their line endings. If the data that is being scanned
516     does not agree with the convention set by this option, pcre-
517     grep may behave in strange ways. Note that this option does
518     not apply to files specified by the -f, --exclude-from, or
519 ph10 1194 --include-from options, which are expected to use the operat-
520     ing system's standard newline sequence.
521 nigel 93
522 nigel 87 -n, --line-number
523     Precede each output line by its line number in the file, fol-
524 ph10 1470 lowed by a colon for matching lines or a hyphen for context
525     lines. If the filename is also being output, it precedes the
526 ph10 392 line number. This option is forced if --line-offsets is used.
527 nigel 49
528 ph10 1470 --no-jit If the PCRE library is built with support for just-in-time
529     compiling (which speeds up matching), pcregrep automatically
530 ph10 691 makes use of this, unless it was explicitly disabled at build
531 ph10 1470 time. This option can be used to disable the use of JIT at
532     run time. It is provided for testing and working round prob-
533 ph10 691 lems. It should never be needed in normal use.
534    
535 nigel 87 -o, --only-matching
536 ph10 567 Show only the part of the line that matched a pattern instead
537 ph10 1470 of the whole line. In this mode, no context is shown. That
538     is, the -A, -B, and -C options are ignored. If there is more
539     than one match in a line, each of them is shown separately.
540     If -o is combined with -v (invert the sense of the match to
541     find non-matching lines), no output is generated, but the
542     return code is set appropriately. If the matched portion of
543     the line is empty, nothing is output unless the file name or
544     line number are being printed, in which case they are shown
545 ph10 567 on an otherwise empty line. This option is mutually exclusive
546     with --file-offsets and --line-offsets.
547 nigel 87
548 ph10 567 -onumber, --only-matching=number
549 ph10 1470 Show only the part of the line that matched the capturing
550 ph10 567 parentheses of the given number. Up to 32 capturing parenthe-
551 ph10 1194 ses are supported, and -o0 is equivalent to -o without a num-
552 ph10 1470 ber. Because these options can be given without an argument
553     (see above), if an argument is present, it must be given in
554     the same shell item, for example, -o3 or --only-matching=2.
555 ph10 1194 The comments given for the non-argument case above also apply
556 ph10 1470 to this case. If the specified capturing parentheses do not
557     exist in the pattern, or were not set in the match, nothing
558     is output unless the file name or line number are being
559 ph10 1194 printed.
560 ph10 567
561 ph10 1470 If this option is given multiple times, multiple substrings
562     are output, in the order the options are given. For example,
563 ph10 1194 -o3 -o1 -o3 causes the substrings matched by capturing paren-
564 ph10 1470 theses 3 and 1 and then 3 again to be output. By default,
565 ph10 1194 there is no separator (but see the next option).
566    
567     --om-separator=text
568 ph10 1470 Specify a separating string for multiple occurrences of -o.
569     The default is an empty string. Separating strings are never
570 ph10 1194 coloured.
571    
572 nigel 87 -q, --quiet
573     Work quietly, that is, display nothing except error messages.
574 ph10 1470 The exit status indicates whether or not any matches were
575 nigel 73 found.
576 nigel 49
577 nigel 87 -r, --recursive
578 ph10 1470 If any given path is a directory, recursively scan the files
579     it contains, taking note of any --include and --exclude set-
580     tings. By default, a directory is read as a normal file; in
581     some operating systems this gives an immediate end-of-file.
582     This option is a shorthand for setting the -d option to
583 nigel 87 "recurse".
584 nigel 77
585 ph10 567 --recursion-limit=number
586     See --match-limit above.
587    
588 nigel 87 -s, --no-messages
589 ph10 1470 Suppress error messages about non-existent or unreadable
590     files. Such files are quietly skipped. However, the return
591 nigel 77 code is still 2, even if matches were found in other files.
592    
593 nigel 87 -u, --utf-8
594 ph10 1470 Operate in UTF-8 mode. This option is available only if PCRE
595 ph10 1194 has been compiled with UTF-8 support. All patterns (including
596 ph10 1470 those for any --exclude and --include options) and all sub-
597     ject lines that are scanned must be valid strings of UTF-8
598 ph10 1194 characters.
599 nigel 63
600 nigel 87 -V, --version
601 ph10 1194 Write the version numbers of pcregrep and the PCRE library to
602 ph10 1470 the standard output and then exit. Anything else on the com-
603 ph10 1194 mand line is ignored.
604 nigel 49
605 nigel 87 -v, --invert-match
606 ph10 1470 Invert the sense of the match, so that lines which do not
607 nigel 87 match any of the patterns are the ones that are found.
608 nigel 77
609 nigel 87 -w, --word-regex, --word-regexp
610     Force the patterns to match only whole words. This is equiva-
611 ph10 1470 lent to having \b at the start and end of the pattern. This
612     option applies only to the patterns that are matched against
613     the contents of files; it does not apply to patterns speci-
614 ph10 1194 fied by any of the --include or --exclude options.
615 nigel 77
616 nigel 87 -x, --line-regex, --line-regexp
617 ph10 1470 Force the patterns to be anchored (each must start matching
618     at the beginning of a line) and in addition, require them to
619     match entire lines. This is equivalent to having ^ and $
620 nigel 73 characters at the start and end of each alternative branch in
621 ph10 1470 every pattern. This option applies only to the patterns that
622     are matched against the contents of files; it does not apply
623     to patterns specified by any of the --include or --exclude
624 ph10 1194 options.
625 nigel 49
626    
627 nigel 87 ENVIRONMENT VARIABLES
628 nigel 49
629 ph10 1470 The environment variables LC_ALL and LC_CTYPE are examined, in that
630     order, for a locale. The first one that is set is used. This can be
631     overridden by the --locale option. If no locale is set, the PCRE
632 nigel 87 library's default (usually the "C" locale) is used.
633 nigel 49
634    
635 nigel 91 NEWLINES
636    
637 ph10 1470 The -N (--newline) option allows pcregrep to scan files with different
638 ph10 1194 newline conventions from the default. Any parts of the input files that
639 ph10 1470 are written to the standard output are copied identically, with what-
640     ever newline sequences they have in the input. However, the setting of
641     this option does not affect the interpretation of files specified by
642 ph10 1194 the -f, --exclude-from, or --include-from options, which are assumed to
643 ph10 1470 use the operating system's standard newline sequence, nor does it
644     affect the way in which pcregrep writes informational messages to the
645 ph10 1194 standard error and output streams. For these it uses the string "\n" to
646 ph10 1470 indicate newlines, relying on the C I/O library to convert this to an
647 ph10 1194 appropriate sequence.
648 nigel 91
649    
650 nigel 87 OPTIONS COMPATIBILITY
651 nigel 49
652 ph10 1470 Many of the short and long forms of pcregrep's options are the same as
653     in the GNU grep program. Any long option of the form --xxx-regexp (GNU
654     terminology) is also available as --xxx-regex (PCRE terminology). How-
655     ever, the --file-list, --file-offsets, --include-dir, --line-offsets,
656     --locale, --match-limit, -M, --multiline, -N, --newline, --om-separa-
657     tor, --recursion-limit, -u, and --utf-8 options are specific to pcre-
658     grep, as is the use of the --only-matching option with a capturing
659 ph10 1194 parentheses number.
660 nigel 87
661 ph10 1470 Although most of the common options work the same way, a few are dif-
662     ferent in pcregrep. For example, the --include option's argument is a
663     glob for GNU grep, but a regular expression for pcregrep. If both the
664     -c and -l options are given, GNU grep lists only file names, without
665 ph10 572 counts, but pcregrep gives the counts.
666 nigel 87
667 ph10 572
668 nigel 77 OPTIONS WITH DATA
669 nigel 49
670 nigel 77 There are four different ways in which an option with data can be spec-
671 ph10 1470 ified. If a short form option is used, the data may follow immedi-
672 ph10 572 ately, or (with one exception) in the next command line item. For exam-
673     ple:
674 nigel 77
675     -f/some/file
676     -f /some/file
677    
678 ph10 1470 The exception is the -o option, which may appear with or without data.
679     Because of this, if data is present, it must follow immediately in the
680 ph10 572 same item, for example -o3.
681    
682 ph10 1470 If a long form option is used, the data may appear in the same command
683     line item, separated by an equals character, or (with two exceptions)
684 ph10 572 it may appear in the next command line item. For example:
685 nigel 77
686     --file=/some/file
687     --file /some/file
688    
689 ph10 1470 Note, however, that if you want to supply a file name beginning with ~
690     as data in a shell command, and have the shell expand ~ to a home
691 nigel 87 directory, you must separate the file name from the option, because the
692 ph10 392 shell does not treat ~ specially unless it is at the start of an item.
693 nigel 77
694 ph10 1470 The exceptions to the above are the --colour (or --color) and --only-
695     matching options, for which the data is optional. If one of these
696     options does have data, it must be given in the first form, using an
697 ph10 579 equals character. Otherwise pcregrep will assume that it has no data.
698 nigel 87
699    
700     MATCHING ERRORS
701    
702 ph10 1470 It is possible to supply a regular expression that takes a very long
703     time to fail to match certain lines. Such patterns normally involve
704     nested indefinite repeats, for example: (a+)*\d when matched against a
705     line of a's with no final digit. The PCRE matching function has a
706     resource limit that causes it to abort in these circumstances. If this
707 nigel 87 happens, pcregrep outputs an error message and the line that caused the
708 ph10 1470 problem to the standard error stream. If there are more than 20 such
709 nigel 87 errors, pcregrep gives up.
710    
711 ph10 1470 The --match-limit option of pcregrep can be used to set the overall
712     resource limit; there is a second option called --recursion-limit that
713     sets a limit on the amount of memory (usually stack) that is used (see
714 ph10 572 the discussion of these options above).
715 nigel 87
716 ph10 572
717 nigel 63 DIAGNOSTICS
718 nigel 49
719 nigel 73 Exit status is 0 if any matches were found, 1 if no matches were found,
720 ph10 1470 and 2 for syntax errors, overlong lines, non-existent or inaccessible
721     files (even if matches were found in other files) or too many matching
722 ph10 654 errors. Using the -s option to suppress error messages about inaccessi-
723     ble files does not affect the return code.
724 nigel 49
725    
726 nigel 93 SEE ALSO
727    
728 ph10 1194 pcrepattern(3), pcresyntax(3), pcretest(1).
729 nigel 93
730    
731 nigel 49 AUTHOR
732 nigel 63
733 nigel 77 Philip Hazel
734 nigel 73 University Computing Service
735 nigel 93 Cambridge CB2 3QH, England.
736 nigel 49
737 ph10 99
738     REVISION
739    
740 ph10 1470 Last updated: 03 April 2014
741     Copyright (c) 1997-2014 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