GREP (1)


grep - search a file for a pattern


Environment Variables
See Also


/usr/5bin/grep [-bchilnrRsvwz] pattern [file ...]

/usr/5bin/posix/grep [-E|-F] -e pattern_list ... [-f pattern_file] [-c|-l|-q] [-bhinrRsvwxz] [file ...]

/usr/5bin/posix/grep [-E|-F] -f pattern_file [-e pattern_list ...] [-c|-l|-q] [-bhinrRsvwxz] [file ...]

/usr/5bin/posix/grep [-E|-F] [-c|-l|-q] [-bhinrRsvwxz] pattern_list [file ...]


The grep command searches the lines of the specified files (or of standard input) for occurrences of the regular expression pattern. The default behavior is to print each matching line to standard output.

The /usr/5bin/grep command accepts one pattern that is treated as a simple regular expression; it uses a compact nondeterministic algorithm.

The /usr/5bin/posix/grep command uses basic regular expressions by default and accepts a newline-separated list of patterns as described for the -e option below. It uses a deterministic algorithm with moderate space requirements for most expressions; backreferences, word delimiters, and multi-character collating elements cause a nondeterministic algorithm to be used.

/usr/5bin/s42/grep and /usr/5bin/posix2001/grep are identical to /usr/5bin/posix/grep.

See the description of ed(1) for the specifications of simple and basic regular expressions.

Care should be taken when using the characters $ * [ ^ | ? ' " ( ) and \ in the expression as they are also meaningful to the Shell. It is safest to enclose the entire expression argument in single quotes ' '.

Both /usr/5bin/grep and /usr/5bin/posix/grep accept the following options:
-b Each line is preceded by the block number on which it was found. This is sometimes useful in locating disk block numbers by context. Block numbers start with 0.
-c Only a count of matching lines is printed.
-h Normally, the name of each input file is printed before a match if there is more that one input file. When this option is present, no file names are printed.
-i Upper- and lowercase differences are ignored when searching matches.
-l The names of files with matching lines are listed (once) separated by newlines.
-n Each line is preceded by its line number in the file. Line numbers start with 1.
-s Error messages for nonexistent or unreadable files are suppressed.
-v All lines but those matching are printed.
The following options are supported by /usr/5bin/posix/grep only:
-e pattern_list
  Specifies one or more patterns, separated by newline characters. A line is selected if one or more of the specified patterns are found.
-E All patterns are interpreted as extended regular expressions as described in egrep(1).
-f pattern_file
  One or more patterns, separated by newline characters, are read from pattern_file.
-F All patterns are interpreted as fixed strings, as with fgrep(1).
-q Do not write anything to standard output.
-x Consider only lines consisting of the pattern as a whole, like a regular expression surrounded by ^ and $.
The following options are supported as extensions:
-r With this option given, grep does not directly search in each given file that is a directory, but descends it recursively and scans each regular file found below it. Device files are ignored. Symbolic links are followed.
-R Operates recursively as with the -r option, but does not follow symbolic links that point to directories unless if they are explicitly specified as arguments.
-w Searches for the patterns treated as words, as if they were surrounded by '\< \>'. Only available if neither the -E nor the -F option are also supplied.
-z If an input file is found to be compressed with compress(1), gzip(1), or bzip2(1), the appropriate compression program is started, and grep searches for the pattern in its output.


  See locale(7).
  Affects the collation order for range expressions, equivalence classes, and collation symbols in basic regular expressions.
  Determines the mapping of bytes to characters in both simple and basic regular expressions, the availability and composition of character classes in basic regular expressions, and the case mapping for the -i option.


ed(1), egrep(1), fgrep(1), sed(1), locale(7)


Exit status is 0 if any matches are found, 1 if none, 2 for syntax errors or inaccessible files.


If a line contains a NUL character, only matches up to this character are found (unless /usr/5bin/posix/grep is used with the -F option). The entire matching line will be printed.

The LC_COLLATE variable has currently no effect. Ranges in bracket expressions are ordered as byte values in single-byte locales and as wide character values in multibyte locales; equivalence classes match the given character only, and multi-character collating elements are not available.

The options supported by /usr/5bin/posix/grep that are not accepted by /usr/5bin/grep can easily be replaced by portable constructs: Use egrep instead of -E, fgrep instead of -F. Use egrep if you need the -e or -f option, use only one of them and that only once; if necessary, use text processing tools to generate a single expression list before. Redirect standard output to /dev/null for -q (the possible speedup with -q is never worth human time spent with porting scripts), and use the '^' and '$' meta-characters instead of -x.

Heirloom Toolchest GREP (1) 8/14/05
Generated by a modified version of manServer 1.07 from grep.1 using man macros.