1PCRE2GREP(1)                General Commands Manual               PCRE2GREP(1)
2
3
4

NAME

6       pcre2grep - a grep with Perl-compatible regular expressions.
7

SYNOPSIS

9       pcre2grep [options] [long options] [pattern] [path1 path2 ...]
10

DESCRIPTION

12
13       pcre2grep  searches  files  for  character patterns, in the same way as
14       other grep commands do, but it uses the PCRE2  regular  expression  li‐
15       brary  to support patterns that are compatible with the regular expres‐
16       sions of Perl 5. See pcre2syntax(3) for a  quick-reference  summary  of
17       pattern syntax, or pcre2pattern(3) for a full description of the syntax
18       and semantics of the regular expressions that PCRE2 supports.
19
20       Patterns, whether supplied on the command line or in a  separate  file,
21       are given without delimiters. For example:
22
23         pcre2grep Thursday /etc/motd
24
25       If you attempt to use delimiters (for example, by surrounding a pattern
26       with slashes, as is common in Perl scripts), they  are  interpreted  as
27       part  of  the pattern. Quotes can of course be used to delimit patterns
28       on the command line because they are interpreted by the shell, and  in‐
29       deed  quotes  are  required  if a pattern contains white space or shell
30       metacharacters.
31
32       The first argument that follows any option settings is treated  as  the
33       single  pattern  to be matched when neither -e nor -f is present.  Con‐
34       versely, when one or both of these options are  used  to  specify  pat‐
35       terns, all arguments are treated as path names. At least one of -e, -f,
36       or an argument pattern must be provided.
37
38       If no files are specified, pcre2grep  reads  the  standard  input.  The
39       standard  input can also be referenced by a name consisting of a single
40       hyphen.  For example:
41
42         pcre2grep some-pattern file1 - file3
43
44       Input files are searched line by  line.  By  default,  each  line  that
45       matches  a  pattern  is  copied to the standard output, and if there is
46       more than one file, the file name is output at the start of each  line,
47       followed  by  a  colon.  However, there are options that can change how
48       pcre2grep behaves. In particular, the -M option makes  it  possible  to
49       search  for  strings  that  span  line  boundaries. What defines a line
50       boundary is controlled by the -N (--newline) option.
51
52       The amount of memory used for buffering files that are being scanned is
53       controlled  by  parameters  that  can  be  set by the --buffer-size and
54       --max-buffer-size options. The first of these sets the size  of  buffer
55       that  is obtained at the start of processing. If an input file contains
56       very long lines, a larger buffer may be needed; this is handled by  au‐
57       tomatically  extending  the buffer, up to the limit specified by --max-
58       buffer-size. The default values for these parameters can  be  set  when
59       pcre2grep  is  built;  if nothing is specified, the defaults are set to
60       20KiB and 1MiB respectively. An error occurs if a line is too long  and
61       the buffer can no longer be expanded.
62
63       The  block  of  memory that is actually used is three times the "buffer
64       size", to allow for buffering "before" and "after" lines. If the buffer
65       size  is too small, fewer than requested "before" and "after" lines may
66       be output.
67
68       Patterns can be no longer than 8KiB or BUFSIZ bytes, whichever  is  the
69       greater.   BUFSIZ  is defined in <stdio.h>. When there is more than one
70       pattern (specified by the use of -e and/or -f), each pattern is applied
71       to  each  line  in the order in which they are defined, except that all
72       the -e patterns are tried before the -f patterns.
73
74       By default, as soon as one pattern matches a line, no further  patterns
75       are considered. However, if --colour (or --color) is used to colour the
76       matching substrings, or if --only-matching, --file-offsets, or  --line-
77       offsets  is  used to output only the part of the line that matched (ei‐
78       ther shown literally, or as an offset),  scanning  resumes  immediately
79       following  the  match,  so that further matches on the same line can be
80       found. If there are multiple patterns, they are all tried  on  the  re‐
81       mainder  of the line, but patterns that follow the one that matched are
82       not tried on the earlier matched part of the line.
83
84       This behaviour means that the order  in  which  multiple  patterns  are
85       specified  can affect the output when one of the above options is used.
86       This is no longer the same behaviour as GNU grep, which now manages  to
87       display  earlier  matches  for  later  patterns (as long as there is no
88       overlap).
89
90       Patterns that can match an empty string are accepted, but empty  string
91       matches   are  never  recognized.  An  example  is  the  pattern  "(su‐
92       per)?(man)?", in which all components are optional. This pattern  finds
93       all  occurrences  of  both  "super"  and "man"; the output differs from
94       matching with "super|man" when only the matching substrings  are  being
95       shown.
96
97       If  the  LC_ALL or LC_CTYPE environment variable is set, pcre2grep uses
98       the value to set a locale when calling the PCRE2 library.  The --locale
99       option can be used to override this.
100

SUPPORT FOR COMPRESSED FILES

102
103       It  is  possible to compile pcre2grep so that it uses libz or libbz2 to
104       read compressed files whose names end in .gz or .bz2, respectively. You
105       can  find out whether your pcre2grep binary has support for one or both
106       of these file types by running it with the --help option. If the appro‐
107       priate support is not present, all files are treated as plain text. The
108       standard input is always so treated. When input is  from  a  compressed
109       .gz or .bz2 file, the --line-buffered option is ignored.
110

BINARY FILES

112
113       By  default,  a  file that contains a binary zero byte within the first
114       1024 bytes is identified as a binary file, and is processed  specially.
115       However,  if  the  newline  type is specified as NUL, that is, the line
116       terminator is a binary zero, the test for a binary file is not applied.
117       See  the  --binary-files  option for a means of changing the way binary
118       files are handled.
119

BINARY ZEROS IN PATTERNS

121
122       Patterns passed from the command line are strings that  are  terminated
123       by  a  binary zero, so cannot contain internal zeros. However, patterns
124       that are read from a file via the -f option may contain binary zeros.
125

OPTIONS

