github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1095 lines
61 KiB

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