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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5