127
128       The order in which some of the options appear can  affect  the  output.
129       For  example,  both  the  -H and -l options affect the printing of file
130       names. Whichever comes later in the command line will be the  one  that
131       takes  effect.  Similarly,  except  where  noted below, if an option is
132       given twice, the later setting is used. Numerical  values  for  options
133       may  be  followed  by  K  or  M,  to  signify multiplication by 1024 or
134       1024*1024 respectively.
135
136       --        This terminates the list of options. It is useful if the next
137                 item  on  the command line starts with a hyphen but is not an
138                 option. This allows for the processing of patterns  and  file
139                 names that start with hyphens.
140
141       -A number, --after-context=number
142                 Output  up  to  number  lines  of context after each matching
143                 line. Fewer lines are output if the next match or the end  of
144                 the  file  is  reached,  or if the processing buffer size has
145                 been set too small. If file names and/or line numbers are be‐
146                 ing output, a hyphen separator is used instead of a colon for
147                 the context lines. A line containing "--" is  output  between
148                 each  group  of  lines, unless they are in fact contiguous in
149                 the input file. The value of number is expected to  be  rela‐
150                 tively small. When -c is used, -A is ignored.
151
152       -a, --text
153                 Treat  binary  files as text. This is equivalent to --binary-
154                 files=text.
155
156       --allow-lookaround-bsk
157                 PCRE2 now forbids the use of \K in lookarounds by default, in
158                 line  with  Perl.   This  option  causes pcre2grep to set the
159                 PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK option, which  enables  this
160                 somewhat dangerous usage.
161
162       -B number, --before-context=number
163                 Output  up  to  number  lines of context before each matching
164                 line. Fewer lines are output if the  previous  match  or  the
165                 start  of the file is within number lines, or if the process‐
166                 ing buffer size has been set too small. If file names  and/or
167                 line numbers are being output, a hyphen separator is used in‐
168                 stead of a colon for the context  lines.  A  line  containing
169                 "--"  is  output between each group of lines, unless they are
170                 in fact contiguous in the input file. The value of number  is
171                 expected  to  be relatively small. When -c is used, -B is ig‐
172                 nored.
173
174       --binary-files=word
175                 Specify how binary files are to be processed. If the word  is
176                 "binary"  (the default), pattern matching is performed on bi‐
177                 nary files, but  the  only  output  is  "Binary  file  <name>
178                 matches"  when a match succeeds. If the word is "text", which
179                 is equivalent to the -a or --text option,  binary  files  are
180                 processed  in  the  same way as any other file. In this case,
181                 when a match succeeds, the  output  may  be  binary  garbage,
182                 which  can  have  nasty effects if sent to a terminal. If the
183                 word is "without-match", which is equivalent to  the  -I  op‐
184                 tion, binary files are not processed at all; they are assumed
185                 not to be of interest and are  skipped  without  causing  any
186                 output or affecting the return code.
187
188       --buffer-size=number
189                 Set  the  parameter that controls how much memory is obtained
190                 at the start of processing for buffering files that are being
191                 scanned. See also --max-buffer-size below.
192
193       -C number, --context=number
194                 Output  number  lines  of  context both before and after each
195                 matching line.  This is equivalent to setting both -A and  -B
196                 to the same value.
197
198       -c, --count
199                 Do  not  output  lines from the files that are being scanned;
200                 instead output the number  of  lines  that  would  have  been
201                 shown, either because they matched, or, if -v is set, because
202                 they failed to match. By default, this count is  exactly  the
203                 same  as the number of lines that would have been output, but
204                 if the -M (multiline) option is used (without -v), there  may
205                 be  more suppressed lines than the count (that is, the number
206                 of matches).
207
208                 If no lines are selected, the number zero is output. If  sev‐
209                 eral  files are are being scanned, a count is output for each
210                 of them and the -t option can be used to cause a total to  be
211                 output  at  the end. However, if the --files-with-matches op‐
212                 tion is also used, only those files whose counts are  greater
213                 than zero are listed. When -c is used, the -A, -B, and -C op‐
214                 tions are ignored.
215
216       --colour, --color
217                 If this option is given without any data, it is equivalent to
218                 "--colour=auto".   If  data  is required, it must be given in
219                 the same shell item, separated by an equals sign.
220
221       --colour=value, --color=value
222                 This option specifies under what circumstances the parts of a
223                 line that matched a pattern should be coloured in the output.
224                 By default, the output is not coloured. The value  (which  is
225                 optional,  see above) may be "never", "always", or "auto". In
226                 the latter case, colouring happens only if the standard  out‐
227                 put  is connected to a terminal. More resources are used when
228                 colouring is enabled, because pcre2grep has to search for all
229                 possible  matches in a line, not just one, in order to colour
230                 them all.
231
232                 The colour that is used can be specified by  setting  one  of
233                 the  environment variables PCRE2GREP_COLOUR, PCRE2GREP_COLOR,
234                 PCREGREP_COLOUR, or PCREGREP_COLOR, which are checked in that
235                 order.  If  none  of  these  are  set,  pcre2grep  looks  for
236                 GREP_COLORS or GREP_COLOR (in that order). The value  of  the
237                 variable  should  be  a string of two numbers, separated by a
238                 semicolon, except in the  case  of  GREP_COLORS,  which  must
239                 start with "ms=" or "mt=" followed by two semicolon-separated
240                 colours, terminated by the end of the string or by  a  colon.
241                 If  GREP_COLORS  does not start with "ms=" or "mt=" it is ig‐
242                 nored, and GREP_COLOR is checked.
243
244                 If the string obtained from one of the above  variables  con‐
245                 tains any characters other than semicolon or digits, the set‐
246                 ting is ignored and the default colour is used. The string is
247                 copied directly into the control string for setting colour on
248                 a terminal, so it is your responsibility to ensure  that  the
249                 values  make  sense.  If  no relevant environment variable is
250                 set, the default is "1;31", which gives red.
251
252       -D action, --devices=action
253                 If an input path is not a regular file or a  directory,  "ac‐
254                 tion"  specifies  how it is to be processed. Valid values are
255                 "read" (the default) or "skip" (silently skip the path).
256
257       -d action, --directories=action
258                 If an input path is a directory, "action" specifies how it is
259                 to  be  processed.   Valid  values are "read" (the default in
260                 non-Windows environments, for compatibility with  GNU  grep),
261                 "recurse"  (equivalent to the -r option), or "skip" (silently
262                 skip the path, the default in Windows environments).  In  the
263                 "read"  case,  directories  are read as if they were ordinary
264                 files. In some operating systems the effect of reading a  di‐
265                 rectory  like  this is an immediate end-of-file; in others it
266                 may provoke an error.
267
268       --depth-limit=number
269                 See --match-limit below.
270
271       -e pattern, --regex=pattern, --regexp=pattern
272                 Specify a pattern to be matched. This option can be used mul‐
273                 tiple times in order to specify several patterns. It can also
274                 be used as a way of specifying a single pattern  that  starts
275                 with  a hyphen. When -e is used, no argument pattern is taken
276                 from the command line; all  arguments  are  treated  as  file
277                 names.  There is no limit to the number of patterns. They are
278                 applied to each line in the order in which they  are  defined
279                 until one matches.
280
281                 If  -f is used with -e, the command line patterns are matched
282                 first, followed by the patterns from the file(s), independent
283                 of  the order in which these options are specified. Note that
284                 multiple use of -e is not the same as a single  pattern  with
285                 alternatives. For example, X|Y finds the first character in a
286                 line that is X or Y, whereas if the two  patterns  are  given
287                 separately, with X first, pcre2grep finds X if it is present,
288                 even if it follows Y in the line. It finds Y only if there is
289                 no  X  in  the line. This matters only if you are using -o or
290                 --colo(u)r to show the part(s) of the line that matched.
291
292       --exclude=pattern
293                 Files (but not directories) whose names match the pattern are
294                 skipped  without  being processed. This applies to all files,
295                 whether listed on the command  line,  obtained  from  --file-
296                 list, or by scanning a directory. The pattern is a PCRE2 reg‐
297                 ular expression, and is matched against the  final  component
298                 of the file name, not the entire path. The -F, -w, and -x op‐
299                 tions do not apply to this pattern. The option may  be  given
300                 any number of times in order to specify multiple patterns. If
301                 a file name matches both an --include and an  --exclude  pat‐
302                 tern, it is excluded. There is no short form for this option.
303
304       --exclude-from=filename
305                 Treat  each  non-empty  line  of  the file as the data for an
306                 --exclude option. What constitutes a newline when reading the
307                 file  is the operating system's default. The --newline option
308                 has no effect on this option. This option may be  given  more
309                 than once in order to specify a number of files to read.
310
311       --exclude-dir=pattern
312                 Directories whose names match the pattern are skipped without
313                 being processed, whatever the setting of the --recursive  op‐
314                 tion.  This applies to all directories, whether listed on the
315                 command line, obtained from --file-list,  or  by  scanning  a
316                 parent  directory. The pattern is a PCRE2 regular expression,
317                 and is matched against the final component of  the  directory
318                 name,  not the entire path. The -F, -w, and -x options do not
319                 apply to this pattern. The option may be given any number  of
320                 times  in order to specify more than one pattern. If a direc‐
321                 tory matches both --include-dir and --exclude-dir, it is  ex‐
322                 cluded. There is no short form for this option.
323
324       -F, --fixed-strings
325                 Interpret  each  data-matching  pattern  as  a  list of fixed
326                 strings, separated by newlines, instead of as a  regular  ex‐
327                 pression. What constitutes a newline for this purpose is con‐
328                 trolled by the --newline option. The -w (match as a word) and
329                 -x  (match whole line) options can be used with -F.  They ap‐
330                 ply to each of the fixed strings. A line is selected  if  any
331                 of the fixed strings are found in it (subject to -w or -x, if
332                 present). This option applies only to the patterns  that  are
333                 matched  against  the contents of files; it does not apply to
334                 patterns specified by any of the --include or  --exclude  op‐
335                 tions.
336
337       -f filename, --file=filename
338                 Read  patterns  from  the  file, one per line, and match them
339                 against each line of input. As is the case with  patterns  on
340                 the  command line, no delimiters should be used. What consti‐
341                 tutes a newline when reading the file is the  operating  sys‐
342                 tem's  default interpretation of \n. The --newline option has
343                 no effect on this option. Trailing  white  space  is  removed
344                 from  each  line,  and blank lines are ignored. An empty file
345                 contains no patterns and therefore matches nothing.  Patterns
346                 read  from a file in this way may contain binary zeros, which
347                 are treated as ordinary data characters. See  also  the  com‐
348                 ments  about  multiple  patterns versus a single pattern with
349                 alternatives in the description of -e above.
350
351                 If this option is given more than  once,  all  the  specified
352                 files  are read. A data line is output if any of the patterns
353                 match it. A file name can be given as "-"  to  refer  to  the
354                 standard  input.  When  -f is used, patterns specified on the
355                 command line using -e may also be present;  they  are  tested
356                 before  the  file's  patterns.  However,  no other pattern is
357                 taken from the command line; all arguments are treated as the
358                 names of paths to be searched.
359
360       --file-list=filename
361                 Read  a  list  of  files  and/or  directories  that are to be
362                 scanned from the given file, one per line. What constitutes a
363                 newline  when  reading the file is the operating system's de‐
364                 fault. Trailing white space is removed from  each  line,  and
365                 blank lines are ignored. These paths are processed before any
366                 that are listed on the command line. The  file  name  can  be
367                 given  as  "-"  to refer to the standard input. If --file and
368                 --file-list are both specified  as  "-",  patterns  are  read
369                 first.  This is useful only when the standard input is a ter‐
370                 minal, from which further lines (the list of  files)  can  be
371                 read after an end-of-file indication. If this option is given
372                 more than once, all the specified files are read.
373
374       --file-offsets
375                 Instead of showing lines or parts of lines that  match,  show
376                 each  match  as  an  offset  from the start of the file and a
377                 length, separated by a comma. In this  mode,  no  context  is
378                 shown.  That  is,  the -A, -B, and -C options are ignored. If
379                 there is more than one match in a line, each of them is shown
380                 separately.  This option is mutually exclusive with --output,
381                 --line-offsets, and --only-matching.
382
383       -H, --with-filename
384                 Force the inclusion of the file name at the start  of  output
385                 lines when searching a single file. By default, the file name
386                 is not shown in this case.  For matching lines, the file name
387                 is followed by a colon; for context lines, a hyphen separator
388                 is used. If a line number is also being  output,  it  follows
389                 the  file  name. When the -M option causes a pattern to match
390                 more than one line, only the first is preceded  by  the  file
391                 name.  This  option  overrides any previous -h, -l, or -L op‐
392                 tions.
393
394       -h, --no-filename
395                 Suppress the output file names when searching multiple files.
396                 By  default,  file  names  are  shown when multiple files are
397                 searched. For matching lines, the file name is followed by  a
398                 colon;  for  context lines, a hyphen separator is used.  If a
399                 line number is also being output, it follows the  file  name.
400                 This option overrides any previous -H, -L, or -l options.
401
402       --heap-limit=number
403                 See --match-limit below.
404
405       --help    Output  a  help  message, giving brief details of the command
406                 options and file type support, and then exit.  Anything  else
407                 on the command line is ignored.
408
409       -I        Ignore   binary   files.  This  is  equivalent  to  --binary-
410                 files=without-match.
411
412       -i, --ignore-case
413                 Ignore upper/lower case distinctions during comparisons.
414
415       --include=pattern
416                 If any --include patterns are specified, the only files  that
417                 are processed are those whose names match one of the patterns
418                 and do not match an --exclude pattern. This option  does  not
419                 affect  directories,  but  it  applies  to all files, whether
420                 listed on the command line, obtained from --file-list, or  by
421                 scanning  a directory. The pattern is a PCRE2 regular expres‐
422                 sion, and is matched against the final component of the  file
423                 name,  not the entire path. The -F, -w, and -x options do not
424                 apply to this pattern. The option may be given any number  of
425                 times.  If a file name matches both an --include and an --ex‐
426                 clude pattern, it is excluded.  There is no  short  form  for
427                 this option.
428
429       --include-from=filename
430                 Treat  each  non-empty  line  of  the file as the data for an
431                 --include option. What constitutes a newline for this purpose
432                 is  the  operating system's default. The --newline option has
433                 no effect on this option. This option may be given any number
434                 of times; all the files are read.
435
436       --include-dir=pattern
437                 If  any --include-dir patterns are specified, the only direc‐
438                 tories that are processed are those whose names match one  of
439                 the  patterns and do not match an --exclude-dir pattern. This
440                 applies to all directories, whether  listed  on  the  command
441                 line,  obtained from --file-list, or by scanning a parent di‐
442                 rectory. The pattern is a PCRE2 regular  expression,  and  is
443                 matched  against  the  final component of the directory name,
444                 not the entire path. The -F, -w, and -x options do not  apply
445                 to this pattern. The option may be given any number of times.
446                 If a directory matches both --include-dir and  --exclude-dir,
447                 it is excluded. There is no short form for this option.
448
449       -L, --files-without-match
450                 Instead  of  outputting lines from the files, just output the
451                 names of the files that do not contain any lines  that  would
452                 have  been  output. Each file name is output once, on a sepa‐
453                 rate line. This option overrides any previous -H, -h,  or  -l
454                 options.
455
456       -l, --files-with-matches
457                 Instead  of  outputting lines from the files, just output the
458                 names of the files containing lines that would have been out‐
459                 put.  Each  file  name  is  output  once, on a separate line.
460                 Searching normally stops as soon as a matching line is  found
461                 in  a  file.  However, if the -c (count) option is also used,
462                 matching continues in order to obtain the correct count,  and
463                 those  files  that  have  at least one match are listed along
464                 with their counts. Using this option with -c is a way of sup‐
465                 pressing  the  listing  of  files with no matches that occurs
466                 with -c on its own. This option overrides  any  previous  -H,
467                 -h, or -L options.
468
469       --label=name
470                 This option supplies a name to be used for the standard input
471                 when file names are being output. If not supplied, "(standard
472                 input)" is used. There is no short form for this option.
473
474       --line-buffered
475                 When  this  option is given, non-compressed input is read and
476                 processed line by line, and the output is flushed after  each
477                 write.  By  default,  input  is  read in large chunks, unless
478                 pcre2grep can determine that it is reading from  a  terminal,
479                 which is currently possible only in Unix-like environments or
480                 Windows. Output to terminal is normally automatically flushed
481                 by  the  operating system. This option can be useful when the
482                 input or output is attached to a pipe and  you  do  not  want
483                 pcre2grep  to  buffer up large amounts of data.  However, its
484                 use will affect performance, and the  -M  (multiline)  option
485                 ceases  to  work. When input is from a compressed .gz or .bz2
486                 file, --line-buffered is ignored.
487
488       --line-offsets
489                 Instead of showing lines or parts of lines that  match,  show
490                 each match as a line number, the offset from the start of the
491                 line, and a length. The line number is terminated by a  colon
492                 (as  usual; see the -n option), and the offset and length are
493                 separated by a comma. In this  mode,  no  context  is  shown.
494                 That  is, the -A, -B, and -C options are ignored. If there is
495                 more than one match in a line, each of them  is  shown  sepa‐
496                 rately.  This  option  is  mutually  exclusive with --output,
497                 --file-offsets, and --only-matching.
498
499       --locale=locale-name
500                 This option specifies a locale to be used for pattern  match‐
501                 ing.  It  overrides the value in the LC_ALL or LC_CTYPE envi‐
502                 ronment variables. If no locale is specified, the  PCRE2  li‐
503                 brary's default (usually the "C" locale) is used. There is no
504                 short form for this option.
505
506       -M, --multiline
507                 Allow patterns to match more than one line. When this  option
508                 is set, the PCRE2 library is called in "multiline" mode. This
509                 allows a matched string to extend past the end of a line  and
510                 continue  on one or more subsequent lines. Patterns used with
511                 -M may usefully contain literal newline characters and inter‐
512                 nal  occurrences of ^ and $ characters. The output for a suc‐
513                 cessful match may consist of more than one  line.  The  first
514                 line  is  the  line  in which the match started, and the last
515                 line is the line in which the match  ended.  If  the  matched
516                 string  ends  with a newline sequence, the output ends at the
517                 end of that line.  If -v is set,  none  of  the  lines  in  a
518                 multi-line  match  are output. Once a match has been handled,
519                 scanning restarts at the beginning of the line after the  one
520                 in which the match ended.
521
522                 The  newline  sequence  that separates multiple lines must be
523                 matched as part of the pattern.  For  example,  to  find  the
524                 phrase  "regular  expression" in a file where "regular" might
525                 be at the end of a line and "expression" at the start of  the
526                 next line, you could use this command:
527
528                   pcre2grep -M 'regular\s+expression' <file>
529
530                 The \s escape sequence matches any white space character, in‐
531                 cluding newlines, and is followed by + so as to match  trail‐
532                 ing  white  space  on the first line as well as possibly han‐
533                 dling a two-character newline sequence.
534
535                 There is a limit to the number of lines that can be  matched,
536                 imposed  by  the way that pcre2grep buffers the input file as
537                 it scans it. With a  sufficiently  large  processing  buffer,
538                 this should not be a problem, but the -M option does not work
539                 when input is read line by line (see --line-buffered.)
540
541       -m number, --max-count=number
542                 Stop processing after finding number matching lines, or  non-
543                 matching  lines if -v is also set. Any trailing context lines
544                 are output after the final match.  In  multiline  mode,  each
545                 multiline  match counts as just one line for this purpose. If
546                 this limit is reached when reading the standard input from  a
547                 regular file, the file is left positioned just after the last
548                 matching line.  If -c is also set, the count that  is  output
549                 is  never  greater  than number. This option has no effect if
550                 used with -L, -l, or -q, or when just checking for a match in
551                 a binary file.
552
553       --match-limit=number
554                 Processing  some  regular expression patterns may take a very
555                 long time to search for all possible matching strings. Others
556                 may  require  a  very large amount of memory. There are three
557                 options that set resource limits for matching.
558
559                 The --match-limit option provides a means of limiting comput‐
560                 ing  resource usage when processing patterns that are not go‐
561                 ing to match, but which have a very large number of possibil‐
562                 ities in their search trees. The classic example is a pattern
563                 that uses nested unlimited repeats. Internally, PCRE2  has  a
564                 counter  that  is  incremented each time around its main pro‐
565                 cessing loop. If the value set by --match-limit  is  reached,
566                 an error occurs.
567
568                 The  --heap-limit  option specifies, as a number of kibibytes
569                 (units of 1024 bytes), the amount of heap memory that may  be
570                 used for matching. Heap memory is needed only if matching the
571                 pattern requires a significant number of nested  backtracking
572                 points to be remembered. This parameter can be set to zero to
573                 forbid the use of heap memory altogether.
574
575                 The --depth-limit option limits the  depth  of  nested  back‐
576                 tracking points, which indirectly limits the amount of memory
577                 that is used. The amount of memory needed for each backtrack‐
578                 ing  point  depends on the number of capturing parentheses in
579                 the pattern, so the amount of memory that is used before this
580                 limit  acts  varies from pattern to pattern. This limit is of
581                 use only if it is set smaller than --match-limit.
582
583                 There are no short forms for these options. The default  lim‐
584                 its  can  be  set when the PCRE2 library is compiled; if they
585                 are not specified, the defaults are very large and so  effec‐
586                 tively unlimited.
587
588       --max-buffer-size=number
589                 This  limits  the  expansion  of the processing buffer, whose
590                 initial size can be set by --buffer-size. The maximum  buffer
591                 size  is  silently  forced to be no smaller than the starting
592                 buffer size.
593
594       -N newline-type, --newline=newline-type
595                 Six different conventions for indicating the ends of lines in
596                 scanned files are supported. For example:
597
598                   pcre2grep -N CRLF 'some pattern' <file>
599
600                 The  newline  type may be specified in upper, lower, or mixed
601                 case. If the newline type is NUL, lines are separated by  bi‐
602                 nary  zero characters. The other types are the single-charac‐
603                 ter sequences CR (carriage return)  and  LF  (linefeed),  the
604                 two-character  sequence CRLF, an "anycrlf" type, which recog‐
605                 nizes any of the preceding three types, and  an  "any"  type,
606                 for  which any Unicode line ending sequence is assumed to end
607                 a line. The Unicode sequences are the three  just  mentioned,
608                 plus  VT  (vertical tab, U+000B), FF (form feed, U+000C), NEL
609                 (next line, U+0085), LS  (line  separator,  U+2028),  and  PS
610                 (paragraph separator, U+2029).
611
612                 When  the  PCRE2  library is built, a default line-ending se‐
613                 quence is specified.  This is normally the standard  sequence
614                 for  the operating system. Unless otherwise specified by this
615                 option, pcre2grep uses the library's default.
616
617                 This option makes it possible to use pcre2grep to scan  files
618                 that have come from other environments without having to mod‐
619                 ify their line endings. If the data  that  is  being  scanned
620                 does  not  agree  with  the  convention  set  by this option,
621                 pcre2grep may behave in strange ways. Note that  this  option
622                 does  not apply to files specified by the -f, --exclude-from,
623                 or --include-from options, which are expected to use the  op‐
624                 erating system's standard newline sequence.
625
626       -n, --line-number
627                 Precede each output line by its line number in the file, fol‐
628                 lowed by a colon for matching lines or a hyphen  for  context
629                 lines. If the file name is also being output, it precedes the
630                 line number. When the -M option causes  a  pattern  to  match
631                 more  than  one  line, only the first is preceded by its line
632                 number. This option is forced if --line-offsets is used.
633
634       --no-jit  If the PCRE2 library is built with support  for  just-in-time
635                 compiling (which speeds up matching), pcre2grep automatically
636                 makes use of this, unless it was explicitly disabled at build
637                 time.  This  option  can be used to disable the use of JIT at
638                 run time. It is provided for testing and working round  prob‐
639                 lems.  It should never be needed in normal use.
640
641       -O text, --output=text
642                 When  there  is  a match, instead of outputting the line that
643                 matched, output just the text specified in this option,  fol‐
644                 lowed  by an operating-system standard newline. In this mode,
645                 no context is shown. That is, the -A, -B, and -C options  are
646                 ignored.  The  --newline option has no effect on this option,
647                 which is mutually exclusive with --only-matching, --file-off‐
648                 sets,  and  --line-offsets. However, like --only-matching, if
649                 there is more than one match in a line, each of them causes a
650                 line of output.
651
652                 Escape sequences starting with a dollar character may be used
653                 to insert the contents of the matched part of the line and/or
654                 captured substrings into the text.
655
656                 $<digits>  or  ${<digits>}  is  replaced by the captured sub‐
657                 string of the given  decimal  number;  zero  substitutes  the
658                 whole match. If the number is greater than the number of cap‐
659                 turing substrings, or if the capture is unset,  the  replace‐
660                 ment is empty.
661
662                 $a  is replaced by bell; $b by backspace; $e by escape; $f by
663                 form feed; $n by newline; $r by carriage return; $t  by  tab;
664                 $v by vertical tab.
665
666                 $o<digits> or $o{<digits>} is replaced by the character whose
667                 code point is the given octal number. In the first  form,  up
668                 to  three  octal  digits are processed.  When more digits are
669                 needed in Unicode mode to specify a wide character, the  sec‐
670                 ond form must be used.
671
672                 $x<digits>  or $x{<digits>} is replaced by the character rep‐
673                 resented by the given hexadecimal number. In the first  form,
674                 up  to two hexadecimal digits are processed. When more digits
675                 are needed in Unicode mode to specify a wide  character,  the
676                 second form must be used.
677
678                 Any  other character is substituted by itself. In particular,
679                 $$ is replaced by a single dollar.
680
681       -o, --only-matching
682                 Show only the part of the line that matched a pattern instead
683                 of  the  whole  line. In this mode, no context is shown. That
684                 is, the -A, -B, and -C options are ignored. If there is  more
685                 than  one  match in a line, each of them is shown separately,
686                 on a separate line of output. If -o is combined with -v  (in‐
687                 vert  the  sense of the match to find non-matching lines), no
688                 output is generated, but the return  code  is  set  appropri‐
689                 ately.  If  the matched portion of the line is empty, nothing
690                 is output unless the file  name  or  line  number  are  being
691                 printed,  in  which case they are shown on an otherwise empty
692                 line.  This  option  is  mutually  exclusive  with  --output,
693                 --file-offsets and --line-offsets.
694
695       -onumber, --only-matching=number
696                 Show  only  the  part  of the line that matched the capturing
697                 parentheses of the given number. Up to 50 capturing parenthe‐
698                 ses  are  supported by default. This limit can be changed via
699                 the --om-capture option. A pattern may contain any number  of
700                 capturing  parentheses, but only those whose number is within
701                 the limit can be accessed by -o. An error occurs if the  num‐
702                 ber specified by -o is greater than the limit.
703
704                 -o0 is the same as -o without a number. Because these options
705                 can be given without an argument (see above), if an  argument
706                 is  present, it must be given in the same shell item, for ex‐
707                 ample, -o3 or --only-matching=2. The comments given  for  the
708                 non-argument  case  above  also  apply to this option. If the
709                 specified capturing parentheses do not exist in the  pattern,
710                 or  were  not  set in the match, nothing is output unless the
711                 file name or line number are being output.
712
713                 If this option is given multiple times,  multiple  substrings
714                 are  output  for  each  match,  in  the order the options are
715                 given, and all on one line. For example, -o3 -o1  -o3  causes
716                 the  substrings  matched by capturing parentheses 3 and 1 and
717                 then 3 again to be output. By default, there is no  separator
718                 (but see the next but one option).
719
720       --om-capture=number
721                 Set  the number of capturing parentheses that can be accessed
722                 by -o. The default is 50.
723
724       --om-separator=text
725                 Specify a separating string for multiple occurrences  of  -o.
726                 The  default is an empty string. Separating strings are never
727                 coloured.
728
729       -q, --quiet
730                 Work quietly, that is, display nothing except error messages.
731                 The  exit  status  indicates  whether or not any matches were
732                 found.
733
734       -r, --recursive
735                 If any given path is a directory, recursively scan the  files
736                 it  contains, taking note of any --include and --exclude set‐
737                 tings. By default, a directory is read as a normal  file;  in
738                 some  operating  systems this gives an immediate end-of-file.
739                 This option is a shorthand for setting the -d option to  "re‐
740                 curse".
741
742       --recursion-limit=number
743                 This  is  an obsolete synonym for --depth-limit. See --match-
744                 limit above for details.
745
746       -s, --no-messages
747                 Suppress error  messages  about  non-existent  or  unreadable
748                 files.  Such  files  are quietly skipped. However, the return
749                 code is still 2, even if matches were found in other files.
750
751       -t, --total-count
752                 This option is useful when scanning more than  one  file.  If
753                 used  on its own, -t suppresses all output except for a grand
754                 total number of matching lines (or non-matching lines  if  -v
755                 is used) in all the files. If -t is used with -c, a grand to‐
756                 tal is output except when the previous  output  is  just  one
757                 line.  In  other words, it is not output when just one file's
758                 count is listed. If file names are being  output,  the  grand
759                 total  is preceded by "TOTAL:". Otherwise, it appears as just
760                 another number. The -t option is ignored when  used  with  -L
761                 (list  files  without matches), because the grand total would
762                 always be zero.
763
764       -u, --utf Operate in UTF-8 mode. This option is available only if PCRE2
765                 has been compiled with UTF-8 support. All patterns (including
766                 those for any --exclude and --include options) and all  lines
767                 that  are  scanned must be valid strings of UTF-8 characters.
768                 If an invalid UTF-8 string is encountered, an error occurs.
769
770       -U, --utf-allow-invalid
771                 As --utf, but in addition subject lines may  contain  invalid
772                 UTF-8  code  unit sequences. These can never form part of any
773                 pattern match. Patterns themselves, however,  must  still  be
774                 valid UTF-8 strings. This facility allows valid UTF-8 strings
775                 to be sought within arbitrary byte sequences in executable or
776                 other  binary  files. For more details about matching in non-
777                 valid UTF-8 strings, see the pcre2unicode(3) documentation.
778
779       -V, --version
780                 Write the version numbers of pcre2grep and the PCRE2  library
781                 to  the  standard  output and then exit. Anything else on the
782                 command line is ignored.
783
784       -v, --invert-match
785                 Invert the sense of the match, so that  lines  which  do  not
786                 match  any  of the patterns are the ones that are found. When
787                 this option is  set,  options  such  as  --only-matching  and
788                 --output,  which specify parts of a match that are to be out‐
789                 put, are ignored.
790
791       -w, --word-regex, --word-regexp
792                 Force the patterns only to match "words". That is, there must
793                 be  a  word  boundary  at  the  start and end of each matched
794                 string. This is equivalent to having "\b(?:" at the start  of
795                 each  pattern, and ")\b" at the end. This option applies only
796                 to the patterns that are  matched  against  the  contents  of
797                 files;  it does not apply to patterns specified by any of the
798                 --include or --exclude options.
799
800       -x, --line-regex, --line-regexp
801                 Force the patterns to start matching only at  the  beginnings
802                 of  lines,  and  in  addition,  require  them to match entire
803                 lines. In multiline mode the match may be more than one line.
804                 This is equivalent to having "^(?:" at the start of each pat‐
805                 tern and ")$" at the end. This option  applies  only  to  the
806                 patterns  that  are matched against the contents of files; it
807                 does not apply to patterns specified by any of the  --include
808                 or --exclude options.
809

