pcre2grep.txt 58 KB

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