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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 281 - (show annotations) (download)
Fri Dec 7 15:57:05 2007 UTC (6 years, 10 months ago) by ph10
File size: 20657 byte(s)
Documentation improvement.

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

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