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