Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ fgrep(1) — SunOS 2.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

vi(1)

ex(1)

ex(1)

ed(1)

ed(1)

sed(1)

awk(1)

sh(1)

GREP(1)  —  USER COMMANDS

NAME

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

SYNOPSIS

grep [ −v ] [ −c ] [ −l ] [ −n ] [ −b ] [ −i ] [ −s ] [ −h ] [ −w ] [ −eexpression ] expression

[ filename ... ]

egrep [ −v ] [ −c ] [ −l ] [ −n ] [ −b ] [ −s ] [ −h ] [ −eexpression ] [ −ffile ] [ expression ]

[ filename ] ... ]

fgrep [ −v ] [ −x ] [ −c ] [ −l ] [ −n ] [ −b ] [ −i ] [ −s ] [ −h ] [ −eexpression ] [ −ffile ]

[ strings ] [ filename ] ... ]

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.  Grep patterns are limited regular expressions in the style of ed(1). Egrep patterns are full regular expressions including alternation.  Fgrep searches for lines that contain one of the (newline-separated) strings.  Fgrep patterns are fixed strings — no regular expression metacharacters are supported. 

In general, egrep is the fastest of these programs. 

Take care when using the characters $ ∗ [ ^ | ( ) and \ in the expression as these characters are also meaningful to the Shell.  Enclose the entire expression argument in single quotes ´ ´ if you need any of the above special characters in the expression.

When any of the grep utilities is applied to more than one input file, the name of the file is displayed preceding each line which matches the pattern.  The filename is not displayed when processing a single file, so if you actually want the filename to appear, use /dev/null as a second file in the list. 

OPTIONS

−v Invert the search to only display lines that do not match. 

−x Display only those lines which match exactly — that is, only lines which match in their entirety (fgrep only). 

−c Display a count of matching lines. 

−l List the names of files with matching lines (once) separated by newlines. 

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

−b Precede each line by the block number on which it was found.  This is sometimes useful in locating disk block numbers by context. 

−i Ignore the case of letters in making comparisons — that is, upper and lower case are considered identical.  This applies to grep and fgrep only. 

−s Work silently, that is, display nothing except error messages.  This is useful for checking the error status. 

−h Do not display filenames. 

−w search for the expression as a word as if surrounded by ‘\<’ and ‘\>’, see ex(1). grep only.

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

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

REGULAR EXPRESSIONS

In the following description ‘character’ excludes newline:

\Is an escape character: \ followed by any single character other than newline matches that character. 

^Anchored match: matches the beginning of a line. 

$Anchored match: matches the end of a line. 

.(period) matches any character. 

cWhere c is any single character not otherwise endowed with special meaning matches that character. 

[string]
Character class: match any single character from string.  Ranges of ASCII character codes may be abbreviated as in ‘a−z0−9’.  A ] may occur only as the first character of the string.  A literal − must be placed where it can’t be mistaken as a range indicator.  A ^ (circumflex) character immediately after the open bracket negates the sense of the character class, that is, the pattern matches any character except those in the character class. 

∗Closure: a regular expression followed by an ∗ (asterisk) matches a sequence of 0 or more matches of the regular expression. 

+Closure: a regular expression followed by a + (plus) matches a sequence of 1 or more matches of the regular expression. 

?Closure: a regular expression followed by a ? (question mark) matches a sequence of 0 or 1 matches of the regular expression. 

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

|Alternation: two regular expressions separated by | or newline match either a match for the first or a match for the second (egrep only). 

()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 [ ] (character classes), then ∗ + ? (closures), then concatenation, then | (alternation) and newline. 

EXAMPLES

Search a file for a fixed string using fgrep:

tutorial% fgrep  intro  /usr/man/man3/∗.3∗

Look for character classes using grep:

tutorial% grep  ’[1-8]([CJMSNX])’  /usr/man/man1/∗.1

Look for alternative patterns using egrep:

tutorial% egrep  ’(Sally|Fred) (Smith|Jones|Parker)’  telephone.list

To get the filename displayed when only processing a single file, use /dev/null as the second file in the list:

tutorial% grep  ’Sally Parker’  telephone.list  /dev/null

SEE ALSO

vi(1)visual display-oriented editor based on ex(1)
ex(1)line-oriented text editor based on ed(1)
ed(1)primitive line-oriented text editor
sed(1)stream editor
awk(1)pattern scanning and text processing language
sh(1)Bourne Shell

DIAGNOSTICS

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

BUGS

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

Ideally there should be only one grep, but for historical reasons there are three different versions each with a slightly different set of options and syntaxes. 

Sun Release 2.0  —  Last change: 12 February 1985

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