Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

GREP(1)

NAME

grep, egrep, fgrep − search a file for a pattern

USAGE

grep [ options ] expression [ files ]

egrep [ options ] [ expression ] [ files ]

fgrep [ options ] [ strings ] [ files ]

DESCRIPTION

Commands of the grep family search the input files (standard input default) for lines matching a pattern.  Normally, each line found is copied to the standard output.  Patterns used by grep are limited regular expressions in the style of ex (1), using a compact nondeterministic algorithm.  Those used by egrep are full regular expressions, using a fast deterministic algorithm that sometimes needs exponential space. Finally, the patterns used by fgrep are fixed strings, fast and compact.

In all cases, the filename is output if there is more than one input file.  Be careful about using the dollar sign ($), asterisk (*), left sqare bracket ([), caret (^), pipe (|), backslash (\), and left and right parentheses in expression, because they are also meaningful to the Shell. It is safest to quote the entire expression argument. 

Fgrep searches for lines that contain one of the strings separated by newlines. 

Egrep accepts regular expressions.  In the following description, character excludes newline:

A backslash (\) followed by a single character other than newline matches that character. 

A caret (^) matches the beginning of a line. 

A dollar sign ($) matches the end of a line. 

A period matches any character. 

A single character not otherwise having a special meaning matches that character. 

A string enclosed in brackets ([]) matches any single character from the string. Ranges of ASCII character codes may be abbreviated as in a−z0−9.  A right square bracket (]) may occur only as the first character of the string.  A literal dash (−) must be placed where it cannot be mistaken as a range indicator. 

A regular expression followed by an asterisk (*) matches a sequence of zero or more matches of the regular expression.  A regular expression followed by a plus sign (+) matches a sequence of one or more matches of the regular expression.  A regular expression followed by a question mark (?) matches a sequence of zero or one matches of the regular expression. 

Two regular expressions concatenated match a match of the first followed by a match of the second. 

Two regular expressions separated by a pipe character (|) or new-line match either a match for the first or a match for the second. 

A regular expression enclosed in parentheses matches a match for the regular expression. 

The order of precedence of operators at the same parenthesis level is as follows: [], *+?, concatenation, |, and newline.

OPTIONS

−v Print all lines but those matching. 

−x Print only lines exactly matched (fgrep only). 

−c Print only a count of matching lines. 

−i Ignore upper- and lowercase distinction during comparisons (grep and fgrep only). 

−l List only the names of files with matching lines.  Separate the names with newlines. 

−n Precede each line by its relative line number in the file. 

−b Precede each line by the block number (1024 bytes per block) on which it was found.  This option helps to locate disk block numbers by context. 

−s Suppress the error messages produced for nonexistent or unreadable files (grep only). 

−w Search for the expression as a word, i.e., as if it is surrounded by the characters < and >.  See ex (1) for further details.  This option applies to grep only. 

−e expression Same as a simple expression argument, but useful when the expression begins with a dash (−). 

−f file Take the regular expression (egrep) or strings list (fgrep) from the file.

CAUTIONS

Lines are limited to 256 characters; longer lines are truncated. 

DIAGNOSTICS

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

RELATED INFORMATION

ex (1), sed (1), sh (1). 

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026