ENVIRONMENT VARIABLES

811
812       The environment variables LC_ALL and LC_CTYPE are examined, in that or‐
813       der, for a locale. The first one that is set is used. This can be over‐
814       ridden by the --locale option. If no locale is set, the PCRE2 library's
815       default (usually the "C" locale) is used.
816

NEWLINES

818
819       The -N (--newline) option allows pcre2grep to scan files  with  newline
820       conventions  that differ from the default. This option affects only the
821       way scanned files are processed. It does not affect the  interpretation
822       of  files  specified  by  the -f, --file-list, --exclude-from, or --in‐
823       clude-from options.
824
825       Any parts of the scanned input files that are written to  the  standard
826       output  are copied with whatever newline sequences they have in the in‐
827       put. However, if the final line of a file is output, and  it  does  not
828       end  with  a newline sequence, a newline sequence is added. If the new‐
829       line setting is CR, LF, CRLF or NUL, that line ending  is  output;  for
830       the other settings (ANYCRLF or ANY) a single NL is used.
831
832       The  newline  setting does not affect the way in which pcre2grep writes
833       newlines in informational messages to the  standard  output  and  error
834       streams.   Under  Windows,  the standard output is set to be binary, so
835       that "\r\n" at the ends of output lines that are copied from the  input
836       is  not converted to "\r\r\n" by the C I/O library. This means that any
837       messages written to the standard output must end with "\r\n".  For  all
838       other  operating  systems,  and  for all messages to the standard error
839       stream, "\n" is used.
840

