PageRenderTime 33ms CodeModel.GetById 9ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

HTML | 516 lines | 516 code | 0 blank | 0 comment | 0 complexity | 47e90d348ca2bb69b20c49c10b3ce499 MD5 | raw file
  3<title>pcregrep specification</title>
  5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6<h1>pcregrep man page</h1>
  8Return to the <a href="index.html">PCRE index page</a>.
 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.
 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">OPTIONS</a>
 20<li><a name="TOC5" href="#SEC5">ENVIRONMENT VARIABLES</a>
 21<li><a name="TOC6" href="#SEC6">NEWLINES</a>
 22<li><a name="TOC7" href="#SEC7">OPTIONS COMPATIBILITY</a>
 23<li><a name="TOC8" href="#SEC8">OPTIONS WITH DATA</a>
 24<li><a name="TOC9" href="#SEC9">MATCHING ERRORS</a>
 25<li><a name="TOC10" href="#SEC10">DIAGNOSTICS</a>
 26<li><a name="TOC11" href="#SEC11">SEE ALSO</a>
 27<li><a name="TOC12" href="#SEC12">AUTHOR</a>
 28<li><a name="TOC13" href="#SEC13">REVISION</a>
 30<br><a name="SEC1" href="#TOC1">SYNOPSIS</a><br>
 32<b>pcregrep [options] [long options] [pattern] [path1 path2 ...]</b>
 34<br><a name="SEC2" href="#TOC1">DESCRIPTION</a><br>
 36<b>pcregrep</b> searches files for character patterns, in the same way as other
 37grep commands do, but it uses the PCRE regular expression library to support
 38patterns that are compatible with the regular expressions of Perl 5. See
 39<a href="pcrepattern.html"><b>pcrepattern</b>(3)</a>
 40for a full description of syntax and semantics of the regular expressions
 41that PCRE supports.
 44Patterns, whether supplied on the command line or in a separate file, are given
 45without delimiters. For example:
 47  pcregrep Thursday /etc/motd
 49If you attempt to use delimiters (for example, by surrounding a pattern with
 50slashes, as is common in Perl scripts), they are interpreted as part of the
 51pattern. Quotes can of course be used to delimit patterns on the command line
 52because they are interpreted by the shell, and indeed they are required if a
 53pattern contains white space or shell metacharacters.
 56The first argument that follows any option settings is treated as the single
 57pattern to be matched when neither <b>-e</b> nor <b>-f</b> is present.
 58Conversely, when one or both of these options are used to specify patterns, all
 59arguments are treated as path names. At least one of <b>-e</b>, <b>-f</b>, or an
 60argument pattern must be provided.
 63If no files are specified, <b>pcregrep</b> reads the standard input. The
 64standard input can also be referenced by a name consisting of a single hyphen.
 65For example:
 67  pcregrep some-pattern /file1 - /file3
 69By default, each line that matches a pattern is copied to the standard
 70output, and if there is more than one file, the file name is output at the
 71start of each line, followed by a colon. However, there are options that can
 72change how <b>pcregrep</b> behaves. In particular, the <b>-M</b> option makes it
 73possible to search for patterns that span line boundaries. What defines a line
 74boundary is controlled by the <b>-N</b> (<b>--newline</b>) option.
 77Patterns are limited to 8K or BUFSIZ characters, whichever is the greater.
 78BUFSIZ is defined in <b>&#60;stdio.h&#62;</b>. When there is more than one pattern
 79(specified by the use of <b>-e</b> and/or <b>-f</b>), each pattern is applied to
 80each line in the order in which they are defined, except that all the <b>-e</b>
 81patterns are tried before the <b>-f</b> patterns. As soon as one pattern matches
 82(or fails to match when <b>-v</b> is used), no further patterns are considered.
 85When <b>--only-matching</b>, <b>--file-offsets</b>, or <b>--line-offsets</b>
 86is used, the output is the part of the line that matched (either shown
 87literally, or as an offset). In this case, scanning resumes immediately
 88following the match, so that further matches on the same line can be found.
 89If there are multiple patterns, they are all tried on the remainder of the
 90line. However, patterns that follow the one that matched are not tried on the
 91earlier part of the line.
 94If the <b>LC_ALL</b> or <b>LC_CTYPE</b> environment variable is set,
 95<b>pcregrep</b> uses the value to set a locale when calling the PCRE library.
 96The <b>--locale</b> option can be used to override this.
 98<br><a name="SEC3" href="#TOC1">SUPPORT FOR COMPRESSED FILES</a><br>
