GREP(1)
NAME
grep, egrep, fgrep − search a file for a pattern
SYNOPSIS
grep [ options ] [ expression ] [ file ... ]
egrep [ options ] [ expression ] [ file ... ]
fgrep [ options ] [ strings ] [ file ... ]
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 supports the Basic Regular Expression syntax (see regexp(5)). Egrep supports the Extended Regular Expression syntax (see regexp(5)) with the extension that newline is also a regular expression alternation character. Fgrep patterns are fixed strings, making it a fast and compact means for finding known text strings.
Options
−v All lines but those matching are printed.
−x (Exact) only lines matched in their entirety are printed (fgrep only).
−c Only a count of matching lines is printed.
−i Ignore uppercase/lowercase distinction during comparisons.
−l Only the names of files with matching lines are listed (once), separated by newlines.
−n Each line is preceded by its relative line number in the file.
−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.
−s The error messages produced for nonexistent or unreadable files are suppressed.
−e expression Same as a simple expression argument, but useful when the expression begins with a hyphen (−).
−f file The regular expression (grep and egrep) or strings list (fgrep) is taken from the file.
In all cases, 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 ´...´.
Fgrep searches for lines that contain one of the strings, each of which is separated from the next by a newline.
DIAGNOSTICS
Exit status is 0 if any matches are found, 1 if none, 2 for syntax errors or inaccessible files (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:
fgrep ´if
then
else
fi´ file1 file2
Note that the single quotes are necessary to tell fgrep when the strings have ended and the file names have begun.
For the C shell (csh(1)) the following command can be used:
fgrep ´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
would print:
Judy 387 Bowditch Apt. 12
WARNINGS
Lines matching the pattern but which are longer than BUFSIZ-1 bytes might not be found as a match or might be only partially copied to the standard output. Grep, fgrep, and egrep differ in their handling of long lines. (BUFSIZ is defined in /usr/include/stdio.h.)
If a line has embedded nulls, grep and egrep search only up to the first null; if a match is found the entire line is output.
SEE ALSO
sed(1), sh(1), environ(5), lang(5), regexp(5).
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 "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.
STANDARDS CONFORMANCE
grep: SVID2, XPG2, XPG3
egrep: SVID2, XPG2, XPG3
fgrep: SVID2, XPG2, XPG3
Hewlett-Packard Company — HP-UX Release 7.0: Sept 1989