OPTIONS COMPATIBILITY

842
843       Many of the short and long forms of pcre2grep's options are the same as
844       in  the GNU grep program. Any long option of the form --xxx-regexp (GNU
845       terminology) is also available as --xxx-regex (PCRE2 terminology). How‐
846       ever,  the  --depth-limit,  --file-list,  --file-offsets, --heap-limit,
847       --include-dir, --line-offsets, --locale,  --match-limit,  -M,  --multi‐
848       line,  -N,  --newline,  --om-separator,  --output,  -u,  --utf, -U, and
849       --utf-allow-invalid options are specific to pcre2grep, as is the use of
850       the --only-matching option with a capturing parentheses number.
851
852       Although  most  of the common options work the same way, a few are dif‐
853       ferent in pcre2grep. For example, the --include option's argument is  a
854       glob  for GNU grep, but a regular expression for pcre2grep. If both the
855       -c and -l options are given, GNU grep lists only  file  names,  without
856       counts, but pcre2grep gives the counts as well.
857

OPTIONS WITH DATA

859
860       There are four different ways in which an option with data can be spec‐
861       ified.  If a short form option is used, the  data  may  follow  immedi‐
862       ately, or (with one exception) in the next command line item. For exam‐
863       ple:
864
865         -f/some/file
866         -f /some/file
867
868       The exception is the -o option, which may appear with or without  data.
869       Because  of this, if data is present, it must follow immediately in the
870       same item, for example -o3.
871
872       If a long form option is used, the data may appear in the same  command
873       line  item,  separated by an equals character, or (with two exceptions)
874       it may appear in the next command line item. For example:
875
876         --file=/some/file
877         --file /some/file
878
879       Note, however, that if you want to supply a file name beginning with  ~
880       as  data  in a shell command, and have the shell expand ~ to a home di‐
881       rectory, you must separate the file name from the option,  because  the
882       shell does not treat ~ specially unless it is at the start of an item.
883
884       The  exceptions  to the above are the --colour (or --color) and --only-
885       matching options, for which the data is optional. If one of  these  op‐
886       tions  does  have  data,  it  must be given in the first form, using an
887       equals character. Otherwise pcre2grep will assume that it has no data.
888

