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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (hide annotations) (download)
Sat Feb 24 21:41:21 2007 UTC (7 years, 7 months ago) by nigel
File size: 16400 byte(s)
Load pcre-6.5 into code/trunk.

1 nigel 49 .TH PCREGREP 1
2     .SH NAME
3     pcregrep - a grep with Perl-compatible regular expressions.
4     .SH SYNOPSIS
5 nigel 87 .B pcregrep [options] [long options] [pattern] [path1 path2 ...]
6 nigel 75 .
7 nigel 49 .SH DESCRIPTION
8 nigel 63 .rs
9     .sp
10 nigel 75 \fBpcregrep\fP searches files for character patterns, in the same way as other
11 nigel 49 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 nigel 63 .\" HREF
14 nigel 75 \fBpcrepattern\fP
15 nigel 63 .\"
16     for a full description of syntax and semantics of the regular expressions that
17     PCRE supports.
18 nigel 75 .P
19 nigel 87 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 on the command line because they are
27     interpreted by the shell, and indeed they are required if a pattern contains
28     white space or shell metacharacters.
29 nigel 75 .P
30 nigel 87 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 nigel 77 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 the pattern is copied to the standard
43 nigel 87 output, and if there is more than one file, the file name is output at the
44     start of each line. However, there are options that can change how
45 nigel 77 \fBpcregrep\fP behaves. In particular, the \fB-M\fP option makes it possible to
46     search for patterns that span line boundaries.
47 nigel 75 .P
48 nigel 77 Patterns are limited to 8K or BUFSIZ characters, whichever is the greater.
49     BUFSIZ is defined in \fB<stdio.h>\fP.
50 nigel 87 .P
51     If the \fBLC_ALL\fP or \fBLC_CTYPE\fP environment variable is set,
52     \fBpcregrep\fP uses the value to set a locale when calling the PCRE library.
53     The \fB--locale\fP option can be used to override this.
54 nigel 75 .
55 nigel 49 .SH OPTIONS
56 nigel 63 .rs
57 nigel 49 .TP 10
58 nigel 77 \fB--\fP
59     This terminate the list of options. It is useful if the next item on the
60 nigel 87 command line starts with a hyphen but is not an option. This allows for the
61     processing of patterns and filenames that start with hyphens.
62 nigel 49 .TP
63 nigel 87 \fB-A\fP \fInumber\fP, \fB--after-context=\fP\fInumber\fP
64     Output \fInumber\fP lines of context after each matching line. If filenames
65     and/or line numbers are being output, a hyphen separator is used instead of a
66     colon for the context lines. A line containing "--" is output between each
67 nigel 77 group of lines, unless they are in fact contiguous in the input file. The value
68     of \fInumber\fP is expected to be relatively small. However, \fBpcregrep\fP
69 nigel 87 guarantees to have up to 8K of following text available for context output.
70 nigel 77 .TP
71 nigel 87 \fB-B\fP \fInumber\fP, \fB--before-context=\fP\fInumber\fP
72     Output \fInumber\fP lines of context before each matching line. If filenames
73     and/or line numbers are being output, a hyphen separator is used instead of a
74     colon for the context lines. A line containing "--" is output between each
75 nigel 77 group of lines, unless they are in fact contiguous in the input file. The value
76     of \fInumber\fP is expected to be relatively small. However, \fBpcregrep\fP
77 nigel 87 guarantees to have up to 8K of preceding text available for context output.
78 nigel 77 .TP
79 nigel 87 \fB-C\fP \fInumber\fP, \fB--context=\fP\fInumber\fP
80     Output \fInumber\fP lines of context both before and after each matching line.
81 nigel 77 This is equivalent to setting both \fB-A\fP and \fB-B\fP to the same value.
82     .TP
83 nigel 87 \fB-c\fP, \fB--count\fP
84     Do not output individual lines; instead just output a count of the number of
85     lines that would otherwise have been output. If several files are given, a
86     count is output for each of them. In this mode, the \fB-A\fP, \fB-B\fP, and
87     \fB-C\fP options are ignored.
88 nigel 49 .TP
89 nigel 87 \fB--colour\fP, \fB--color\fP
90     If this option is given without any data, it is equivalent to "--colour=auto".
91     If data is required, it must be given in the same shell item, separated by an
92     equals sign.
93     .TP
94     \fB--colour=\fP\fIvalue\fP, \fB--color=\fP\fIvalue\fP
95     This option specifies under what circumstances the part of a line that matched
96     a pattern should be coloured in the output. The value may be "never" (the
97     default), "always", or "auto". In the latter case, colouring happens only if
98     the standard output is connected to a terminal. The colour can be specified by
99     setting the environment variable PCREGREP_COLOUR or PCREGREP_COLOR. The value
100     of this variable should be a string of two numbers, separated by a semicolon.
101     They are copied directly into the control string for setting colour on a
102     terminal, so it is your responsibility to ensure that they make sense. If
103     neither of the environment variables is set, the default is "1;31", which gives
104     red.
105     .TP
106     \fB-D\fP \fIaction\fP, \fB--devices=\fP\fIaction\fP
107     If an input path is not a regular file or a directory, "action" specifies how
108     it is to be processed. Valid values are "read" (the default) or "skip"
109     (silently skip the path).
110     .TP
111     \fB-d\fP \fIaction\fP, \fB--directories=\fP\fIaction\fP
112     If an input path is a directory, "action" specifies how it is to be processed.
113     Valid values are "read" (the default), "recurse" (equivalent to the \fB-r\fP
114     option), or "skip" (silently skip the path). In the default case, directories
115     are read as if they were ordinary files. In some operating systems the effect
116     of reading a directory like this is an immediate end-of-file.
117     .TP
118     \fB-e\fP \fIpattern\fP, \fB--regex=\fP\fIpattern\fP,
119     \fB--regexp=\fP\fIpattern\fP Specify a pattern to be matched. This option can
120     be used multiple times in order to specify several patterns. It can also be
121     used as a way of specifying a single pattern that starts with a hyphen. When
122     \fB-e\fP is used, no argument pattern is taken from the command line; all
123     arguments are treated as file names. There is an overall maximum of 100
124     patterns. They are applied to each line in the order in which they are defined
125     until one matches (or fails to match if \fB-v\fP is used). If \fB-f\fP is used
126     with \fB-e\fP, the command line patterns are matched first, followed by the
127     patterns from the file, independent of the order in which these options are
128     specified. Note that multiple use of \fB-e\fP is not the same as a single
129     pattern with alternatives. For example, X|Y finds the first character in a line
130     that is X or Y, whereas if the two patterns are given separately,
131     \fBpcregrep\fP finds X if it is present, even if it follows Y in the line. It
132     finds Y only if there is no X in the line. This really matters only if you are
133     using \fB-o\fP to show the portion of the line that matched.
134     .TP
135 nigel 77 \fB--exclude\fP=\fIpattern\fP
136     When \fBpcregrep\fP is searching the files in a directory as a consequence of
137     the \fB-r\fP (recursive search) option, any files whose names match the pattern
138     are excluded. The pattern is a PCRE regular expression. If a file name matches
139     both \fB--include\fP and \fB--exclude\fP, it is excluded. There is no short
140     form for this option.
141     .TP
142 nigel 87 \fB-F\fP, \fB--fixed-strings\fP
143     Interpret each pattern as a list of fixed strings, separated by newlines,
144     instead of as a regular expression. The \fB-w\fP (match as a word) and \fB-x\fP
145     (match whole line) options can be used with \fB-F\fP. They apply to each of the
146     fixed strings. A line is selected if any of the fixed strings are found in it
147     (subject to \fB-w\fP or \fB-x\fP, if present).
148 nigel 53 .TP
149 nigel 87 \fB-f\fP \fIfilename\fP, \fB--file=\fP\fIfilename\fP
150     Read a number of patterns from the file, one per line, and match them against
151     each line of input. A data line is output if any of the patterns match it. The
152     filename can be given as "-" to refer to the standard input. When \fB-f\fP is
153     used, patterns specified on the command line using \fB-e\fP may also be
154     present; they are tested before the file's patterns. However, no other pattern
155     is taken from the command line; all arguments are treated as file names. There
156     is an overall maximum of 100 patterns. Trailing white space is removed from
157     each line, and blank lines are ignored. An empty file contains no patterns and
158     therefore matches nothing.
159 nigel 49 .TP
160 nigel 87 \fB-H\fP, \fB--with-filename\fP
161     Force the inclusion of the filename at the start of output lines when searching
162     a single file. By default, the filename is not shown in this case. For matching
163     lines, the filename is followed by a colon and a space; for context lines, a
164     hyphen separator is used. If a line number is also being output, it follows the
165     file name without a space.
166     .TP
167     \fB-h\fP, \fB--no-filename\fP
168     Suppress the output filenames when searching multiple files. By default,
169     filenames are shown when multiple files are searched. For matching lines, the
170     filename is followed by a colon and a space; for context lines, a hyphen
171     separator is used. If a line number is also being output, it follows the file
172     name without a space.
173     .TP
174     \fB--help\fP
175     Output a brief help message and exit.
176     .TP
177     \fB-i\fP, \fB--ignore-case\fP
178 nigel 49 Ignore upper/lower case distinctions during comparisons.
179     .TP
180 nigel 77 \fB--include\fP=\fIpattern\fP
181     When \fBpcregrep\fP is searching the files in a directory as a consequence of
182 nigel 87 the \fB-r\fP (recursive search) option, only those files whose names match the
183 nigel 77 pattern are included. The pattern is a PCRE regular expression. If a file name
184     matches both \fB--include\fP and \fB--exclude\fP, it is excluded. There is no
185     short form for this option.
186     .TP
187 nigel 87 \fB-L\fP, \fB--files-without-match\fP
188     Instead of outputting lines from the files, just output the names of the files
189     that do not contain any lines that would have been output. Each file name is
190     output once, on a separate line.
191 nigel 77 .TP
192 nigel 87 \fB-l\fP, \fB--files-with-matches\fP
193     Instead of outputting lines from the files, just output the names of the files
194     containing lines that would have been output. Each file name is output
195     once, on a separate line. Searching stops as soon as a matching line is found
196     in a file.
197 nigel 49 .TP
198 nigel 77 \fB--label\fP=\fIname\fP
199     This option supplies a name to be used for the standard input when file names
200 nigel 87 are being output. If not supplied, "(standard input)" is used. There is no
201 nigel 77 short form for this option.
202     .TP
203 nigel 87 \fB--locale\fP=\fIlocale-name\fP
204     This option specifies a locale to be used for pattern matching. It overrides
205     the value in the \fBLC_ALL\fP or \fBLC_CTYPE\fP environment variables. If no
206     locale is specified, the PCRE library's default (usually the "C" locale) is
207     used. There is no short form for this option.
208     .TP
209     \fB-M\fP, \fB--multiline\fP
210 nigel 77 Allow patterns to match more than one line. When this option is given, patterns
211     may usefully contain literal newline characters and internal occurrences of ^
212     and $ characters. The output for any one match may consist of more than one
213     line. When this option is set, the PCRE library is called in "multiline" mode.
214     There is a limit to the number of lines that can be matched, imposed by the way
215     that \fBpcregrep\fP buffers the input file as it scans it. However,
216     \fBpcregrep\fP ensures that at least 8K characters or the rest of the document
217     (whichever is the shorter) are available for forward matching, and similarly
218     the previous 8K characters (or all the previous characters, if fewer than 8K)
219     are guaranteed to be available for lookbehind assertions.
220     .TP
221 nigel 87 \fB-n\fP, \fB--line-number\fP
222     Precede each output line by its line number in the file, followed by a colon
223     and a space for matching lines or a hyphen and a space for context lines. If
224     the filename is also being output, it precedes the line number.
225 nigel 49 .TP
226 nigel 87 \fB-o\fP, \fB--only-matching\fP
227     Show only the part of the line that matched a pattern. In this mode, no
228     context is shown. That is, the \fB-A\fP, \fB-B\fP, and \fB-C\fP options are
229     ignored.
230 nigel 77 .TP
231 nigel 87 \fB-q\fP, \fB--quiet\fP
232     Work quietly, that is, display nothing except error messages. The exit
233     status indicates whether or not any matches were found.
234     .TP
235     \fB-r\fP, \fB--recursive\fP
236 nigel 77 If any given path is a directory, recursively scan the files it contains,
237 nigel 87 taking note of any \fB--include\fP and \fB--exclude\fP settings. By default, a
238     directory is read as a normal file; in some operating systems this gives an
239     immediate end-of-file. This option is a shorthand for setting the \fB-d\fP
240     option to "recurse".
241 nigel 53 .TP
242 nigel 87 \fB-s\fP, \fB--no-messages\fP
243 nigel 77 Suppress error messages about non-existent or unreadable files. Such files are
244     quietly skipped. However, the return code is still 2, even if matches were
245     found in other files.
246 nigel 49 .TP
247 nigel 87 \fB-u\fP, \fB--utf-8\fP
248 nigel 63 Operate in UTF-8 mode. This option is available only if PCRE has been compiled
249 nigel 87 with UTF-8 support. Both patterns and subject lines must be valid strings of
250     UTF-8 characters.
251 nigel 63 .TP
252 nigel 87 \fB-V\fP, \fB--version\fP
253 nigel 77 Write the version numbers of \fBpcregrep\fP and the PCRE library that is being
254     used to the standard error stream.
255     .TP
256 nigel 87 \fB-v\fP, \fB--invert-match\fP
257     Invert the sense of the match, so that lines which do \fInot\fP match any of
258     the patterns are the ones that are found.
259 nigel 49 .TP
260 nigel 87 \fB-w\fP, \fB--word-regex\fP, \fB--word-regexp\fP
261     Force the patterns to match only whole words. This is equivalent to having \eb
262 nigel 77 at the start and end of the pattern.
263     .TP
264 nigel 87 \fB-x\fP, \fB--line-regex\fP, \fP--line-regexp\fP
265     Force the patterns to be anchored (each must start matching at the beginning of
266     a line) and in addition, require them to match entire lines. This is
267 nigel 49 equivalent to having ^ and $ characters at the start and end of each
268 nigel 87 alternative branch in every pattern.
269 nigel 75 .
270 nigel 87 .
271     .SH "ENVIRONMENT VARIABLES"
272 nigel 63 .rs
273     .sp
274 nigel 87 The environment variables \fBLC_ALL\fP and \fBLC_CTYPE\fP are examined, in that
275     order, for a locale. The first one that is set is used. This can be overridden
276     by the \fB--locale\fP option. If no locale is set, the PCRE library's default
277     (usually the "C" locale) is used.
278     .
279     .
280     .SH "OPTIONS COMPATIBILITY"
281     .rs
282 nigel 75 .sp
283 nigel 87 The majority of short and long forms of \fBpcregrep\fP's options are the same
284     as in the GNU \fBgrep\fP program. Any long option of the form
285     \fB--xxx-regexp\fP (GNU terminology) is also available as \fB--xxx-regex\fP
286     (PCRE terminology). However, the \fB--locale\fP, \fB-M\fP, \fB--multiline\fP,
287     \fB-u\fP, and \fB--utf-8\fP options are specific to \fBpcregrep\fP.
288 nigel 77 .
289 nigel 87 .
290 nigel 77 .SH "OPTIONS WITH DATA"
291     .rs
292 nigel 75 .sp
293 nigel 77 There are four different ways in which an option with data can be specified.
294     If a short form option is used, the data may follow immediately, or in the next
295     command line item. For example:
296     .sp
297     -f/some/file
298     -f /some/file
299     .sp
300     If a long form option is used, the data may appear in the same command line
301 nigel 87 item, separated by an equals character, or (with one exception) it may appear
302     in the next command line item. For example:
303 nigel 77 .sp
304     --file=/some/file
305     --file /some/file
306     .sp
307 nigel 87 Note, however, that if you want to supply a file name beginning with ~ as data
308     in a shell command, and have the shell expand ~ to a home directory, you must
309     separate the file name from the option, because the shell does not treat ~
310     specially unless it is at the start of an item.
311     .P
312     The exception to the above is the \fB--colour\fP (or \fB--color\fP) option,
313     for which the data is optional. If this option does have data, it must be given
314     in the first form, using an equals character. Otherwise it will be assumed that
315     it has no data.
316 nigel 75 .
317 nigel 87 .
318     .SH MATCHING ERRORS
319     .rs
320     .sp
321     It is possible to supply a regular expression that takes a very long time to
322     fail to match certain lines. Such patterns normally involve nested indefinite
323     repeats, for example: (a+)*\ed when matched against a line of a's with no final
324     digit. The PCRE matching function has a resource limit that causes it to abort
325     in these circumstances. If this happens, \fBpcregrep\fP outputs an error
326     message and the line that caused the problem to the standard error stream. If
327     there are more than 20 such errors, \fBpcregrep\fP gives up.
328     .
329     .
330 nigel 49 .SH DIAGNOSTICS
331 nigel 63 .rs
332     .sp
333 nigel 49 Exit status is 0 if any matches were found, 1 if no matches were found, and 2
334 nigel 77 for syntax errors and non-existent or inacessible files (even if matches were
335 nigel 87 found in other files) or too many matching errors. Using the \fB-s\fP option to
336     suppress error messages about inaccessble files does not affect the return
337     code.
338 nigel 75 .
339     .
340 nigel 49 .SH AUTHOR
341 nigel 63 .rs
342     .sp
343 nigel 77 Philip Hazel
344 nigel 63 .br
345     University Computing Service
346     .br
347     Cambridge CB2 3QG, England.
348 nigel 75 .P
349 nigel 63 .in 0
350 nigel 87 Last updated: 23 January 2006
351 nigel 49 .br
352 nigel 87 Copyright (c) 1997-2006 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12