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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (show annotations) (download) (as text)
Tue Apr 17 08:22:40 2007 UTC (7 years, 3 months ago) by ph10
File MIME type: text/html
File size: 20138 byte(s)
Update HTML documentation.

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

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