USING PCRE2'S CALLOUT FACILITY

890
891       pcre2grep has, by default, support for  calling  external  programs  or
892       scripts  or  echoing  specific strings during matching by making use of
893       PCRE2's callout facility. However, this support can  be  completely  or
894       partially  disabled  when  pcre2grep is built. You can find out whether
895       your binary has support for callouts by running it with the --help  op‐
896       tion.  If  callout support is completely disabled, all callouts in pat‐
897       terns are ignored by pcre2grep.  If the facility is partially disabled,
898       calling  external  programs is not supported, and callouts that request
899       it are ignored.
900
901       A callout in a PCRE2 pattern is of the form (?C<arg>) where  the  argu‐
902       ment  is either a number or a quoted string (see the pcre2callout docu‐
903       mentation for details). Numbered callouts  are  ignored  by  pcre2grep;
904       only callouts with string arguments are useful.
905
906   Echoing a specific string
907
908       Starting  the  callout  string with a pipe character invokes an echoing
909       facility that avoids calling an external program or script. This facil‐
910       ity  is  always  available,  provided that callouts were not completely
911       disabled when pcre2grep was built. The rest of the  callout  string  is
912       processed  as  a zero-terminated string, which means it should not con‐
913       tain any internal binary zeros. It is written  to  the  output,  having
914       first  been  passed through the same escape processing as text from the
915       --output (-O) option (see above). However, $0 cannot be used to  insert
916       a  matched  substring  because the match is still in progress. Instead,
917       the single character '0' is inserted. Any syntax errors in  the  string
918       (for  example,  a  dollar not followed by another character) causes the
919       callout to be ignored. No terminator is added to the output string,  so
920       if  you want a newline, you must include it explicitly using the escape
921       $n. For example:
922
923         pcre2grep '(.)(..(.))(?C"|[$1] [$2] [$3]$n")' <some file>
924
925       Matching continues normally after the string is output. If you want  to
926       see  only  the  callout output but not any output from an actual match,
927       you should end the pattern with (*FAIL).
928
929   Calling external programs or scripts
930
931       This facility can be independently disabled when pcre2grep is built. It
932       is  supported for Windows, where a call to _spawnvp() is used, for VMS,
933       where lib$spawn() is used, and  for  any  Unix-like  environment  where
934       fork() and execv() are available.
935
936       If the callout string does not start with a pipe (vertical bar) charac‐
937       ter, it is parsed into a list of substrings separated by  pipe  charac‐
938       ters.  The first substring must be an executable name, with the follow‐
939       ing substrings specifying arguments:
940
941         executable_name|arg1|arg2|...
942
943       Any substring (including the executable name) may  contain  escape  se‐
944       quences  started  by  a dollar character. These are the same as for the
945       --output (-O) option documented above, except that $0 cannot insert the
946       matched  string  because  the  match is still in progress. Instead, the
947       character '0' is inserted. If you need a literal dollar or pipe charac‐
948       ter in any substring, use $$ or $| respectively. Here is an example:
949
950         echo -e "abcde\n12345" | pcre2grep \
951           '(?x)(.)(..(.))
952           (?C"/bin/echo|Arg1: [$1] [$2] [$3]|Arg2: $|${1}$| ($4)")()' -
953
954         Output:
955
956           Arg1: [a] [bcd] [d] Arg2: |a| ()
957           abcde
958           Arg1: [1] [234] [4] Arg2: |1| ()
959           12345
960
961       The  parameters  for the system call that is used to run the program or
962       script are zero-terminated strings. This means that binary zero charac‐
963       ters  in the callout argument will cause premature termination of their
964       substrings, and therefore should not be present. Any syntax  errors  in
965       the  string  (for  example, a dollar not followed by another character)
966       causes the callout to be ignored.  If running the program fails for any
967       reason  (including the non-existence of the executable), a local match‐
968       ing failure occurs and the matcher backtracks in the normal way.
969

