MAN page from Fedora 7 pcre-7.0-2.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 on the command line because they areinterpreted by the shell, and indeed they are required if a pattern containswhite 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 the pattern is copied to the standardoutput, and if there is more than one file, the file name is output at thestart of each line. However, there are options that can change howpcregrep behaves. In particular, the -M option makes it possible tosearch for patterns that span line boundaries. What defines a line boundary iscontrolled by the -N (--newline) option.Patterns are limited to 8K or BUFSIZ characters, whichever is the greater.BUFSIZ is defined in <stdio.h>.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.
- 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 canbe used multiple times in order to specify several patterns. It can also beused as a way of specifying a single pattern that starts with a hyphen. When-e is used, no argument pattern is taken from the command line; allarguments are treated as file names. There is an overall maximum of 100patterns. They are applied to each line in the order in which they are defineduntil one matches (or fails to match if -v is used). If -f is usedwith -e, the command line patterns are matched first, followed by thepatterns from the file, independent of the order in which these options arespecified. Note that multiple use of -e is not the same as a singlepattern with alternatives. For example, X|Y finds the first character in a linethat is X or Y, whereas if the two patterns are given separately,pcregrep finds X if it is present, even if it follows Y in the line. Itfinds Y only if there is no X in the line. This really matters only if you areusing -o to show the portion of the line that matched.
- When pcregrep is searching the files in a directory as a consequence ofthe -r (recursive search) option, any files whose names match the patternare excluded. The pattern is a PCRE regular expression. If a file name matchesboth --include and --exclude, it is excluded. There is no shortform 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.
- -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 brief help message and 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 files whose names match thepattern are included. The pattern is a PCRE regular expression. If a file namematches both --include and --exclude, it is excluded. There is noshort 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.
- 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 four different conventions for indicatingthe ends of lines. They are the single-character sequences CR (carriage return)and LF (linefeed), the two-character sequence CRLF, 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(formfeed, U+000C), NEL (next line, U+0085), LS (line separator, U+2028), andPS (paragraph separator, U+0029).
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, or ANY. This makes itpossible to use pcregrep on files that have come from other environmentswithout having to modify their line endings. If the data that is being scanneddoes not agree with the convention set by this option, pcregrep maybehave 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.
- -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.
- -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 inacessible files (even if matches werefound in other files) or too many matching errors. Using the -s option tosuppress error messages about inaccessble files does not affect the returncode.
University Computing Service
Cambridge CB2 3QH, England.Last updated: 29 November 2006
Copyright (c) 1997-2006 University of Cambridge.
- ENVIRONMENT VARIABLES
- OPTIONS COMPATIBILITY
- OPTIONS WITH DATA
- MATCHING ERRORS
- SEE ALSO
This document was created byman2html,using the manual pages.