Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

sed(1)

sh(1)

environ(5)

lang(5)

regcomp(3c)

regexp(5)

grep(1)

NAME

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

SYNOPSIS

Plain call with pattern
grep [−E | −F] [−cilnqsvx] pattern [file ...]

Call with (multiple) −e pattern
grep [−E | −F] [−cilnqsvx] −e pattern... [−e pattern ] ... [file ...]

Call with −f file
grep [−E | −F] [−cilnqsvx] [−f pattern_file] [file...]

Obsolescent:
egrep [−cefilnsv] [expression] [file...]

fgrep [−cefilnsvx] [strings] [file...]

DESCRIPTION

grep searches the input text files (standard input default) for lines matching a pattern.  Normally, each line found is copied to the standard output.  grep supports the Basic Regular Expression syntax (see regexp(5)). The −E option (egrep) supports Extended Regular Expression (ERE) syntax (see regexp(5)). The −F option (fgrep) searches for fixed strings using the fast Boyer-Moore string searching algorithm.  −E and −F options treat new-lines embedded in the pattern as alternation characters.  A null expression or string matches every line. 

The forms egrep and fgrep are maintained for backward compatibility; use of −E and −F options is recommended for portability. 

Options

−E Extended regular expressions.  Each pattern specified is a sequence of one or more EREs.  The EREs can be separated by new-line characters or given in separate −e expression options.  A pattern matches an input line if any ERE in the sequence matches the contents of the input line without its trailing new-line character.  The same functionality is obtained by using egrep. 

−F Fixed strings.  Each pattern specified is a sequence of one or more strings.  Strings can be separated by new-line characters or given in separate −e expression options.  A pattern matches an input line if the line contains any of the strings in the sequence.  The same functionality is obtained by using fgrep. 

−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 are calculated by dividing by 512 the number of bytes that have been read from the file and rounding down the result. 

−c Only a count of matching lines is printed. 

−e expression Same as a simple expression argument, but useful when the expression begins with a hyphen (−).  Multiple −e options can be used to specify multiple patterns; an input line is selected if it matches any of the specified patterns. 

−f pattern_file The regular expression (grep and grep −E) or strings list (grep −F) is taken from the pattern_file.

−i Ignore uppercase/lowercase distinctions during comparisons. 

−l Only the names of files with matching lines are listed (once), separated by new-lines.  If standard input is searched, a pathname of − is listed. 

−n Each line is preceded by its relative line number in the file starting at 1.  The line number is reset for each file searched.  This option is ignored if −c, −b, −l, or −q is specified. 

−q (Quiet) Do not write anything to the standard output, regardless of matching lines.  Exit with zero status upon finding the first matching line.  Overrides any options that would produce output. 

−s Error messages produced for nonexistent or unreadable files are suppressed. 

−v All lines but those matching are printed. 

−x (eXact) Matches are recognized only when the entire input line matches the fixed string or regular expression. 

In all cases in which output is generated, the file name is output if there is more than one input file.  Care should be taken when using the characters $, ∗, [, ^, │, (, ), and \ in expression, because they are also meaningful to the shell. It is safest to enclose the entire expression argument in single quotes (´...´).

EXTERNAL INFLUENCES

Environment Variables

LC_COLLATE determines the collating sequence used in evaluating regular expressions. 

LC_CTYPE determines the interpretation of text as single- and/or multi-byte characters, the classification of characters as letters, the case information for the -i option, and the characters matched by character class expressions in regular expressions. 

LANG determines the language in which messages are displayed. 

If LC_COLLATE or LC_CTYPE is not specified in the environment or is set to the empty string, the value of LANG is used as a default for each unspecified or empty variable.  If LANG is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of LANG.  If any internationalization variable contains an invalid setting, the commands behave as if all internationalization variables are set to "C".  See environ(5).

International Code Set Support

Single- and multi-byte character code sets are supported. 

RETURN VALUE

Exit status is:

0 One or more matches found

1 No match found

2 Syntax error or inaccessible file (even if matches were found). 

EXAMPLES

In the Bourne shell (sh(1)) the following example searches two files, finding all lines containing occurrences of any of four strings:

grep −F  ´if
then
else
fi´  file1 file2

Note that the single quotes are necessary to tell grep -F when the strings have ended and the file names have begun. 

For the C shell (csh(1)) the following command can be used:

grep −F ´if\
then\
else\
fi´ file1 file2

To search an address file with the following entries:

Ken   112 Warring St.  Apt. A
Judy  387 Bowditch  Apt. 12
Ann   429 Sixth St.

the command:

grep Judy address

prints:

Judy  387 Bowditch  Apt. 12

To search a file for lines that contain either a Dec or Nov, use the command:

grep −E ´[Dd]ec|[Nn]ov´ file

or

egrep -i ´dec|nov´ file

SEE ALSO

sed(1), sh(1), environ(5), lang(5), regcomp(3c), regexp(5). 

STANDARDS CONFORMANCE

grep: SVID2, XPG2, XPG3, proposed POSIX.2 FIPS (June 1990)

egrep: SVID2, XPG2, XPG3, proposed POSIX.2 FIPS (June 1990)

fgrep: SVID2, XPG2, XPG3, proposed POSIX.2 FIPS (June 1990)

Hewlett-Packard Company  —  HP-UX Release 8.05: June 1991

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