MATCHING ERRORS

971
972       It is possible to supply a regular expression that takes  a  very  long
973       time  to  fail  to  match certain lines. Such patterns normally involve
974       nested indefinite repeats, for example: (a+)*\d when matched against  a
975       line  of a's with no final digit. The PCRE2 matching function has a re‐
976       source limit that causes it to abort in these  circumstances.  If  this
977       happens,  pcre2grep  outputs  an error message and the line that caused
978       the problem to the standard error stream. If there  are  more  than  20
979       such errors, pcre2grep gives up.
980
981       The  --match-limit  option  of pcre2grep can be used to set the overall
982       resource limit. There are also other limits that affect the  amount  of
983       memory  used  during  matching;  see the discussion of --heap-limit and
984       --depth-limit above.
985

DIAGNOSTICS

987
988       Exit status is 0 if any matches were found, 1 if no matches were found,
989       and  2  for syntax errors, overlong lines, non-existent or inaccessible
990       files (even if matches were found in other files) or too many  matching
991       errors. Using the -s option to suppress error messages about inaccessi‐
992       ble files does not affect the return code.
993
994       When  run  under  VMS,  the  return  code  is  placed  in  the   symbol
995       PCRE2GREP_RC  because  VMS  does  not  distinguish  between exit(0) and
996       exit(1).
997

SEE ALSO

999
1000       pcre2pattern(3), pcre2syntax(3), pcre2callout(3), pcre2unicode(3).
1001

AUTHOR

1003
1004       Philip Hazel
1005       Retired from University Computing Service
1006       Cambridge, England.
1007

REVISION

1009
1010       Last updated: 31 August 2021
1011       Copyright (c) 1997-2021 University of Cambridge.
1012
1013
1014
1015PCRE2 10.38                     31 August 2021                    PCRE2GREP(1)
Impressum