blob: 10815919ec8dec6aa6707d7d9e4ad3ecd45d4eea [file] [log] [blame]
Elliott Hughes5b808042021-10-01 10:56:10 -07001.TH PCRE2GREP 1 "31 August 2021" "PCRE2 10.38"
2.SH NAME
3pcre2grep - a grep with Perl-compatible regular expressions.
4.SH SYNOPSIS
5.B pcre2grep [options] [long options] [pattern] [path1 path2 ...]
6.
7.SH DESCRIPTION
8.rs
9.sp
10\fBpcre2grep\fP searches files for character patterns, in the same way as other
11grep commands do, but it uses the PCRE2 regular expression library to support
12patterns that are compatible with the regular expressions of Perl 5. See
13.\" HREF
14\fBpcre2syntax\fP(3)
15.\"
16for a quick-reference summary of pattern syntax, or
17.\" HREF
18\fBpcre2pattern\fP(3)
19.\"
20for a full description of the syntax and semantics of the regular expressions
21that PCRE2 supports.
22.P
23Patterns, whether supplied on the command line or in a separate file, are given
24without delimiters. For example:
25.sp
26 pcre2grep Thursday /etc/motd
27.sp
28If you attempt to use delimiters (for example, by surrounding a pattern with
29slashes, as is common in Perl scripts), they are interpreted as part of the
30pattern. Quotes can of course be used to delimit patterns on the command line
31because they are interpreted by the shell, and indeed quotes are required if a
32pattern contains white space or shell metacharacters.
33.P
34The first argument that follows any option settings is treated as the single
35pattern to be matched when neither \fB-e\fP nor \fB-f\fP is present.
36Conversely, when one or both of these options are used to specify patterns, all
37arguments are treated as path names. At least one of \fB-e\fP, \fB-f\fP, or an
38argument pattern must be provided.
39.P
40If no files are specified, \fBpcre2grep\fP reads the standard input. The
41standard input can also be referenced by a name consisting of a single hyphen.
42For example:
43.sp
44 pcre2grep some-pattern file1 - file3
45.sp
46Input files are searched line by line. By default, each line that matches a
47pattern is copied to the standard output, and if there is more than one file,
48the file name is output at the start of each line, followed by a colon.
49However, there are options that can change how \fBpcre2grep\fP behaves. In
50particular, the \fB-M\fP option makes it possible to search for strings that
51span line boundaries. What defines a line boundary is controlled by the
52\fB-N\fP (\fB--newline\fP) option.
53.P
54The amount of memory used for buffering files that are being scanned is
55controlled by parameters that can be set by the \fB--buffer-size\fP and
56\fB--max-buffer-size\fP options. The first of these sets the size of buffer
57that is obtained at the start of processing. If an input file contains very
58long lines, a larger buffer may be needed; this is handled by automatically
59extending the buffer, up to the limit specified by \fB--max-buffer-size\fP. The
60default values for these parameters can be set when \fBpcre2grep\fP is
61built; if nothing is specified, the defaults are set to 20KiB and 1MiB
62respectively. An error occurs if a line is too long and the buffer can no
63longer be expanded.
64.P
65The block of memory that is actually used is three times the "buffer size", to
66allow for buffering "before" and "after" lines. If the buffer size is too
67small, fewer than requested "before" and "after" lines may be output.
68.P
69Patterns can be no longer than 8KiB or BUFSIZ bytes, whichever is the greater.
70BUFSIZ is defined in \fB<stdio.h>\fP. When there is more than one pattern
71(specified by the use of \fB-e\fP and/or \fB-f\fP), each pattern is applied to
72each line in the order in which they are defined, except that all the \fB-e\fP
73patterns are tried before the \fB-f\fP patterns.
74.P
75By default, as soon as one pattern matches a line, no further patterns are
76considered. However, if \fB--colour\fP (or \fB--color\fP) is used to colour the
77matching substrings, or if \fB--only-matching\fP, \fB--file-offsets\fP, or
78\fB--line-offsets\fP is used to output only the part of the line that matched
79(either shown literally, or as an offset), scanning resumes immediately
80following the match, so that further matches on the same line can be found. If
81there are multiple patterns, they are all tried on the remainder of the line,
82but patterns that follow the one that matched are not tried on the earlier
83matched part of the line.
84.P
85This behaviour means that the order in which multiple patterns are specified
86can affect the output when one of the above options is used. This is no longer
87the same behaviour as GNU grep, which now manages to display earlier matches
88for later patterns (as long as there is no overlap).
89.P
90Patterns that can match an empty string are accepted, but empty string
91matches are never recognized. An example is the pattern "(super)?(man)?", in
92which all components are optional. This pattern finds all occurrences of both
93"super" and "man"; the output differs from matching with "super|man" when only
94the matching substrings are being shown.
95.P
96If the \fBLC_ALL\fP or \fBLC_CTYPE\fP environment variable is set,
97\fBpcre2grep\fP uses the value to set a locale when calling the PCRE2 library.
98The \fB--locale\fP option can be used to override this.
99.
100.
101.SH "SUPPORT FOR COMPRESSED FILES"
102.rs
103.sp
104It is possible to compile \fBpcre2grep\fP so that it uses \fBlibz\fP or
105\fBlibbz2\fP to read compressed files whose names end in \fB.gz\fP or
106\fB.bz2\fP, respectively. You can find out whether your \fBpcre2grep\fP binary
107has support for one or both of these file types by running it with the
108\fB--help\fP option. If the appropriate support is not present, all files are
109treated as plain text. The standard input is always so treated. When input is
110from a compressed .gz or .bz2 file, the \fB--line-buffered\fP option is
111ignored.
112.
113.
114.SH "BINARY FILES"
115.rs
116.sp
117By default, a file that contains a binary zero byte within the first 1024 bytes
118is identified as a binary file, and is processed specially. However, if the
119newline type is specified as NUL, that is, the line terminator is a binary
120zero, the test for a binary file is not applied. See the \fB--binary-files\fP
121option for a means of changing the way binary files are handled.
122.
123.
124.SH "BINARY ZEROS IN PATTERNS"
125.rs
126.sp
127Patterns passed from the command line are strings that are terminated by a
128binary zero, so cannot contain internal zeros. However, patterns that are read
129from a file via the \fB-f\fP option may contain binary zeros.
130.
131.
132.SH OPTIONS
133.rs
134.sp
135The order in which some of the options appear can affect the output. For
136example, both the \fB-H\fP and \fB-l\fP options affect the printing of file
137names. Whichever comes later in the command line will be the one that takes
138effect. Similarly, except where noted below, if an option is given twice, the
139later setting is used. Numerical values for options may be followed by K or M,
140to signify multiplication by 1024 or 1024*1024 respectively.
141.TP 10
142\fB--\fP
143This terminates the list of options. It is useful if the next item on the
144command line starts with a hyphen but is not an option. This allows for the
145processing of patterns and file names that start with hyphens.
146.TP
147\fB-A\fP \fInumber\fP, \fB--after-context=\fP\fInumber\fP
148Output up to \fInumber\fP lines of context after each matching line. Fewer
149lines are output if the next match or the end of the file is reached, or if the
150processing buffer size has been set too small. If file names and/or line
151numbers are being output, a hyphen separator is used instead of a colon for the
152context lines. A line containing "--" is output between each group of lines,
153unless they are in fact contiguous in the input file. The value of \fInumber\fP
154is expected to be relatively small. When \fB-c\fP is used, \fB-A\fP is ignored.
155.TP
156\fB-a\fP, \fB--text\fP
157Treat binary files as text. This is equivalent to
158\fB--binary-files\fP=\fItext\fP.
159.TP
160\fB--allow-lookaround-bsk\fP
161PCRE2 now forbids the use of \eK in lookarounds by default, in line with Perl.
162This option causes \fBpcre2grep\fP to set the PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
163option, which enables this somewhat dangerous usage.
164.TP
165\fB-B\fP \fInumber\fP, \fB--before-context=\fP\fInumber\fP
166Output up to \fInumber\fP lines of context before each matching line. Fewer
167lines are output if the previous match or the start of the file is within
168\fInumber\fP lines, or if the processing buffer size has been set too small. If
169file names and/or line numbers are being output, a hyphen separator is used
170instead of a colon for the context lines. A line containing "--" is output
171between each group of lines, unless they are in fact contiguous in the input
172file. The value of \fInumber\fP is expected to be relatively small. When
173\fB-c\fP is used, \fB-B\fP is ignored.
174.TP
175\fB--binary-files=\fP\fIword\fP
176Specify how binary files are to be processed. If the word is "binary" (the
177default), pattern matching is performed on binary files, but the only output is
178"Binary file <name> matches" when a match succeeds. If the word is "text",
179which is equivalent to the \fB-a\fP or \fB--text\fP option, binary files are
180processed in the same way as any other file. In this case, when a match
181succeeds, the output may be binary garbage, which can have nasty effects if
182sent to a terminal. If the word is "without-match", which is equivalent to the
183\fB-I\fP option, binary files are not processed at all; they are assumed not to
184be of interest and are skipped without causing any output or affecting the
185return code.
186.TP
187\fB--buffer-size=\fP\fInumber\fP
188Set the parameter that controls how much memory is obtained at the start of
189processing for buffering files that are being scanned. See also
190\fB--max-buffer-size\fP below.
191.TP
192\fB-C\fP \fInumber\fP, \fB--context=\fP\fInumber\fP
193Output \fInumber\fP lines of context both before and after each matching line.
194This is equivalent to setting both \fB-A\fP and \fB-B\fP to the same value.
195.TP
196\fB-c\fP, \fB--count\fP
197Do not output lines from the files that are being scanned; instead output the
198number of lines that would have been shown, either because they matched, or, if
199\fB-v\fP is set, because they failed to match. By default, this count is
200exactly the same as the number of lines that would have been output, but if the
201\fB-M\fP (multiline) option is used (without \fB-v\fP), there may be more
202suppressed lines than the count (that is, the number of matches).
203.sp
204If no lines are selected, the number zero is output. If several files are are
205being scanned, a count is output for each of them and the \fB-t\fP option can
206be used to cause a total to be output at the end. However, if the
207\fB--files-with-matches\fP option is also used, only those files whose counts
208are greater than zero are listed. When \fB-c\fP is used, the \fB-A\fP,
209\fB-B\fP, and \fB-C\fP options are ignored.
210.TP
211\fB--colour\fP, \fB--color\fP
212If this option is given without any data, it is equivalent to "--colour=auto".
213If data is required, it must be given in the same shell item, separated by an
214equals sign.
215.TP
216\fB--colour=\fP\fIvalue\fP, \fB--color=\fP\fIvalue\fP
217This option specifies under what circumstances the parts of a line that matched
218a pattern should be coloured in the output. By default, the output is not
219coloured. The value (which is optional, see above) may be "never", "always", or
220"auto". In the latter case, colouring happens only if the standard output is
221connected to a terminal. More resources are used when colouring is enabled,
222because \fBpcre2grep\fP has to search for all possible matches in a line, not
223just one, in order to colour them all.
224.sp
225The colour that is used can be specified by setting one of the environment
226variables PCRE2GREP_COLOUR, PCRE2GREP_COLOR, PCREGREP_COLOUR, or
227PCREGREP_COLOR, which are checked in that order. If none of these are set,
228\fBpcre2grep\fP looks for GREP_COLORS or GREP_COLOR (in that order). The value
229of the variable should be a string of two numbers, separated by a semicolon,
230except in the case of GREP_COLORS, which must start with "ms=" or "mt="
231followed by two semicolon-separated colours, terminated by the end of the
232string or by a colon. If GREP_COLORS does not start with "ms=" or "mt=" it is
233ignored, and GREP_COLOR is checked.
234.sp
235If the string obtained from one of the above variables contains any characters
236other than semicolon or digits, the setting is ignored and the default colour
237is used. The string is copied directly into the control string for setting
238colour on a terminal, so it is your responsibility to ensure that the values
239make sense. If no relevant environment variable is set, the default is "1;31",
240which gives red.
241.TP
242\fB-D\fP \fIaction\fP, \fB--devices=\fP\fIaction\fP
243If an input path is not a regular file or a directory, "action" specifies how
244it is to be processed. Valid values are "read" (the default) or "skip"
245(silently skip the path).
246.TP
247\fB-d\fP \fIaction\fP, \fB--directories=\fP\fIaction\fP
248If an input path is a directory, "action" specifies how it is to be processed.
249Valid values are "read" (the default in non-Windows environments, for
250compatibility with GNU grep), "recurse" (equivalent to the \fB-r\fP option), or
251"skip" (silently skip the path, the default in Windows environments). In the
252"read" case, directories are read as if they were ordinary files. In some
253operating systems the effect of reading a directory like this is an immediate
254end-of-file; in others it may provoke an error.
255.TP
256\fB--depth-limit\fP=\fInumber\fP
257See \fB--match-limit\fP below.
258.TP
259\fB-e\fP \fIpattern\fP, \fB--regex=\fP\fIpattern\fP, \fB--regexp=\fP\fIpattern\fP
260Specify a pattern to be matched. This option can be used multiple times in
261order to specify several patterns. It can also be used as a way of specifying a
262single pattern that starts with a hyphen. When \fB-e\fP is used, no argument
263pattern is taken from the command line; all arguments are treated as file
264names. There is no limit to the number of patterns. They are applied to each
265line in the order in which they are defined until one matches.
266.sp
267If \fB-f\fP is used with \fB-e\fP, the command line patterns are matched first,
268followed by the patterns from the file(s), independent of the order in which
269these options are specified. Note that multiple use of \fB-e\fP is not the same
270as a single pattern with alternatives. For example, X|Y finds the first
271character in a line that is X or Y, whereas if the two patterns are given
272separately, with X first, \fBpcre2grep\fP finds X if it is present, even if it
273follows Y in the line. It finds Y only if there is no X in the line. This
274matters only if you are using \fB-o\fP or \fB--colo(u)r\fP to show the part(s)
275of the line that matched.
276.TP
277\fB--exclude\fP=\fIpattern\fP
278Files (but not directories) whose names match the pattern are skipped without
279being processed. This applies to all files, whether listed on the command line,
280obtained from \fB--file-list\fP, or by scanning a directory. The pattern is a
281PCRE2 regular expression, and is matched against the final component of the
282file name, not the entire path. The \fB-F\fP, \fB-w\fP, and \fB-x\fP options do
283not apply to this pattern. The option may be given any number of times in order
284to specify multiple patterns. If a file name matches both an \fB--include\fP
285and an \fB--exclude\fP pattern, it is excluded. There is no short form for this
286option.
287.TP
288\fB--exclude-from=\fP\fIfilename\fP
289Treat each non-empty line of the file as the data for an \fB--exclude\fP
290option. What constitutes a newline when reading the file is the operating
291system's default. The \fB--newline\fP option has no effect on this option. This
292option may be given more than once in order to specify a number of files to
293read.
294.TP
295\fB--exclude-dir\fP=\fIpattern\fP
296Directories whose names match the pattern are skipped without being processed,
297whatever the setting of the \fB--recursive\fP option. This applies to all
298directories, whether listed on the command line, obtained from
299\fB--file-list\fP, or by scanning a parent directory. The pattern is a PCRE2
300regular expression, and is matched against the final component of the directory
301name, not the entire path. The \fB-F\fP, \fB-w\fP, and \fB-x\fP options do not
302apply to this pattern. The option may be given any number of times in order to
303specify more than one pattern. If a directory matches both \fB--include-dir\fP
304and \fB--exclude-dir\fP, it is excluded. There is no short form for this
305option.
306.TP
307\fB-F\fP, \fB--fixed-strings\fP
308Interpret each data-matching pattern as a list of fixed strings, separated by
309newlines, instead of as a regular expression. What constitutes a newline for
310this purpose is controlled by the \fB--newline\fP option. The \fB-w\fP (match
311as a word) and \fB-x\fP (match whole line) options can be used with \fB-F\fP.
312They apply to each of the fixed strings. A line is selected if any of the fixed
313strings are found in it (subject to \fB-w\fP or \fB-x\fP, if present). This
314option applies only to the patterns that are matched against the contents of
315files; it does not apply to patterns specified by any of the \fB--include\fP or
316\fB--exclude\fP options.
317.TP
318\fB-f\fP \fIfilename\fP, \fB--file=\fP\fIfilename\fP
319Read patterns from the file, one per line, and match them against each line of
320input. As is the case with patterns on the command line, no delimiters should
321be used. What constitutes a newline when reading the file is the operating
322system's default interpretation of \en. The \fB--newline\fP option has no
323effect on this option. Trailing white space is removed from each line, and
324blank lines are ignored. An empty file contains no patterns and therefore
325matches nothing. Patterns read from a file in this way may contain binary
326zeros, which are treated as ordinary data characters. See also the comments
327about multiple patterns versus a single pattern with alternatives in the
328description of \fB-e\fP above.
329.sp
330If this option is given more than once, all the specified files are read. A
331data line is output if any of the patterns match it. A file name can be given
332as "-" to refer to the standard input. When \fB-f\fP is used, patterns
333specified on the command line using \fB-e\fP may also be present; they are
334tested before the file's patterns. However, no other pattern is taken from the
335command line; all arguments are treated as the names of paths to be searched.
336.TP
337\fB--file-list\fP=\fIfilename\fP
338Read a list of files and/or directories that are to be scanned from the given
339file, one per line. What constitutes a newline when reading the file is the
340operating system's default. Trailing white space is removed from each line, and
341blank lines are ignored. These paths are processed before any that are listed
342on the command line. The file name can be given as "-" to refer to the standard
343input. If \fB--file\fP and \fB--file-list\fP are both specified as "-",
344patterns are read first. This is useful only when the standard input is a
345terminal, from which further lines (the list of files) can be read after an
346end-of-file indication. If this option is given more than once, all the
347specified files are read.
348.TP
349\fB--file-offsets\fP
350Instead of showing lines or parts of lines that match, show each match as an
351offset from the start of the file and a length, separated by a comma. In this
352mode, no context is shown. That is, the \fB-A\fP, \fB-B\fP, and \fB-C\fP
353options are ignored. If there is more than one match in a line, each of them is
354shown separately. This option is mutually exclusive with \fB--output\fP,
355\fB--line-offsets\fP, and \fB--only-matching\fP.
356.TP
357\fB-H\fP, \fB--with-filename\fP
358Force the inclusion of the file name at the start of output lines when
359searching a single file. By default, the file name is not shown in this case.
360For matching lines, the file name is followed by a colon; for context lines, a
361hyphen separator is used. If a line number is also being output, it follows the
362file name. When the \fB-M\fP option causes a pattern to match more than one
363line, only the first is preceded by the file name. This option overrides any
364previous \fB-h\fP, \fB-l\fP, or \fB-L\fP options.
365.TP
366\fB-h\fP, \fB--no-filename\fP
367Suppress the output file names when searching multiple files. By default,
368file names are shown when multiple files are searched. For matching lines, the
369file name is followed by a colon; for context lines, a hyphen separator is used.
370If a line number is also being output, it follows the file name. This option
371overrides any previous \fB-H\fP, \fB-L\fP, or \fB-l\fP options.
372.TP
373\fB--heap-limit\fP=\fInumber\fP
374See \fB--match-limit\fP below.
375.TP
376\fB--help\fP
377Output a help message, giving brief details of the command options and file
378type support, and then exit. Anything else on the command line is
379ignored.
380.TP
381\fB-I\fP
382Ignore binary files. This is equivalent to
383\fB--binary-files\fP=\fIwithout-match\fP.
384.TP
385\fB-i\fP, \fB--ignore-case\fP
386Ignore upper/lower case distinctions during comparisons.
387.TP
388\fB--include\fP=\fIpattern\fP
389If any \fB--include\fP patterns are specified, the only files that are
390processed are those whose names match one of the patterns and do not match an
391\fB--exclude\fP pattern. This option does not affect directories, but it
392applies to all files, whether listed on the command line, obtained from
393\fB--file-list\fP, or by scanning a directory. The pattern is a PCRE2 regular
394expression, and is matched against the final component of the file name, not
395the entire path. The \fB-F\fP, \fB-w\fP, and \fB-x\fP options do not apply to
396this pattern. The option may be given any number of times. If a file name
397matches both an \fB--include\fP and an \fB--exclude\fP pattern, it is excluded.
398There is no short form for this option.
399.TP
400\fB--include-from=\fP\fIfilename\fP
401Treat each non-empty line of the file as the data for an \fB--include\fP
402option. What constitutes a newline for this purpose is the operating system's
403default. The \fB--newline\fP option has no effect on this option. This option
404may be given any number of times; all the files are read.
405.TP
406\fB--include-dir\fP=\fIpattern\fP
407If any \fB--include-dir\fP patterns are specified, the only directories that
408are processed are those whose names match one of the patterns and do not match
409an \fB--exclude-dir\fP pattern. This applies to all directories, whether listed
410on the command line, obtained from \fB--file-list\fP, or by scanning a parent
411directory. The pattern is a PCRE2 regular expression, and is matched against
412the final component of the directory name, not the entire path. The \fB-F\fP,
413\fB-w\fP, and \fB-x\fP options do not apply to this pattern. The option may be
414given any number of times. If a directory matches both \fB--include-dir\fP and
415\fB--exclude-dir\fP, it is excluded. There is no short form for this option.
416.TP
417\fB-L\fP, \fB--files-without-match\fP
418Instead of outputting lines from the files, just output the names of the files
419that do not contain any lines that would have been output. Each file name is
420output once, on a separate line. This option overrides any previous \fB-H\fP,
421\fB-h\fP, or \fB-l\fP options.
422.TP
423\fB-l\fP, \fB--files-with-matches\fP
424Instead of outputting lines from the files, just output the names of the files
425containing lines that would have been output. Each file name is output once, on
426a separate line. Searching normally stops as soon as a matching line is found
427in a file. However, if the \fB-c\fP (count) option is also used, matching
428continues in order to obtain the correct count, and those files that have at
429least one match are listed along with their counts. Using this option with
430\fB-c\fP is a way of suppressing the listing of files with no matches that
431occurs with \fB-c\fP on its own. This option overrides any previous \fB-H\fP,
432\fB-h\fP, or \fB-L\fP options.
433.TP
434\fB--label\fP=\fIname\fP
435This option supplies a name to be used for the standard input when file names
436are being output. If not supplied, "(standard input)" is used. There is no
437short form for this option.
438.TP
439\fB--line-buffered\fP
440When this option is given, non-compressed input is read and processed line by
441line, and the output is flushed after each write. By default, input is read in
442large chunks, unless \fBpcre2grep\fP can determine that it is reading from a
443terminal, which is currently possible only in Unix-like environments or
444Windows. Output to terminal is normally automatically flushed by the operating
445system. This option can be useful when the input or output is attached to a
446pipe and you do not want \fBpcre2grep\fP to buffer up large amounts of data.
447However, its use will affect performance, and the \fB-M\fP (multiline) option
448ceases to work. When input is from a compressed .gz or .bz2 file,
449\fB--line-buffered\fP is ignored.
450.TP
451\fB--line-offsets\fP
452Instead of showing lines or parts of lines that match, show each match as a
453line number, the offset from the start of the line, and a length. The line
454number is terminated by a colon (as usual; see the \fB-n\fP option), and the
455offset and length are separated by a comma. In this mode, no context is shown.
456That is, the \fB-A\fP, \fB-B\fP, and \fB-C\fP options are ignored. If there is
457more than one match in a line, each of them is shown separately. This option is
458mutually exclusive with \fB--output\fP, \fB--file-offsets\fP, and
459\fB--only-matching\fP.
460.TP
461\fB--locale\fP=\fIlocale-name\fP
462This option specifies a locale to be used for pattern matching. It overrides
463the value in the \fBLC_ALL\fP or \fBLC_CTYPE\fP environment variables. If no
464locale is specified, the PCRE2 library's default (usually the "C" locale) is
465used. There is no short form for this option.
466.TP
467\fB-M\fP, \fB--multiline\fP
468Allow patterns to match more than one line. When this option is set, the PCRE2
469library is called in "multiline" mode. This allows a matched string to extend
470past the end of a line and continue on one or more subsequent lines. Patterns
471used with \fB-M\fP may usefully contain literal newline characters and internal
472occurrences of ^ and $ characters. The output for a successful match may
473consist of more than one line. The first line is the line in which the match
474started, and the last line is the line in which the match ended. If the matched
475string ends with a newline sequence, the output ends at the end of that line.
476If \fB-v\fP is set, none of the lines in a multi-line match are output. Once a
477match has been handled, scanning restarts at the beginning of the line after
478the one in which the match ended.
479.sp
480The newline sequence that separates multiple lines must be matched as part of
481the pattern. For example, to find the phrase "regular expression" in a file
482where "regular" might be at the end of a line and "expression" at the start of
483the next line, you could use this command:
484.sp
485 pcre2grep -M 'regular\es+expression' <file>
486.sp
487The \es escape sequence matches any white space character, including newlines,
488and is followed by + so as to match trailing white space on the first line as
489well as possibly handling a two-character newline sequence.
490.sp
491There is a limit to the number of lines that can be matched, imposed by the way
492that \fBpcre2grep\fP buffers the input file as it scans it. With a sufficiently
493large processing buffer, this should not be a problem, but the \fB-M\fP option
494does not work when input is read line by line (see \fB--line-buffered\fP.)
495.TP
496\fB-m\fP \fInumber\fP, \fB--max-count\fP=\fInumber\fP
497Stop processing after finding \fInumber\fP matching lines, or non-matching
498lines if \fB-v\fP is also set. Any trailing context lines are output after the
499final match. In multiline mode, each multiline match counts as just one line
500for this purpose. If this limit is reached when reading the standard input from
501a regular file, the file is left positioned just after the last matching line.
502If \fB-c\fP is also set, the count that is output is never greater than
503\fInumber\fP. This option has no effect if used with \fB-L\fP, \fB-l\fP, or
504\fB-q\fP, or when just checking for a match in a binary file.
505.TP
506\fB--match-limit\fP=\fInumber\fP
507Processing some regular expression patterns may take a very long time to search
508for all possible matching strings. Others may require a very large amount of
509memory. There are three options that set resource limits for matching.
510.sp
511The \fB--match-limit\fP option provides a means of limiting computing resource
512usage when processing patterns that are not going to match, but which have a
513very large number of possibilities in their search trees. The classic example
514is a pattern that uses nested unlimited repeats. Internally, PCRE2 has a
515counter that is incremented each time around its main processing loop. If the
516value set by \fB--match-limit\fP is reached, an error occurs.
517.sp
518The \fB--heap-limit\fP option specifies, as a number of kibibytes (units of
5191024 bytes), the amount of heap memory that may be used for matching. Heap
520memory is needed only if matching the pattern requires a significant number of
521nested backtracking points to be remembered. This parameter can be set to zero
522to forbid the use of heap memory altogether.
523.sp
524The \fB--depth-limit\fP option limits the depth of nested backtracking points,
525which indirectly limits the amount of memory that is used. The amount of memory
526needed for each backtracking point depends on the number of capturing
527parentheses in the pattern, so the amount of memory that is used before this
528limit acts varies from pattern to pattern. This limit is of use only if it is
529set smaller than \fB--match-limit\fP.
530.sp
531There are no short forms for these options. The default limits can be set
532when the PCRE2 library is compiled; if they are not specified, the defaults
533are very large and so effectively unlimited.
534.TP
535\fB--max-buffer-size\fP=\fInumber\fP
536This limits the expansion of the processing buffer, whose initial size can be
537set by \fB--buffer-size\fP. The maximum buffer size is silently forced to be no
538smaller than the starting buffer size.
539.TP
540\fB-N\fP \fInewline-type\fP, \fB--newline\fP=\fInewline-type\fP
541Six different conventions for indicating the ends of lines in scanned files are
542supported. For example:
543.sp
544 pcre2grep -N CRLF 'some pattern' <file>
545.sp
546The newline type may be specified in upper, lower, or mixed case. If the
547newline type is NUL, lines are separated by binary zero characters. The other
548types are the single-character sequences CR (carriage return) and LF
549(linefeed), the two-character sequence CRLF, an "anycrlf" type, which
550recognizes any of the preceding three types, and an "any" type, for which any
551Unicode line ending sequence is assumed to end a line. The Unicode sequences
552are the three just mentioned, plus VT (vertical tab, U+000B), FF (form feed,
553U+000C), NEL (next line, U+0085), LS (line separator, U+2028), and PS
554(paragraph separator, U+2029).
555.sp
556When the PCRE2 library is built, a default line-ending sequence is specified.
557This is normally the standard sequence for the operating system. Unless
558otherwise specified by this option, \fBpcre2grep\fP uses the library's default.
559.sp
560This option makes it possible to use \fBpcre2grep\fP to scan files that have
561come from other environments without having to modify their line endings. If
562the data that is being scanned does not agree with the convention set by this
563option, \fBpcre2grep\fP may behave in strange ways. Note that this option does
564not apply to files specified by the \fB-f\fP, \fB--exclude-from\fP, or
565\fB--include-from\fP options, which are expected to use the operating system's
566standard newline sequence.
567.TP
568\fB-n\fP, \fB--line-number\fP
569Precede each output line by its line number in the file, followed by a colon
570for matching lines or a hyphen for context lines. If the file name is also
571being output, it precedes the line number. When the \fB-M\fP option causes a
572pattern to match more than one line, only the first is preceded by its line
573number. This option is forced if \fB--line-offsets\fP is used.
574.TP
575\fB--no-jit\fP
576If the PCRE2 library is built with support for just-in-time compiling (which
577speeds up matching), \fBpcre2grep\fP automatically makes use of this, unless it
578was explicitly disabled at build time. This option can be used to disable the
579use of JIT at run time. It is provided for testing and working round problems.
580It should never be needed in normal use.
581.TP
582\fB-O\fP \fItext\fP, \fB--output\fP=\fItext\fP
583When there is a match, instead of outputting the line that matched, output just
584the text specified in this option, followed by an operating-system standard
585newline. In this mode, no context is shown. That is, the \fB-A\fP, \fB-B\fP,
586and \fB-C\fP options are ignored. The \fB--newline\fP option has no effect on
587this option, which is mutually exclusive with \fB--only-matching\fP,
588\fB--file-offsets\fP, and \fB--line-offsets\fP. However, like
589\fB--only-matching\fP, if there is more than one match in a line, each of them
590causes a line of output.
591.sp
592Escape sequences starting with a dollar character may be used to insert the
593contents of the matched part of the line and/or captured substrings into the
594text.
595.sp
596$<digits> or ${<digits>} is replaced by the captured substring of the given
597decimal number; zero substitutes the whole match. If the number is greater than
598the number of capturing substrings, or if the capture is unset, the replacement
599is empty.
600.sp
601$a is replaced by bell; $b by backspace; $e by escape; $f by form feed; $n by
602newline; $r by carriage return; $t by tab; $v by vertical tab.
603.sp
604$o<digits> or $o{<digits>} is replaced by the character whose code point is the
605given octal number. In the first form, up to three octal digits are processed.
606When more digits are needed in Unicode mode to specify a wide character, the
607second form must be used.
608.sp
609$x<digits> or $x{<digits>} is replaced by the character represented by the
610given hexadecimal number. In the first form, up to two hexadecimal digits are
611processed. When more digits are needed in Unicode mode to specify a wide
612character, the second form must be used.
613.sp
614Any other character is substituted by itself. In particular, $$ is replaced by
615a single dollar.
616.TP
617\fB-o\fP, \fB--only-matching\fP
618Show only the part of the line that matched a pattern instead of the whole
619line. In this mode, no context is shown. That is, the \fB-A\fP, \fB-B\fP, and
620\fB-C\fP options are ignored. If there is more than one match in a line, each
621of them is shown separately, on a separate line of output. If \fB-o\fP is
622combined with \fB-v\fP (invert the sense of the match to find non-matching
623lines), no output is generated, but the return code is set appropriately. If
624the matched portion of the line is empty, nothing is output unless the file
625name or line number are being printed, in which case they are shown on an
626otherwise empty line. This option is mutually exclusive with \fB--output\fP,
627\fB--file-offsets\fP and \fB--line-offsets\fP.
628.TP
629\fB-o\fP\fInumber\fP, \fB--only-matching\fP=\fInumber\fP
630Show only the part of the line that matched the capturing parentheses of the
631given number. Up to 50 capturing parentheses are supported by default. This
632limit can be changed via the \fB--om-capture\fP option. A pattern may contain
633any number of capturing parentheses, but only those whose number is within the
634limit can be accessed by \fB-o\fP. An error occurs if the number specified by
635\fB-o\fP is greater than the limit.
636.sp
637-o0 is the same as \fB-o\fP without a number. Because these options can be
638given without an argument (see above), if an argument is present, it must be
639given in the same shell item, for example, -o3 or --only-matching=2. The
640comments given for the non-argument case above also apply to this option. If
641the specified capturing parentheses do not exist in the pattern, or were not
642set in the match, nothing is output unless the file name or line number are
643being output.
644.sp
645If this option is given multiple times, multiple substrings are output for each
646match, in the order the options are given, and all on one line. For example,
647-o3 -o1 -o3 causes the substrings matched by capturing parentheses 3 and 1 and
648then 3 again to be output. By default, there is no separator (but see the next
649but one option).
650.TP
651\fB--om-capture\fP=\fInumber\fP
652Set the number of capturing parentheses that can be accessed by \fB-o\fP. The
653default is 50.
654.TP
655\fB--om-separator\fP=\fItext\fP
656Specify a separating string for multiple occurrences of \fB-o\fP. The default
657is an empty string. Separating strings are never coloured.
658.TP
659\fB-q\fP, \fB--quiet\fP
660Work quietly, that is, display nothing except error messages. The exit
661status indicates whether or not any matches were found.
662.TP
663\fB-r\fP, \fB--recursive\fP
664If any given path is a directory, recursively scan the files it contains,
665taking note of any \fB--include\fP and \fB--exclude\fP settings. By default, a
666directory is read as a normal file; in some operating systems this gives an
667immediate end-of-file. This option is a shorthand for setting the \fB-d\fP
668option to "recurse".
669.TP
670\fB--recursion-limit\fP=\fInumber\fP
671This is an obsolete synonym for \fB--depth-limit\fP. See \fB--match-limit\fP
672above for details.
673.TP
674\fB-s\fP, \fB--no-messages\fP
675Suppress error messages about non-existent or unreadable files. Such files are
676quietly skipped. However, the return code is still 2, even if matches were
677found in other files.
678.TP
679\fB-t\fP, \fB--total-count\fP
680This option is useful when scanning more than one file. If used on its own,
681\fB-t\fP suppresses all output except for a grand total number of matching
682lines (or non-matching lines if \fB-v\fP is used) in all the files. If \fB-t\fP
683is used with \fB-c\fP, a grand total is output except when the previous output
684is just one line. In other words, it is not output when just one file's count
685is listed. If file names are being output, the grand total is preceded by
686"TOTAL:". Otherwise, it appears as just another number. The \fB-t\fP option is
687ignored when used with \fB-L\fP (list files without matches), because the grand
688total would always be zero.
689.TP
690\fB-u\fP, \fB--utf\fP
691Operate in UTF-8 mode. This option is available only if PCRE2 has been compiled
692with UTF-8 support. All patterns (including those for any \fB--exclude\fP and
693\fB--include\fP options) and all lines that are scanned must be valid strings
694of UTF-8 characters. If an invalid UTF-8 string is encountered, an error
695occurs.
696.TP
697\fB-U\fP, \fB--utf-allow-invalid\fP
698As \fB--utf\fP, but in addition subject lines may contain invalid UTF-8 code
699unit sequences. These can never form part of any pattern match. Patterns
700themselves, however, must still be valid UTF-8 strings. This facility allows
701valid UTF-8 strings to be sought within arbitrary byte sequences in executable
702or other binary files. For more details about matching in non-valid UTF-8
703strings, see the
704.\" HREF
705\fBpcre2unicode\fP(3)
706.\"
707documentation.
708.TP
709\fB-V\fP, \fB--version\fP
710Write the version numbers of \fBpcre2grep\fP and the PCRE2 library to the
711standard output and then exit. Anything else on the command line is
712ignored.
713.TP
714\fB-v\fP, \fB--invert-match\fP
715Invert the sense of the match, so that lines which do \fInot\fP match any of
716the patterns are the ones that are found. When this option is set, options such
717as \fB--only-matching\fP and \fB--output\fP, which specify parts of a match
718that are to be output, are ignored.
719.TP
720\fB-w\fP, \fB--word-regex\fP, \fB--word-regexp\fP
721Force the patterns only to match "words". That is, there must be a word
722boundary at the start and end of each matched string. This is equivalent to
723having "\eb(?:" at the start of each pattern, and ")\eb" at the end. This
724option applies only to the patterns that are matched against the contents of
725files; it does not apply to patterns specified by any of the \fB--include\fP or
726\fB--exclude\fP options.
727.TP
728\fB-x\fP, \fB--line-regex\fP, \fB--line-regexp\fP
729Force the patterns to start matching only at the beginnings of lines, and in
730addition, require them to match entire lines. In multiline mode the match may
731be more than one line. This is equivalent to having "^(?:" at the start of each
732pattern and ")$" at the end. This option applies only to the patterns that are
733matched against the contents of files; it does not apply to patterns specified
734by any of the \fB--include\fP or \fB--exclude\fP options.
735.
736.
737.SH "ENVIRONMENT VARIABLES"
738.rs
739.sp
740The environment variables \fBLC_ALL\fP and \fBLC_CTYPE\fP are examined, in that
741order, for a locale. The first one that is set is used. This can be overridden
742by the \fB--locale\fP option. If no locale is set, the PCRE2 library's default
743(usually the "C" locale) is used.
744.
745.
746.SH "NEWLINES"
747.rs
748.sp
749The \fB-N\fP (\fB--newline\fP) option allows \fBpcre2grep\fP to scan files with
750newline conventions that differ from the default. This option affects only the
751way scanned files are processed. It does not affect the interpretation of files
752specified by the \fB-f\fP, \fB--file-list\fP, \fB--exclude-from\fP, or
753\fB--include-from\fP options.
754.P
755Any parts of the scanned input files that are written to the standard output
756are copied with whatever newline sequences they have in the input. However, if
757the final line of a file is output, and it does not end with a newline
758sequence, a newline sequence is added. If the newline setting is CR, LF, CRLF
759or NUL, that line ending is output; for the other settings (ANYCRLF or ANY) a
760single NL is used.
761.P
762The newline setting does not affect the way in which \fBpcre2grep\fP writes
763newlines in informational messages to the standard output and error streams.
764Under Windows, the standard output is set to be binary, so that "\er\en" at the
765ends of output lines that are copied from the input is not converted to
766"\er\er\en" by the C I/O library. This means that any messages written to the
767standard output must end with "\er\en". For all other operating systems, and
768for all messages to the standard error stream, "\en" is used.
769.
770.
771.SH "OPTIONS COMPATIBILITY"
772.rs
773.sp
774Many of the short and long forms of \fBpcre2grep\fP's options are the same
775as in the GNU \fBgrep\fP program. Any long option of the form
776\fB--xxx-regexp\fP (GNU terminology) is also available as \fB--xxx-regex\fP
777(PCRE2 terminology). However, the \fB--depth-limit\fP, \fB--file-list\fP,
778\fB--file-offsets\fP, \fB--heap-limit\fP, \fB--include-dir\fP,
779\fB--line-offsets\fP, \fB--locale\fP, \fB--match-limit\fP, \fB-M\fP,
780\fB--multiline\fP, \fB-N\fP, \fB--newline\fP, \fB--om-separator\fP,
781\fB--output\fP, \fB-u\fP, \fB--utf\fP, \fB-U\fP, and \fB--utf-allow-invalid\fP
782options are specific to \fBpcre2grep\fP, as is the use of the
783\fB--only-matching\fP option with a capturing parentheses number.
784.P
785Although most of the common options work the same way, a few are different in
786\fBpcre2grep\fP. For example, the \fB--include\fP option's argument is a glob
787for GNU \fBgrep\fP, but a regular expression for \fBpcre2grep\fP. If both the
788\fB-c\fP and \fB-l\fP options are given, GNU grep lists only file names,
789without counts, but \fBpcre2grep\fP gives the counts as well.
790.
791.
792.SH "OPTIONS WITH DATA"
793.rs
794.sp
795There are four different ways in which an option with data can be specified.
796If a short form option is used, the data may follow immediately, or (with one
797exception) in the next command line item. For example:
798.sp
799 -f/some/file
800 -f /some/file
801.sp
802The exception is the \fB-o\fP option, which may appear with or without data.
803Because of this, if data is present, it must follow immediately in the same
804item, for example -o3.
805.P
806If a long form option is used, the data may appear in the same command line
807item, separated by an equals character, or (with two exceptions) it may appear
808in the next command line item. For example:
809.sp
810 --file=/some/file
811 --file /some/file
812.sp
813Note, however, that if you want to supply a file name beginning with ~ as data
814in a shell command, and have the shell expand ~ to a home directory, you must
815separate the file name from the option, because the shell does not treat ~
816specially unless it is at the start of an item.
817.P
818The exceptions to the above are the \fB--colour\fP (or \fB--color\fP) and
819\fB--only-matching\fP options, for which the data is optional. If one of these
820options does have data, it must be given in the first form, using an equals
821character. Otherwise \fBpcre2grep\fP will assume that it has no data.
822.
823.
824.SH "USING PCRE2'S CALLOUT FACILITY"
825.rs
826.sp
827\fBpcre2grep\fP has, by default, support for calling external programs or
828scripts or echoing specific strings during matching by making use of PCRE2's
829callout facility. However, this support can be completely or partially disabled
830when \fBpcre2grep\fP is built. You can find out whether your binary has support
831for callouts by running it with the \fB--help\fP option. If callout support is
832completely disabled, all callouts in patterns are ignored by \fBpcre2grep\fP.
833If the facility is partially disabled, calling external programs is not
834supported, and callouts that request it are ignored.
835.P
836A callout in a PCRE2 pattern is of the form (?C<arg>) where the argument is
837either a number or a quoted string (see the
838.\" HREF
839\fBpcre2callout\fP
840.\"
841documentation for details). Numbered callouts are ignored by \fBpcre2grep\fP;
842only callouts with string arguments are useful.
843.
844.
845.SS "Echoing a specific string"
846.rs
847.sp
848Starting the callout string with a pipe character invokes an echoing facility
849that avoids calling an external program or script. This facility is always
850available, provided that callouts were not completely disabled when
851\fBpcre2grep\fP was built. The rest of the callout string is processed as a
852zero-terminated string, which means it should not contain any internal binary
853zeros. It is written to the output, having first been passed through the same
854escape processing as text from the \fB--output\fP (\fB-O\fP) option (see
855above). However, $0 cannot be used to insert a matched substring because the
856match is still in progress. Instead, the single character '0' is inserted. Any
857syntax errors in the string (for example, a dollar not followed by another
858character) causes the callout to be ignored. No terminator is added to the
859output string, so if you want a newline, you must include it explicitly using
860the escape $n. For example:
861.sp
862 pcre2grep '(.)(..(.))(?C"|[$1] [$2] [$3]$n")' <some file>
863.sp
864Matching continues normally after the string is output. If you want to see only
865the callout output but not any output from an actual match, you should end the
866pattern with (*FAIL).
867.
868.
869.SS "Calling external programs or scripts"
870.rs
871.sp
872This facility can be independently disabled when \fBpcre2grep\fP is built. It
873is supported for Windows, where a call to \fB_spawnvp()\fP is used, for VMS,
874where \fBlib$spawn()\fP is used, and for any Unix-like environment where
875\fBfork()\fP and \fBexecv()\fP are available.
876.P
877If the callout string does not start with a pipe (vertical bar) character, it
878is parsed into a list of substrings separated by pipe characters. The first
879substring must be an executable name, with the following substrings specifying
880arguments:
881.sp
882 executable_name|arg1|arg2|...
883.sp
884Any substring (including the executable name) may contain escape sequences
885started by a dollar character. These are the same as for the \fB--output\fP
886(\fB-O\fP) option documented above, except that $0 cannot insert the matched
887string because the match is still in progress. Instead, the character '0'
888is inserted. If you need a literal dollar or pipe character in any
889substring, use $$ or $| respectively. Here is an example:
890.sp
891 echo -e "abcde\en12345" | pcre2grep \e
892 '(?x)(.)(..(.))
893 (?C"/bin/echo|Arg1: [$1] [$2] [$3]|Arg2: $|${1}$| ($4)")()' -
894.sp
895 Output:
896.sp
897 Arg1: [a] [bcd] [d] Arg2: |a| ()
898 abcde
899 Arg1: [1] [234] [4] Arg2: |1| ()
900 12345
901.sp
902The parameters for the system call that is used to run the program or script
903are zero-terminated strings. This means that binary zero characters in the
904callout argument will cause premature termination of their substrings, and
905therefore should not be present. Any syntax errors in the string (for example,
906a dollar not followed by another character) causes the callout to be ignored.
907If running the program fails for any reason (including the non-existence of the
908executable), a local matching failure occurs and the matcher backtracks in the
909normal way.
910.
911.
912.SH "MATCHING ERRORS"
913.rs
914.sp
915It is possible to supply a regular expression that takes a very long time to
916fail to match certain lines. Such patterns normally involve nested indefinite
917repeats, for example: (a+)*\ed when matched against a line of a's with no final
918digit. The PCRE2 matching function has a resource limit that causes it to abort
919in these circumstances. If this happens, \fBpcre2grep\fP outputs an error
920message and the line that caused the problem to the standard error stream. If
921there are more than 20 such errors, \fBpcre2grep\fP gives up.
922.P
923The \fB--match-limit\fP option of \fBpcre2grep\fP can be used to set the
924overall resource limit. There are also other limits that affect the amount of
925memory used during matching; see the discussion of \fB--heap-limit\fP and
926\fB--depth-limit\fP above.
927.
928.
929.SH DIAGNOSTICS
930.rs
931.sp
932Exit status is 0 if any matches were found, 1 if no matches were found, and 2
933for syntax errors, overlong lines, non-existent or inaccessible files (even if
934matches were found in other files) or too many matching errors. Using the
935\fB-s\fP option to suppress error messages about inaccessible files does not
936affect the return code.
937.P
938When run under VMS, the return code is placed in the symbol PCRE2GREP_RC
939because VMS does not distinguish between exit(0) and exit(1).
940.
941.
942.SH "SEE ALSO"
943.rs
944.sp
945\fBpcre2pattern\fP(3), \fBpcre2syntax\fP(3), \fBpcre2callout\fP(3),
946\fBpcre2unicode\fP(3).
947.
948.
949.SH AUTHOR
950.rs
951.sp
952.nf
953Philip Hazel
954Retired from University Computing Service
955Cambridge, England.
956.fi
957.
958.
959.SH REVISION
960.rs
961.sp
962.nf
963Last updated: 31 August 2021
964Copyright (c) 1997-2021 University of Cambridge.
965.fi