100It is possible to compile <b>pcregrep</b> so that it uses <b>libz</b> or
101<b>libbz2</b> to read files whose names end in <b>.gz</b> or <b>.bz2</b>,
102respectively. You can find out whether your binary has support for one or both
103of these file types by running it with the <b>--help</b> option. If the
104appropriate support is not present, files are treated as plain text. The
105standard input is always so treated.
107<br><a name="SEC4" href="#TOC1">OPTIONS</a><br>
110This terminate the list of options. It is useful if the next item on the
111command line starts with a hyphen but is not an option. This allows for the
112processing of patterns and filenames that start with hyphens.
115<b>-A</b> <i>number</i>, <b>--after-context=</b><i>number</i>
116Output <i>number</i> lines of context after each matching line. If filenames
117and/or line numbers are being output, a hyphen separator is used instead of a
118colon for the context lines. A line containing "--" is output between each
119group of lines, unless they are in fact contiguous in the input file. The value
120of <i>number</i> is expected to be relatively small. However, <b>pcregrep</b>
121guarantees to have up to 8K of following text available for context output.
124<b>-B</b> <i>number</i>, <b>--before-context=</b><i>number</i>
125Output <i>number</i> lines of context before each matching line. If filenames
126and/or line numbers are being output, a hyphen separator is used instead of a
127colon for the context lines. A line containing "--" is output between each
128group of lines, unless they are in fact contiguous in the input file. The value
129of <i>number</i> is expected to be relatively small. However, <b>pcregrep</b>
130guarantees to have up to 8K of preceding text available for context output.
133<b>-C</b> <i>number</i>, <b>--context=</b><i>number</i>
134Output <i>number</i> lines of context both before and after each matching line.
135This is equivalent to setting both <b>-A</b> and <b>-B</b> to the same value.
138<b>-c</b>, <b>--count</b>
139Do not output individual lines; instead just output a count of the number of
140lines that would otherwise have been output. If several files are given, a
141count is output for each of them. In this mode, the <b>-A</b>, <b>-B</b>, and
142<b>-C</b> options are ignored.
145<b>--colour</b>, <b>--color</b>
146If this option is given without any data, it is equivalent to "--colour=auto".
147If data is required, it must be given in the same shell item, separated by an
148equals sign.
151<b>--colour=</b><i>value</i>, <b>--color=</b><i>value</i>
152This option specifies under what circumstances the part of a line that matched
153a pattern should be coloured in the output. The value may be "never" (the
154default), "always", or "auto". In the latter case, colouring happens only if
155the standard output is connected to a terminal. The colour can be specified by
156setting the environment variable PCREGREP_COLOUR or PCREGREP_COLOR. The value
157of this variable should be a string of two numbers, separated by a semicolon.
158They are copied directly into the control string for setting colour on a
159terminal, so it is your responsibility to ensure that they make sense. If
160neither of the environment variables is set, the default is "1;31", which gives
164<b>-D</b> <i>action</i>, <b>--devices=</b><i>action</i>
165If an input path is not a regular file or a directory, "action" specifies how
166it is to be processed. Valid values are "read" (the default) or "skip"
167(silently skip the path).
170<b>-d</b> <i>action</i>, <b>--directories=</b><i>action</i>
171If an input path is a directory, "action" specifies how it is to be processed.
172Valid values are "read" (the default), "recurse" (equivalent to the <b>-r</b>
173option), or "skip" (silently skip the path). In the default case, directories
174are read as if they were ordinary files. In some operating systems the effect
175of reading a directory like this is an immediate end-of-file.
178<b>-e</b> <i>pattern</i>, <b>--regex=</b><i>pattern</i>, <b>--regexp=</b><i>pattern</i>
179Specify a pattern to be matched. This option can be used multiple times in
180order to specify several patterns. It can also be used as a way of specifying a
181single pattern that starts with a hyphen. When <b>-e</b> is used, no argument
182pattern is taken from the command line; all arguments are treated as file
183names. There is an overall maximum of 100 patterns. They are applied to each
184line in the order in which they are defined until one matches (or fails to
185match if <b>-v</b> is used). If <b>-f</b> is used with <b>-e</b>, the command line
186patterns are matched first, followed by the patterns from the file, independent
187of the order in which these options are specified. Note that multiple use of
188<b>-e</b> is not the same as a single pattern with alternatives. For example,
189X|Y finds the first character in a line that is X or Y, whereas if the two
190patterns are given separately, <b>pcregrep</b> finds X if it is present, even if
191it follows Y in the line. It finds Y only if there is no X in the line. This
192really matters only if you are using <b>-o</b> to show the part(s) of the line
193that matched.
197When <b>pcregrep</b> is searching the files in a directory as a consequence of
198the <b>-r</b> (recursive search) option, any regular files whose names match the
199pattern are excluded. Subdirectories are not excluded by this option; they are
200searched recursively, subject to the <b>--exclude_dir</b> and
201<b>--include_dir</b> options. The pattern is a PCRE regular expression, and is
202matched against the final component of the file name (not the entire path). If
203a file name matches both <b>--include</b> and <b>--exclude</b>, it is excluded.
204There is no short form for this option.
208When <b>pcregrep</b> is searching the contents of a directory as a consequence
209of the <b>-r</b> (recursive search) option, any subdirectories whose names match
210the pattern are excluded. (Note that the \fP--exclude\fP option does not affect
211subdirectories.) The pattern is a PCRE regular expression, and is matched
212against the final component of the name (not the entire path). If a
213subdirectory name matches both <b>--include_dir</b> and <b>--exclude_dir</b>, it
214is excluded. There is no short form for this option.
217<b>-F</b>, <b>--fixed-strings</b>
218Interpret each pattern as a list of fixed strings, separated by newlines,
219instead of as a regular expression. The <b>-w</b> (match as a word) and <b>-x</b>
220(match whole line) options can be used with <b>-F</b>. They apply to each of the
221fixed strings. A line is selected if any of the fixed strings are found in it
222(subject to <b>-w</b> or <b>-x</b>, if present).
225<b>-f</b> <i>filename</i>, <b>--file=</b><i>filename</i>
226Read a number of patterns from the file, one per line, and match them against
227each line of input. A data line is output if any of the patterns match it. The
228filename can be given as "-" to refer to the standard input. When <b>-f</b> is
229used, patterns specified on the command line using <b>-e</b> may also be
230present; they are tested before the file's patterns. However, no other pattern
231is taken from the command line; all arguments are treated as file names. There
232is an overall maximum of 100 patterns. Trailing white space is removed from
233each line, and blank lines are ignored. An empty file contains no patterns and
234therefore matches nothing. See also the comments about multiple patterns versus
235a single pattern with alternatives in the description of <b>-e</b> above.
239Instead of showing lines or parts of lines that match, show each match as an
240offset from the start of the file and a length, separated by a comma. In this
241mode, no context is shown. That is, the <b>-A</b>, <b>-B</b>, and <b>-C</b>
242options are ignored. If there is more than one match in a line, each of them is
243shown separately. This option is mutually exclusive with <b>--line-offsets</b>
244and <b>--only-matching</b>.
247<b>-H</b>, <b>--with-filename</b>
248Force the inclusion of the filename at the start of output lines when searching
249a single file. By default, the filename is not shown in this case. For matching
250lines, the filename is followed by a colon and a space; for context lines, a
251hyphen separator is used. If a line number is also being output, it follows the
252file name without a space.
255<b>-h</b>, <b>--no-filename</b>
256Suppress the output filenames when searching multiple files. By default,
257filenames are shown when multiple files are searched. For matching lines, the
258filename is followed by a colon and a space; for context lines, a hyphen
259separator is used. If a line number is also being output, it follows the file
260name without a space.
264Output a help message, giving brief details of the command options and file
265type support, and then exit.
268<b>-i</b>, <b>--ignore-case</b>
269Ignore upper/lower case distinctions during comparisons.
273When <b>pcregrep</b> is searching the files in a directory as a consequence of
274the <b>-r</b> (recursive search) option, only those regular files whose names
275match the pattern are included. Subdirectories are always included and searched
276recursively, subject to the \fP--include_dir\fP and <b>--exclude_dir</b>
277options. The pattern is a PCRE regular expression, and is matched against the
278final component of the file name (not the entire path). If a file name matches
279both <b>--include</b> and <b>--exclude</b>, it is excluded. There is no short
280form for this option.
284When <b>pcregrep</b> is searching the contents of a directory as a consequence
285of the <b>-r</b> (recursive search) option, only those subdirectories whose
286names match the pattern are included. (Note that the <b>--include</b> option
287does not affect subdirectories.) The pattern is a PCRE regular expression, and
288is matched against the final component of the name (not the entire path). If a
289subdirectory name matches both <b>--include_dir</b> and <b>--exclude_dir</b>, it
290is excluded. There is no short form for this option.
293<b>-L</b>, <b>--files-without-match</b>
294Instead of outputting lines from the files, just output the names of the files
295that do not contain any lines that would have been output. Each file name is
296output once, on a separate line.
299<b>-l</b>, <b>--files-with-matches</b>
300Instead of outputting lines from the files, just output the names of the files
301containing lines that would have been output. Each file name is output
302once, on a separate line. Searching stops as soon as a matching line is found
303in a file.
307This option supplies a name to be used for the standard input when file names
308are being output. If not supplied, "(standard input)" is used. There is no
309short form for this option.
313Instead of showing lines or parts of lines that match, show each match as a
314line number, the offset from the start of the line, and a length. The line
315number is terminated by a colon (as usual; see the <b>-n</b> option), and the
316offset and length are separated by a comma. In this mode, no context is shown.
317That is, the <b>-A</b>, <b>-B</b>, and <b>-C</b> options are ignored. If there is
318more than one match in a line, each of them is shown separately. This option is
319mutually exclusive with <b>--file-offsets</b> and <b>--only-matching</b>.
323This option specifies a locale to be used for pattern matching. It overrides
324the value in the <b>LC_ALL</b> or <b>LC_CTYPE</b> environment variables. If no
325locale is specified, the PCRE library's default (usually the "C" locale) is
326used. There is no short form for this option.
329<b>-M</b>, <b>--multiline</b>
330Allow patterns to match more than one line. When this option is given, patterns
331may usefully contain literal newline characters and internal occurrences of ^
332and $ characters. The output for any one match may consist of more than one
333line. When this option is set, the PCRE library is called in "multiline" mode.
334There is a limit to the number of lines that can be matched, imposed by the way
335that <b>pcregrep</b> buffers the input file as it scans it. However,
336<b>pcregrep</b> ensures that at least 8K characters or the rest of the document
337(whichever is the shorter) are available for forward matching, and similarly
338the previous 8K characters (or all the previous characters, if fewer than 8K)
339are guaranteed to be available for lookbehind assertions.
342<b>-N</b> <i>newline-type</i>, <b>--newline=</b><i>newline-type</i>
343The PCRE library supports five different conventions for indicating
344the ends of lines. They are the single-character sequences CR (carriage return)
345and LF (linefeed), the two-character sequence CRLF, an "anycrlf" convention,
346which recognizes any of the preceding three types, and an "any" convention, in
347which any Unicode line ending sequence is assumed to end a line. The Unicode
348sequences are the three just mentioned, plus VT (vertical tab, U+000B), FF
349(formfeed, U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and
350PS (paragraph separator, U+2029).
353When the PCRE library is built, a default line-ending sequence is specified.
354This is normally the standard sequence for the operating system. Unless
355otherwise specified by this option, <b>pcregrep</b> uses the library's default.
356The possible values for this option are CR, LF, CRLF, ANYCRLF, or ANY. This
357makes it possible to use <b>pcregrep</b> on files that have come from other
358environments without having to modify their line endings. If the data that is
359being scanned does not agree with the convention set by this option,
360<b>pcregrep</b> may behave in strange ways.
363<b>-n</b>, <b>--line-number</b>
364Precede each output line by its line number in the file, followed by a colon
365and a space for matching lines or a hyphen and a space for context lines. If
366the filename is also being output, it precedes the line number. This option is
367forced if <b>--line-offsets</b> is used.
370<b>-o</b>, <b>--only-matching</b>
371Show only the part of the line that matched a pattern. In this mode, no
372context is shown. That is, the <b>-A</b>, <b>-B</b>, and <b>-C</b> options are
373ignored. If there is more than one match in a line, each of them is shown
374separately. If <b>-o</b> is combined with <b>-v</b> (invert the sense of the
375match to find non-matching lines), no output is generated, but the return code
376is set appropriately. This option is mutually exclusive with
377<b>--file-offsets</b> and <b>--line-offsets</b>.
380<b>-q</b>, <b>--quiet</b>
381Work quietly, that is, display nothing except error messages. The exit
382status indicates whether or not any matches were found.
385<b>-r</b>, <b>--recursive</b>
386If any given path is a directory, recursively scan the files it contains,
387taking note of any <b>--include</b> and <b>--exclude</b> settings. By default, a
388directory is read as a normal file; in some operating systems this gives an
389immediate end-of-file. This option is a shorthand for setting the <b>-d</b>
390option to "recurse".
393<b>-s</b>, <b>--no-messages</b>
394Suppress error messages about non-existent or unreadable files. Such files are
395quietly skipped. However, the return code is still 2, even if matches were
396found in other files.
399<b>-u</b>, <b>--utf-8</b>
400Operate in UTF-8 mode. This option is available only if PCRE has been compiled
401with UTF-8 support. Both patterns and subject lines must be valid strings of
402UTF-8 characters.
405<b>-V</b>, <b>--version</b>
406Write the version numbers of <b>pcregrep</b> and the PCRE library that is being
407used to the standard error stream.
410<b>-v</b>, <b>--invert-match</b>
411Invert the sense of the match, so that lines which do <i>not</i> match any of
412the patterns are the ones that are found.
415<b>-w</b>, <b>--word-regex</b>, <b>--word-regexp</b>
416Force the patterns to match only whole words. This is equivalent to having \b
417at the start and end of the pattern.
420<b>-x</b>, <b>--line-regex</b>, <b>--line-regexp</b>
421Force the patterns to be anchored (each must start matching at the beginning of
422a line) and in addition, require them to match entire lines. This is
423equivalent to having ^ and $ characters at the start and end of each
424alternative branch in every pattern.
426<br><a name="SEC5" href="#TOC1">ENVIRONMENT VARIABLES</a><br>
428The environment variables <b>LC_ALL</b> and <b>LC_CTYPE</b> are examined, in that
429order, for a locale. The first one that is set is used. This can be overridden
430by the <b>--locale</b> option. If no locale is set, the PCRE library's default
431(usually the "C" locale) is used.
433<br><a name="SEC6" href="#TOC1">NEWLINES</a><br>
435The <b>-N</b> (<b>--newline</b>) option allows <b>pcregrep</b> to scan files with
436different newline conventions from the default. However, the setting of this
437option does not affect the way in which <b>pcregrep</b> writes information to
438the standard error and output streams. It uses the string "\n" in C
439<b>printf()</b> calls to indicate newlines, relying on the C I/O library to
440convert this to an appropriate sequence if the output is sent to a file.
442<br><a name="SEC7" href="#TOC1">OPTIONS COMPATIBILITY</a><br>
444The majority of short and long forms of <b>pcregrep</b>'s options are the same
445as in the GNU <b>grep</b> program. Any long option of the form
446<b>--xxx-regexp</b> (GNU terminology) is also available as <b>--xxx-regex</b>
447(PCRE terminology). However, the <b>--locale</b>, <b>-M</b>, <b>--multiline</b>,
448<b>-u</b>, and <b>--utf-8</b> options are specific to <b>pcregrep</b>.
450<br><a name="SEC8" href="#TOC1">OPTIONS WITH DATA</a><br>
452There are four different ways in which an option with data can be specified.
453If a short form option is used, the data may follow immediately, or in the next
454command line item. For example:
456  -f/some/file
457  -f /some/file
459If a long form option is used, the data may appear in the same command line
460item, separated by an equals character, or (with one exception) it may appear
461in the next command line item. For example:
463  --file=/some/file
464  --file /some/file
466Note, however, that if you want to supply a file name beginning with ~ as data
467in a shell command, and have the shell expand ~ to a home directory, you must
468separate the file name from the option, because the shell does not treat ~
469specially unless it is at the start of an item.
472The exception to the above is the <b>--colour</b> (or <b>--color</b>) option,
473for which the data is optional. If this option does have data, it must be given
474in the first form, using an equals character. Otherwise it will be assumed that
475it has no data.
477<br><a name="SEC9" href="#TOC1">MATCHING ERRORS</a><br>
479It is possible to supply a regular expression that takes a very long time to
480fail to match certain lines. Such patterns normally involve nested indefinite
481repeats, for example: (a+)*\d when matched against a line of a's with no final
482digit. The PCRE matching function has a resource limit that causes it to abort
483in these circumstances. If this happens, <b>pcregrep</b> outputs an error
484message and the line that caused the problem to the standard error stream. If
485there are more than 20 such errors, <b>pcregrep</b> gives up.
487<br><a name="SEC10" href="#TOC1">DIAGNOSTICS</a><br>
489Exit status is 0 if any matches were found, 1 if no matches were found, and 2
490for syntax errors and non-existent or inacessible files (even if matches were
491found in other files) or too many matching errors. Using the <b>-s</b> option to
492suppress error messages about inaccessble files does not affect the return
495<br><a name="SEC11" href="#TOC1">SEE ALSO</a><br>
497<b>pcrepattern</b>(3), <b>pcretest</b>(1).
499<br><a name="SEC12" href="#TOC1">AUTHOR</a><br>
501Philip Hazel
503University Computing Service
505Cambridge CB2 3QH, England.
508<br><a name="SEC13" href="#TOC1">REVISION</a><br>
510Last updated: 08 March 2008
512Copyright &copy; 1997-2008 University of Cambridge.
515Return to the <a href="index.html">PCRE index page</a>.