ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 93 - (show annotations)
Sat Feb 24 21:41:42 2007 UTC (13 years, 9 months ago) by nigel
File MIME type: text/html
File size: 19948 byte(s)
Error occurred while calculating annotation data.
Load pcre-7.0 into code/trunk.
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>
8 Return to the <a href="index.html">PCRE index page</a>.
9 </p>
10 <p>
11 This page is part of the PCRE HTML documentation. It was generated automatically
12 from the original man page. If there is any nonsense in it, please consult the
13 man 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">OPTIONS</a>
19 <li><a name="TOC4" href="#SEC4">ENVIRONMENT VARIABLES</a>
20 <li><a name="TOC5" href="#SEC5">NEWLINES</a>
21 <li><a name="TOC6" href="#SEC6">OPTIONS COMPATIBILITY</a>
22 <li><a name="TOC7" href="#SEC7">OPTIONS WITH DATA</a>
23 <li><a name="TOC8" href="#SEC8">MATCHING ERRORS</a>
24 <li><a name="TOC9" href="#SEC9">DIAGNOSTICS</a>
25 <li><a name="TOC10" href="#SEC10">SEE ALSO</a>
26 <li><a name="TOC11" href="#SEC11">AUTHOR</a>
27 </ul>
28 <br><a name="SEC1" href="#TOC1">SYNOPSIS</a><br>
29 <P>
30 <b>pcregrep [options] [long options] [pattern] [path1 path2 ...]</b>
31 </P>
32 <br><a name="SEC2" href="#TOC1">DESCRIPTION</a><br>
33 <P>
34 <b>pcregrep</b> searches files for character patterns, in the same way as other
35 grep commands do, but it uses the PCRE regular expression library to support
36 patterns that are compatible with the regular expressions of Perl 5. See
37 <a href="pcrepattern.html"><b>pcrepattern</b>(3)</a>
38 for a full description of syntax and semantics of the regular expressions
39 that PCRE supports.
40 </P>
41 <P>
42 Patterns, whether supplied on the command line or in a separate file, are given
43 without delimiters. For example:
44 <pre>
45 pcregrep Thursday /etc/motd
46 </pre>
47 If you attempt to use delimiters (for example, by surrounding a pattern with
48 slashes, as is common in Perl scripts), they are interpreted as part of the
49 pattern. Quotes can of course be used on the command line because they are
50 interpreted by the shell, and indeed they are required if a pattern contains
51 white space or shell metacharacters.
52 </P>
53 <P>
54 The first argument that follows any option settings is treated as the single
55 pattern to be matched when neither <b>-e</b> nor <b>-f</b> is present.
56 Conversely, when one or both of these options are used to specify patterns, all
57 arguments are treated as path names. At least one of <b>-e</b>, <b>-f</b>, or an
58 argument pattern must be provided.
59 </P>
60 <P>
61 If no files are specified, <b>pcregrep</b> reads the standard input. The
62 standard input can also be referenced by a name consisting of a single hyphen.
63 For example:
64 <pre>
65 pcregrep some-pattern /file1 - /file3
66 </pre>
67 By default, each line that matches the pattern is copied to the standard
68 output, and if there is more than one file, the file name is output at the
69 start of each line. However, there are options that can change how
70 <b>pcregrep</b> behaves. In particular, the <b>-M</b> option makes it possible to
71 search for patterns that span line boundaries. What defines a line boundary is
72 controlled by the <b>-N</b> (<b>--newline</b>) option.
73 </P>
74 <P>
75 Patterns are limited to 8K or BUFSIZ characters, whichever is the greater.
76 BUFSIZ is defined in <b>&#60;stdio.h&#62;</b>.
77 </P>
78 <P>
79 If the <b>LC_ALL</b> or <b>LC_CTYPE</b> environment variable is set,
80 <b>pcregrep</b> uses the value to set a locale when calling the PCRE library.
81 The <b>--locale</b> option can be used to override this.
82 </P>
83 <br><a name="SEC3" href="#TOC1">OPTIONS</a><br>
84 <P>
85 <b>--</b>
86 This terminate the list of options. It is useful if the next item on the
87 command line starts with a hyphen but is not an option. This allows for the
88 processing of patterns and filenames that start with hyphens.
89 </P>
90 <P>
91 <b>-A</b> <i>number</i>, <b>--after-context=</b><i>number</i>
92 Output <i>number</i> lines of context after each matching line. If filenames
93 and/or line numbers are being output, a hyphen separator is used instead of a
94 colon for the context lines. A line containing "--" is output between each
95 group of lines, unless they are in fact contiguous in the input file. The value
96 of <i>number</i> is expected to be relatively small. However, <b>pcregrep</b>
97 guarantees to have up to 8K of following text available for context output.
98 </P>
99 <P>
100 <b>-B</b> <i>number</i>, <b>--before-context=</b><i>number</i>
101 Output <i>number</i> lines of context before each matching line. If filenames
102 and/or line numbers are being output, a hyphen separator is used instead of a
103 colon for the context lines. A line containing "--" is output between each
104 group of lines, unless they are in fact contiguous in the input file. The value
105 of <i>number</i> is expected to be relatively small. However, <b>pcregrep</b>
106 guarantees to have up to 8K of preceding text available for context output.
107 </P>
108 <P>
109 <b>-C</b> <i>number</i>, <b>--context=</b><i>number</i>
110 Output <i>number</i> lines of context both before and after each matching line.
111 This is equivalent to setting both <b>-A</b> and <b>-B</b> to the same value.
112 </P>
113 <P>
114 <b>-c</b>, <b>--count</b>
115 Do not output individual lines; instead just output a count of the number of
116 lines that would otherwise have been output. If several files are given, a
117 count is output for each of them. In this mode, the <b>-A</b>, <b>-B</b>, and
118 <b>-C</b> options are ignored.
119 </P>
120 <P>
121 <b>--colour</b>, <b>--color</b>
122 If this option is given without any data, it is equivalent to "--colour=auto".
123 If data is required, it must be given in the same shell item, separated by an
124 equals sign.
125 </P>
126 <P>
127 <b>--colour=</b><i>value</i>, <b>--color=</b><i>value</i>
128 This option specifies under what circumstances the part of a line that matched
129 a pattern should be coloured in the output. The value may be "never" (the
130 default), "always", or "auto". In the latter case, colouring happens only if
131 the standard output is connected to a terminal. The colour can be specified by
132 setting the environment variable PCREGREP_COLOUR or PCREGREP_COLOR. The value
133 of this variable should be a string of two numbers, separated by a semicolon.
134 They are copied directly into the control string for setting colour on a
135 terminal, so it is your responsibility to ensure that they make sense. If
136 neither of the environment variables is set, the default is "1;31", which gives
137 red.
138 </P>
139 <P>
140 <b>-D</b> <i>action</i>, <b>--devices=</b><i>action</i>
141 If an input path is not a regular file or a directory, "action" specifies how
142 it is to be processed. Valid values are "read" (the default) or "skip"
143 (silently skip the path).
144 </P>
145 <P>
146 <b>-d</b> <i>action</i>, <b>--directories=</b><i>action</i>
147 If an input path is a directory, "action" specifies how it is to be processed.
148 Valid values are "read" (the default), "recurse" (equivalent to the <b>-r</b>
149 option), or "skip" (silently skip the path). In the default case, directories
150 are read as if they were ordinary files. In some operating systems the effect
151 of reading a directory like this is an immediate end-of-file.
152 </P>
153 <P>
154 <b>-e</b> <i>pattern</i>, <b>--regex=</b><i>pattern</i>,
155 <b>--regexp=</b><i>pattern</i> Specify a pattern to be matched. This option can
156 be used multiple times in order to specify several patterns. It can also be
157 used as a way of specifying a single pattern that starts with a hyphen. When
158 <b>-e</b> is used, no argument pattern is taken from the command line; all
159 arguments are treated as file names. There is an overall maximum of 100
160 patterns. They are applied to each line in the order in which they are defined
161 until one matches (or fails to match if <b>-v</b> is used). If <b>-f</b> is used
162 with <b>-e</b>, the command line patterns are matched first, followed by the
163 patterns from the file, independent of the order in which these options are
164 specified. Note that multiple use of <b>-e</b> is not the same as a single
165 pattern with alternatives. For example, X|Y finds the first character in a line
166 that is X or Y, whereas if the two patterns are given separately,
167 <b>pcregrep</b> finds X if it is present, even if it follows Y in the line. It
168 finds Y only if there is no X in the line. This really matters only if you are
169 using <b>-o</b> to show the portion of the line that matched.
170 </P>
171 <P>
172 <b>--exclude</b>=<i>pattern</i>
173 When <b>pcregrep</b> is searching the files in a directory as a consequence of
174 the <b>-r</b> (recursive search) option, any files whose names match the pattern
175 are excluded. The pattern is a PCRE regular expression. If a file name matches
176 both <b>--include</b> and <b>--exclude</b>, it is excluded. There is no short
177 form for this option.
178 </P>
179 <P>
180 <b>-F</b>, <b>--fixed-strings</b>
181 Interpret each pattern as a list of fixed strings, separated by newlines,
182 instead of as a regular expression. The <b>-w</b> (match as a word) and <b>-x</b>
183 (match whole line) options can be used with <b>-F</b>. They apply to each of the
184 fixed strings. A line is selected if any of the fixed strings are found in it
185 (subject to <b>-w</b> or <b>-x</b>, if present).
186 </P>
187 <P>
188 <b>-f</b> <i>filename</i>, <b>--file=</b><i>filename</i>
189 Read a number of patterns from the file, one per line, and match them against
190 each line of input. A data line is output if any of the patterns match it. The
191 filename can be given as "-" to refer to the standard input. When <b>-f</b> is
192 used, patterns specified on the command line using <b>-e</b> may also be
193 present; they are tested before the file's patterns. However, no other pattern
194 is taken from the command line; all arguments are treated as file names. There
195 is an overall maximum of 100 patterns. Trailing white space is removed from
196 each line, and blank lines are ignored. An empty file contains no patterns and
197 therefore matches nothing.
198 </P>
199 <P>
200 <b>-H</b>, <b>--with-filename</b>
201 Force the inclusion of the filename at the start of output lines when searching
202 a single file. By default, the filename is not shown in this case. For matching
203 lines, the filename is followed by a colon and a space; for context lines, a
204 hyphen separator is used. If a line number is also being output, it follows the
205 file name without a space.
206 </P>
207 <P>
208 <b>-h</b>, <b>--no-filename</b>
209 Suppress the output filenames when searching multiple files. By default,
210 filenames are shown when multiple files are searched. For matching lines, the
211 filename is followed by a colon and a space; for context lines, a hyphen
212 separator is used. If a line number is also being output, it follows the file
213 name without a space.
214 </P>
215 <P>
216 <b>--help</b>
217 Output a brief help message and exit.
218 </P>
219 <P>
220 <b>-i</b>, <b>--ignore-case</b>
221 Ignore upper/lower case distinctions during comparisons.
222 </P>
223 <P>
224 <b>--include</b>=<i>pattern</i>
225 When <b>pcregrep</b> is searching the files in a directory as a consequence of
226 the <b>-r</b> (recursive search) option, only those files whose names match the
227 pattern are included. The pattern is a PCRE regular expression. If a file name
228 matches both <b>--include</b> and <b>--exclude</b>, it is excluded. There is no
229 short form for this option.
230 </P>
231 <P>
232 <b>-L</b>, <b>--files-without-match</b>
233 Instead of outputting lines from the files, just output the names of the files
234 that do not contain any lines that would have been output. Each file name is
235 output once, on a separate line.
236 </P>
237 <P>
238 <b>-l</b>, <b>--files-with-matches</b>
239 Instead of outputting lines from the files, just output the names of the files
240 containing lines that would have been output. Each file name is output
241 once, on a separate line. Searching stops as soon as a matching line is found
242 in a file.
243 </P>
244 <P>
245 <b>--label</b>=<i>name</i>
246 This option supplies a name to be used for the standard input when file names
247 are being output. If not supplied, "(standard input)" is used. There is no
248 short form for this option.
249 </P>
250 <P>
251 <b>--locale</b>=<i>locale-name</i>
252 This option specifies a locale to be used for pattern matching. It overrides
253 the value in the <b>LC_ALL</b> or <b>LC_CTYPE</b> environment variables. If no
254 locale is specified, the PCRE library's default (usually the "C" locale) is
255 used. There is no short form for this option.
256 </P>
257 <P>
258 <b>-M</b>, <b>--multiline</b>
259 Allow patterns to match more than one line. When this option is given, patterns
260 may usefully contain literal newline characters and internal occurrences of ^
261 and $ characters. The output for any one match may consist of more than one
262 line. When this option is set, the PCRE library is called in "multiline" mode.
263 There is a limit to the number of lines that can be matched, imposed by the way
264 that <b>pcregrep</b> buffers the input file as it scans it. However,
265 <b>pcregrep</b> ensures that at least 8K characters or the rest of the document
266 (whichever is the shorter) are available for forward matching, and similarly
267 the previous 8K characters (or all the previous characters, if fewer than 8K)
268 are guaranteed to be available for lookbehind assertions.
269 </P>
270 <P>
271 <b>-N</b> <i>newline-type</i>, <b>--newline=</b><i>newline-type</i>
272 The PCRE library supports four different conventions for indicating
273 the ends of lines. They are the single-character sequences CR (carriage return)
274 and LF (linefeed), the two-character sequence CRLF, and an "any" convention, in
275 which any Unicode line ending sequence is assumed to end a line. The Unicode
276 sequences are the three just mentioned, plus VT (vertical tab, U+000B), FF
277 (formfeed, U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and
278 PS (paragraph separator, U+0029).
279 <br>
280 <br>
281 When the PCRE library is built, a default line-ending sequence is specified.
282 This is normally the standard sequence for the operating system. Unless
283 otherwise specified by this option, <b>pcregrep</b> uses the library's default.
284 The possible values for this option are CR, LF, CRLF, or ANY. This makes it
285 possible to use <b>pcregrep</b> on files that have come from other environments
286 without having to modify their line endings. If the data that is being scanned
287 does not agree with the convention set by this option, <b>pcregrep</b> may
288 behave in strange ways.
289 </P>
290 <P>
291 <b>-n</b>, <b>--line-number</b>
292 Precede each output line by its line number in the file, followed by a colon
293 and a space for matching lines or a hyphen and a space for context lines. If
294 the filename is also being output, it precedes the line number.
295 </P>
296 <P>
297 <b>-o</b>, <b>--only-matching</b>
298 Show only the part of the line that matched a pattern. In this mode, no
299 context is shown. That is, the <b>-A</b>, <b>-B</b>, and <b>-C</b> options are
300 ignored.
301 </P>
302 <P>
303 <b>-q</b>, <b>--quiet</b>
304 Work quietly, that is, display nothing except error messages. The exit
305 status indicates whether or not any matches were found.
306 </P>
307 <P>
308 <b>-r</b>, <b>--recursive</b>
309 If any given path is a directory, recursively scan the files it contains,
310 taking note of any <b>--include</b> and <b>--exclude</b> settings. By default, a
311 directory is read as a normal file; in some operating systems this gives an
312 immediate end-of-file. This option is a shorthand for setting the <b>-d</b>
313 option to "recurse".
314 </P>
315 <P>
316 <b>-s</b>, <b>--no-messages</b>
317 Suppress error messages about non-existent or unreadable files. Such files are
318 quietly skipped. However, the return code is still 2, even if matches were
319 found in other files.
320 </P>
321 <P>
322 <b>-u</b>, <b>--utf-8</b>
323 Operate in UTF-8 mode. This option is available only if PCRE has been compiled
324 with UTF-8 support. Both patterns and subject lines must be valid strings of
325 UTF-8 characters.
326 </P>
327 <P>
328 <b>-V</b>, <b>--version</b>
329 Write the version numbers of <b>pcregrep</b> and the PCRE library that is being
330 used to the standard error stream.
331 </P>
332 <P>
333 <b>-v</b>, <b>--invert-match</b>
334 Invert the sense of the match, so that lines which do <i>not</i> match any of
335 the patterns are the ones that are found.
336 </P>
337 <P>
338 <b>-w</b>, <b>--word-regex</b>, <b>--word-regexp</b>
339 Force the patterns to match only whole words. This is equivalent to having \b
340 at the start and end of the pattern.
341 </P>
342 <P>
343 <b>-x</b>, <b>--line-regex</b>, \fP--line-regexp\fP
344 Force the patterns to be anchored (each must start matching at the beginning of
345 a line) and in addition, require them to match entire lines. This is
346 equivalent to having ^ and $ characters at the start and end of each
347 alternative branch in every pattern.
348 </P>
349 <br><a name="SEC4" href="#TOC1">ENVIRONMENT VARIABLES</a><br>
350 <P>
351 The environment variables <b>LC_ALL</b> and <b>LC_CTYPE</b> are examined, in that
352 order, for a locale. The first one that is set is used. This can be overridden
353 by the <b>--locale</b> option. If no locale is set, the PCRE library's default
354 (usually the "C" locale) is used.
355 </P>
356 <br><a name="SEC5" href="#TOC1">NEWLINES</a><br>
357 <P>
358 The <b>-N</b> (<b>--newline</b>) option allows <b>pcregrep</b> to scan files with
359 different newline conventions from the default. However, the setting of this
360 option does not affect the way in which <b>pcregrep</b> writes information to
361 the standard error and output streams. It uses the string "\n" in C
362 <b>printf()</b> calls to indicate newlines, relying on the C I/O library to
363 convert this to an appropriate sequence if the output is sent to a file.
364 </P>
365 <br><a name="SEC6" href="#TOC1">OPTIONS COMPATIBILITY</a><br>
366 <P>
367 The majority of short and long forms of <b>pcregrep</b>'s options are the same
368 as in the GNU <b>grep</b> program. Any long option of the form
369 <b>--xxx-regexp</b> (GNU terminology) is also available as <b>--xxx-regex</b>
370 (PCRE terminology). However, the <b>--locale</b>, <b>-M</b>, <b>--multiline</b>,
371 <b>-u</b>, and <b>--utf-8</b> options are specific to <b>pcregrep</b>.
372 </P>
373 <br><a name="SEC7" href="#TOC1">OPTIONS WITH DATA</a><br>
374 <P>
375 There are four different ways in which an option with data can be specified.
376 If a short form option is used, the data may follow immediately, or in the next
377 command line item. For example:
378 <pre>
379 -f/some/file
380 -f /some/file
381 </pre>
382 If a long form option is used, the data may appear in the same command line
383 item, separated by an equals character, or (with one exception) it may appear
384 in the next command line item. For example:
385 <pre>
386 --file=/some/file
387 --file /some/file
388 </pre>
389 Note, however, that if you want to supply a file name beginning with ~ as data
390 in a shell command, and have the shell expand ~ to a home directory, you must
391 separate the file name from the option, because the shell does not treat ~
392 specially unless it is at the start of an item.
393 </P>
394 <P>
395 The exception to the above is the <b>--colour</b> (or <b>--color</b>) option,
396 for which the data is optional. If this option does have data, it must be given
397 in the first form, using an equals character. Otherwise it will be assumed that
398 it has no data.
399 </P>
400 <br><a name="SEC8" href="#TOC1">MATCHING ERRORS</a><br>
401 <P>
402 It is possible to supply a regular expression that takes a very long time to
403 fail to match certain lines. Such patterns normally involve nested indefinite
404 repeats, for example: (a+)*\d when matched against a line of a's with no final
405 digit. The PCRE matching function has a resource limit that causes it to abort
406 in these circumstances. If this happens, <b>pcregrep</b> outputs an error
407 message and the line that caused the problem to the standard error stream. If
408 there are more than 20 such errors, <b>pcregrep</b> gives up.
409 </P>
410 <br><a name="SEC9" href="#TOC1">DIAGNOSTICS</a><br>
411 <P>
412 Exit status is 0 if any matches were found, 1 if no matches were found, and 2
413 for syntax errors and non-existent or inacessible files (even if matches were
414 found in other files) or too many matching errors. Using the <b>-s</b> option to
415 suppress error messages about inaccessble files does not affect the return
416 code.
417 </P>
418 <br><a name="SEC10" href="#TOC1">SEE ALSO</a><br>
419 <P>
420 <b>pcrepattern</b>(3), <b>pcretest</b>(1).
421 </P>
422 <br><a name="SEC11" href="#TOC1">AUTHOR</a><br>
423 <P>
424 Philip Hazel
425 <br>
426 University Computing Service
427 <br>
428 Cambridge CB2 3QH, England.
429 </P>
430 <P>
431 Last updated: 29 November 2006
432 <br>
433 Copyright &copy; 1997-2006 University of Cambridge.
434 <p>
435 Return to the <a href="index.html">PCRE index page</a>.
436 </p>

  ViewVC Help
Powered by ViewVC 1.1.5