MAN page from CentOS 6 pcre-7.8-4.el6.x86_64.rpm
Section: User Commands (1)Index
pcregrep - a grep with Perl-compatible regular expressions.
SYNOPSISpcregrep [options] [long options] [pattern] [path1 path2 ...]
pcregrep searches files for character patterns, in the same way as othergrep commands do, but it uses the PCRE regular expression library to supportpatterns that are compatible with the regular expressions of Perl 5. Seepcrepattern(3)for a full description of syntax and semantics of the regular expressionsthat PCRE supports.Patterns, whether supplied on the command line or in a separate file, are givenwithout delimiters. For example:
pcregrep Thursday /etc/motd
If you attempt to use delimiters (for example, by surrounding a pattern withslashes, as is common in Perl scripts), they are interpreted as part of thepattern. Quotes can of course be used to delimit patterns on the command linebecause they are interpreted by the shell, and indeed they are required if apattern contains white space or shell metacharacters.The first argument that follows any option settings is treated as the singlepattern to be matched when neither -e nor -f is present.Conversely, when one or both of these options are used to specify patterns, allarguments are treated as path names. At least one of -e, -f, or anargument pattern must be provided.If no files are specified, pcregrep reads the standard input. Thestandard input can also be referenced by a name consisting of a single hyphen.For example:
pcregrep some-pattern /file1 - /file3
By default, each line that matches a pattern is copied to the standardoutput, and if there is more than one file, the file name is output at thestart of each line, followed by a colon. However, there are options that canchange how pcregrep behaves. In particular, the -M option makes itpossible to search for patterns that span line boundaries. What defines a lineboundary is controlled by the -N (--newline) option.Patterns are limited to 8K or BUFSIZ characters, whichever is the greater.BUFSIZ is defined in <stdio.h>. When there is more than one pattern(specified by the use of -e and/or -f), each pattern is applied toeach line in the order in which they are defined, except that all the -epatterns are tried before the -f patterns. As soon as one pattern matches(or fails to match when -v is used), no further patterns are considered.When --only-matching, --file-offsets, or --line-offsetsis used, the output is the part of the line that matched (either shownliterally, or as an offset). In this case, scanning resumes immediatelyfollowing the match, so that further matches on the same line can be found.If there are multiple patterns, they are all tried on the remainder of theline. However, patterns that follow the one that matched are not tried on theearlier part of the line.If the LC_ALL or LC_CTYPE environment variable is set,pcregrep uses the value to set a locale when calling the PCRE library.The --locale option can be used to override this.
SUPPORT FOR COMPRESSED FILES
It is possible to compile pcregrep so that it uses libz orlibbz2 to read files whose names end in .gz or .bz2,respectively. You can find out whether your binary has support for one or bothof these file types by running it with the --help option. If theappropriate support is not present, files are treated as plain text. Thestandard input is always so treated.
- This terminate the list of options. It is useful if the next item on thecommand line starts with a hyphen but is not an option. This allows for theprocessing of patterns and filenames that start with hyphens.
- -A number, --after-context=number
- Output number lines of context after each matching line. If filenamesand/or line numbers are being output, a hyphen separator is used instead of acolon for the context lines. A line containing "--" is output between eachgroup of lines, unless they are in fact contiguous in the input file. The valueof number is expected to be relatively small. However, pcregrepguarantees to have up to 8K of following text available for context output.
- -B number, --before-context=number
- Output number lines of context before each matching line. If filenamesand/or line numbers are being output, a hyphen separator is used instead of acolon for the context lines. A line containing "--" is output between eachgroup of lines, unless they are in fact contiguous in the input file. The valueof number is expected to be relatively small. However, pcregrepguarantees to have up to 8K of preceding text available for context output.
- -C number, --context=number
- Output number lines of context both before and after each matching line.This is equivalent to setting both -A and -B to the same value.
- -c, --count
- Do not output individual lines; instead just output a count of the number oflines that would otherwise have been output. If several files are given, acount is output for each of them. In this mode, the -A, -B, and-C options are ignored.
- --colour, --color
- If this option is given without any data, it is equivalent to "--colour=auto".If data is required, it must be given in the same shell item, separated by anequals sign.
- --colour=value, --color=value
- This option specifies under what circumstances the part of a line that matcheda pattern should be coloured in the output. The value may be "never" (thedefault), "always", or "auto". In the latter case, colouring happens only ifthe standard output is connected to a terminal. The colour can be specified bysetting the environment variable PCREGREP_COLOUR or PCREGREP_COLOR. The valueof this variable should be a string of two numbers, separated by a semicolon.They are copied directly into the control string for setting colour on aterminal, so it is your responsibility to ensure that they make sense. Ifneither of the environment variables is set, the default is "1;31", which givesred.
- -D action, --devices=action
- If an input path is not a regular file or a directory, "action" specifies howit is to be processed. Valid values are "read" (the default) or "skip"(silently skip the path).
- -d action, --directories=action
- If an input path is a directory, "action" specifies how it is to be processed.Valid values are "read" (the default), "recurse" (equivalent to the -roption), or "skip" (silently skip the path). In the default case, directoriesare read as if they were ordinary files. In some operating systems the effectof reading a directory like this is an immediate end-of-file.
- -e pattern, --regex=pattern, --regexp=pattern
- Specify a pattern to be matched. This option can be used multiple times inorder to specify several patterns. It can also be used as a way of specifying asingle pattern that starts with a hyphen. When -e is used, no argumentpattern is taken from the command line; all arguments are treated as filenames. There is an overall maximum of 100 patterns. They are applied to eachline in the order in which they are defined until one matches (or fails tomatch if -v is used). If -f is used with -e, the command linepatterns are matched first, followed by the patterns from the file, independentof the order in which these options are specified. Note that multiple use of-e is not the same as a single pattern with alternatives. For example,X|Y finds the first character in a line that is X or Y, whereas if the twopatterns are given separately, pcregrep finds X if it is present, even ifit follows Y in the line. It finds Y only if there is no X in the line. Thisreally matters only if you are using -o to show the part(s) of the linethat matched.
- When pcregrep is searching the files in a directory as a consequence ofthe -r (recursive search) option, any regular files whose names match thepattern are excluded. Subdirectories are not excluded by this option; they aresearched recursively, subject to the --exclude_dir and--include_dir options. The pattern is a PCRE regular expression, and ismatched against the final component of the file name (not the entire path). Ifa file name matches both --include and --exclude, it is excluded.There is no short form for this option.
- When pcregrep is searching the contents of a directory as a consequenceof the -r (recursive search) option, any subdirectories whose names matchthe pattern are excluded. (Note that the --exclude option does not affectsubdirectories.) The pattern is a PCRE regular expression, and is matchedagainst the final component of the name (not the entire path). If asubdirectory name matches both --include_dir and --exclude_dir, itis excluded. There is no short form for this option.
- -F, --fixed-strings
- Interpret each pattern as a list of fixed strings, separated by newlines,instead of as a regular expression. The -w (match as a word) and -x(match whole line) options can be used with -F. They apply to each of thefixed strings. A line is selected if any of the fixed strings are found in it(subject to -w or -x, if present).
- -f filename, --file=filename
- Read a number of patterns from the file, one per line, and match them againsteach line of input. A data line is output if any of the patterns match it. Thefilename can be given as "-" to refer to the standard input. When -f isused, patterns specified on the command line using -e may also bepresent; they are tested before the file's patterns. However, no other patternis taken from the command line; all arguments are treated as file names. Thereis an overall maximum of 100 patterns. Trailing white space is removed fromeach line, and blank lines are ignored. An empty file contains no patterns andtherefore matches nothing. See also the comments about multiple patterns versusa single pattern with alternatives in the description of -e above.
- Instead of showing lines or parts of lines that match, show each match as anoffset from the start of the file and a length, separated by a comma. In thismode, no context is shown. That is, the -A, -B, and -Coptions are ignored. If there is more than one match in a line, each of them isshown separately. This option is mutually exclusive with --line-offsetsand --only-matching.
- -H, --with-filename
- Force the inclusion of the filename at the start of output lines when searchinga single file. By default, the filename is not shown in this case. For matchinglines, the filename is followed by a colon and a space; for context lines, ahyphen separator is used. If a line number is also being output, it follows thefile name without a space.
- -h, --no-filename
- Suppress the output filenames when searching multiple files. By default,filenames are shown when multiple files are searched. For matching lines, thefilename is followed by a colon and a space; for context lines, a hyphenseparator is used. If a line number is also being output, it follows the filename without a space.
- Output a help message, giving brief details of the command options and filetype support, and then exit.
- -i, --ignore-case
- Ignore upper/lower case distinctions during comparisons.
- When pcregrep is searching the files in a directory as a consequence ofthe -r (recursive search) option, only those regular files whose namesmatch the pattern are included. Subdirectories are always included and searchedrecursively, subject to the --include_dir and --exclude_diroptions. The pattern is a PCRE regular expression, and is matched against thefinal component of the file name (not the entire path). If a file name matchesboth --include and --exclude, it is excluded. There is no shortform for this option.
- When pcregrep is searching the contents of a directory as a consequenceof the -r (recursive search) option, only those subdirectories whosenames match the pattern are included. (Note that the --include optiondoes not affect subdirectories.) The pattern is a PCRE regular expression, andis matched against the final component of the name (not the entire path). If asubdirectory name matches both --include_dir and --exclude_dir, itis excluded. There is no short form for this option.
- -L, --files-without-match
- Instead of outputting lines from the files, just output the names of the filesthat do not contain any lines that would have been output. Each file name isoutput once, on a separate line.
- -l, --files-with-matches
- Instead of outputting lines from the files, just output the names of the filescontaining lines that would have been output. Each file name is outputonce, on a separate line. Searching stops as soon as a matching line is foundin a file.
- This option supplies a name to be used for the standard input when file namesare being output. If not supplied, "(standard input)" is used. There is noshort form for this option.
- Instead of showing lines or parts of lines that match, show each match as aline number, the offset from the start of the line, and a length. The linenumber is terminated by a colon (as usual; see the -n option), and theoffset and length are separated by a comma. In this mode, no context is shown.That is, the -A, -B, and -C options are ignored. If there ismore than one match in a line, each of them is shown separately. This option ismutually exclusive with --file-offsets and --only-matching.
- This option specifies a locale to be used for pattern matching. It overridesthe value in the LC_ALL or LC_CTYPE environment variables. If nolocale is specified, the PCRE library's default (usually the "C" locale) isused. There is no short form for this option.
- -M, --multiline
- Allow patterns to match more than one line. When this option is given, patternsmay usefully contain literal newline characters and internal occurrences of ^and $ characters. The output for any one match may consist of more than oneline. When this option is set, the PCRE library is called in "multiline" mode.There is a limit to the number of lines that can be matched, imposed by the waythat pcregrep buffers the input file as it scans it. However,pcregrep ensures that at least 8K characters or the rest of the document(whichever is the shorter) are available for forward matching, and similarlythe previous 8K characters (or all the previous characters, if fewer than 8K)are guaranteed to be available for lookbehind assertions.
- -N newline-type, --newline=newline-type
- The PCRE library supports five different conventions for indicatingthe ends of lines. They are the single-character sequences CR (carriage return)and LF (linefeed), the two-character sequence CRLF, an "anycrlf" convention,which recognizes any of the preceding three types, and an "any" convention, inwhich any Unicode line ending sequence is assumed to end a line. The Unicodesequences are the three just mentioned, plus VT (vertical tab, U+000B), FF(form feed, U+000C), NEL (next line, U+0085), LS (line separator, U+2028), andPS (paragraph separator, U+2029).
When the PCRE library is built, a default line-ending sequence is specified.This is normally the standard sequence for the operating system. Unlessotherwise specified by this option, pcregrep uses the library's default.The possible values for this option are CR, LF, CRLF, ANYCRLF, or ANY. Thismakes it possible to use pcregrep on files that have come from otherenvironments without having to modify their line endings. If the data that isbeing scanned does not agree with the convention set by this option,pcregrep may behave in strange ways.
- -n, --line-number
- Precede each output line by its line number in the file, followed by a colonand a space for matching lines or a hyphen and a space for context lines. Ifthe filename is also being output, it precedes the line number. This option isforced if --line-offsets is used.
- -o, --only-matching
- Show only the part of the line that matched a pattern. In this mode, nocontext is shown. That is, the -A, -B, and -C options areignored. If there is more than one match in a line, each of them is shownseparately. If -o is combined with -v (invert the sense of thematch to find non-matching lines), no output is generated, but the return codeis set appropriately. This option is mutually exclusive with--file-offsets and --line-offsets.
- -q, --quiet
- Work quietly, that is, display nothing except error messages. The exitstatus indicates whether or not any matches were found.
- -r, --recursive
- If any given path is a directory, recursively scan the files it contains,taking note of any --include and --exclude settings. By default, adirectory is read as a normal file; in some operating systems this gives animmediate end-of-file. This option is a shorthand for setting the -doption to "recurse".
- -s, --no-messages
- Suppress error messages about non-existent or unreadable files. Such files arequietly skipped. However, the return code is still 2, even if matches werefound in other files.
- -u, --utf-8
- Operate in UTF-8 mode. This option is available only if PCRE has been compiledwith UTF-8 support. Both patterns and subject lines must be valid strings ofUTF-8 characters.
- -V, --version
- Write the version numbers of pcregrep and the PCRE library that is beingused to the standard error stream.
- -v, --invert-match
- Invert the sense of the match, so that lines which do not match any ofthe patterns are the ones that are found.
- -w, --word-regex, --word-regexp
- Force the patterns to match only whole words. This is equivalent to having \bat the start and end of the pattern.
- -x, --line-regex, --line-regexp
- Force the patterns to be anchored (each must start matching at the beginning ofa line) and in addition, require them to match entire lines. This isequivalent to having ^ and $ characters at the start and end of eachalternative branch in every pattern.
The environment variables LC_ALL and LC_CTYPE are examined, in thatorder, for a locale. The first one that is set is used. This can be overriddenby the --locale option. If no locale is set, the PCRE library's default(usually the "C" locale) is used.
The -N (--newline) option allows pcregrep to scan files withdifferent newline conventions from the default. However, the setting of thisoption does not affect the way in which pcregrep writes information tothe standard error and output streams. It uses the string "\n" in Cprintf() calls to indicate newlines, relying on the C I/O library toconvert this to an appropriate sequence if the output is sent to a file.
The majority of short and long forms of pcregrep's options are the sameas in the GNU grep program. Any long option of the form--xxx-regexp (GNU terminology) is also available as --xxx-regex(PCRE terminology). However, the --locale, -M, --multiline,-u, and --utf-8 options are specific to pcregrep.
OPTIONS WITH DATA
There are four different ways in which an option with data can be specified.If a short form option is used, the data may follow immediately, or in the nextcommand line item. For example:
If a long form option is used, the data may appear in the same command lineitem, separated by an equals character, or (with one exception) it may appearin the next command line item. For example:
Note, however, that if you want to supply a file name beginning with ~ as datain a shell command, and have the shell expand ~ to a home directory, you mustseparate the file name from the option, because the shell does not treat ~specially unless it is at the start of an item.The exception to the above is the --colour (or --color) option,for which the data is optional. If this option does have data, it must be givenin the first form, using an equals character. Otherwise it will be assumed thatit has no data.
It is possible to supply a regular expression that takes a very long time tofail to match certain lines. Such patterns normally involve nested indefiniterepeats, for example: (a+)*\d when matched against a line of a's with no finaldigit. The PCRE matching function has a resource limit that causes it to abortin these circumstances. If this happens, pcregrep outputs an errormessage and the line that caused the problem to the standard error stream. Ifthere are more than 20 such errors, pcregrep gives up.
Exit status is 0 if any matches were found, 1 if no matches were found, and 2for syntax errors and non-existent or inaccessible files (even if matches werefound in other files) or too many matching errors. Using the -s option tosuppress error messages about inaccessible files does not affect the returncode.
Philip HazelUniversity Computing ServiceCambridge CB2 3QH, England.
Last updated: 08 March 2008Copyright (c) 1997-2008 University of Cambridge.
- SUPPORT FOR COMPRESSED FILES
- ENVIRONMENT VARIABLES
- OPTIONS COMPATIBILITY
- OPTIONS WITH DATA
- MATCHING ERRORS
- SEE ALSO
This document was created byman2html,using the manual pages.