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