grep: grep Programs
2.4 ‘grep’ Programs
===================
‘grep’ searches the named input files for lines containing a match to
the given patterns. By default, ‘grep’ prints the matching lines. A
file named ‘-’ stands for standard input. If no input is specified,
‘grep’ searches the working directory ‘.’ if given a command-line option
specifying recursion; otherwise, ‘grep’ searches standard input. There
are four major variants of ‘grep’, controlled by the following options.
‘-G’
‘--basic-regexp’
Interpret patterns as basic regular expressions (BREs). This is
the default.
‘-E’
‘--extended-regexp’
Interpret patterns as extended regular expressions (EREs). (‘-E’
is specified by POSIX.)
‘-F’
‘--fixed-strings’
Interpret patterns as fixed strings, not regular expressions.
(‘-F’ is specified by POSIX.)
‘-P’
‘--perl-regexp’
Interpret patterns as Perl-compatible regular expressions (PCREs).
PCRE support is here to stay, but consider this option experimental
when combined with the ‘-z’ (‘--null-data’) option, and note that
‘grep -P’ may warn of unimplemented features. ⇒Other
Options.
For documentation, refer to <https://www.pcre.org/>, with these
caveats:
• ‘\d’ matches only the ten ASCII digits (and ‘\D’ matches the
complement), regardless of locale. Use ‘\p{Nd}’ to also match
non-ASCII digits. (The behavior of ‘\d’ and ‘\D’ is
unspecified after in-regexp directives like ‘(?aD)’.)
• Although PCRE tracks the syntax and semantics of Perl's
regular expressions, the match is not always exact. For
example, Perl evolves and a Perl installation may predate or
postdate the PCRE2 installation on the same host, or their
Unicode versions may differ, or Perl and PCRE2 may disagree
about an obscure construct.
• By default, ‘grep’ applies each regexp to a line at a time, so
the ‘(?s)’ directive (making ‘.’ match line breaks) is
generally ineffective. However, with ‘-z’ (‘--null-data’) it
can work:
$ printf 'a\nb\n' |grep -zP '(?s)a.b'
a
b
But beware: with the ‘-z’ (‘--null-data’) and a file
containing no NUL byte, grep must read the entire file into
memory before processing any of it. Thus, it will exhaust
memory and fail for some large files.