grep, egrep, fgrep - search a file for a pattern
Plain call with pattern
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file ...]
Call with (multiple) -e pattern
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern... [-e pattern] ...
Call with -f file
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-f pattern_file] [file ...]
egrep [-cefilnsv] [expression] [file ...]
fgrep [-cefilnsvx] [strings] [file ...]
The grep command 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. The -E and -F options treat newlines
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.
The use of the -E and -F options is recommended for portability.
-E Extended regular expressions. Each pattern
specified is a sequence of one or more EREs.
The EREs can be separated by newline
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
newline 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 newline 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.
Hewlett-Packard Company - 1 - HP-UX Release 11i: November 2000
-b Each line is preceded by the block number on
which it was found. This is 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
-f pattern_file The regular expression (grep and grep -E) or
strings list (grep -F) is taken from the
-h Suppress printing of filenames when searching
-i Ignore uppercase/lowercase distinctions
-l Only the names of files with matching lines
are listed (once), separated by newlines. If
standard input is searched, a path name of
(standard input) will be written, in the
POSIX locale. In other locales, (standard
input) may be replaced by something more
appropriate in those locales.
-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
-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.
Hewlett-Packard Company - 2 - HP-UX Release 11i: November 2000
-w Select only those lines containing matches
that form whole words. The test is that the
matching substring must either be at the
beginning of the line, or preceded by a non-
word constituent character. Similarly, it
must be either at the end of the line or
followed by a non-word constituent character.
Word-constituent characters are letters,
digits, and the underscore.
-x (eXact) Matches are recognized only when the
entire input line matches the fixed string or
The file name is output in all the cases in which output is generated
if there are more than one input file, unless the -h option is
specified. 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 ('...').
LANG determines the locale to use for the locale categories when both
LC_ALL and the corresponding environment variable (beginning with LC_)
do not specify a locale. If LANG is not specified or is set to the
empty string, a default of C (see lang(5)) is used.
LC_ALL determines the locale to use to override any values for locale
categories specified by the settings of LANG or any environment
variables beginning with LC_.
LC_COLLATE determines the collating sequence used in evaluating
LC_CTYPE determines the interpretation of text as single byte 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.
LC_MESSAGES determines the language in which messages are displayed.
If any internationalization variable contains an invalid setting, the
commands behave as if all internationalization variables are set to C.
International Code Set Support
Single-byte and multi-byte character code sets are supported.
Upon completion, grep returns one of the following values:
Hewlett-Packard Company - 3 - HP-UX Release 11i: November 2000
0 One or more matches found.
1 No match found.
2 Syntax error or inaccessible file (even if matches were
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
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 (see csh(1)) the following command can be used:
grep -F 'if\ then\ else\ fi' file1 file2
To search a file named address containing the following entries:
Ken 112 Warring St. Apt. A
Judy 387 Bowditch Apt. 12
Ann 429 Sixth St.
grep Judy address
Judy 387 Bowditch Apt. 12
To search a file for lines that contain either a Dec or Nov, use
either of the following commands:
grep -E '[Dd]ec|[Nn]ov' file
egrep -i 'dec|nov' file
Search all files in the current directory for the string xyz:
grep xyz *
Search all files in the current directory subtree for the string xyz,
and ensure that no error occurs due to file name expansion exceeding
system argument list limits:
find . -type f -print |xargs grep xyz
Hewlett-Packard Company - 4 - HP-UX Release 11i: November 2000
The previous example does not print the name of files where string xyz
appears. To force grep to print file names, add a second argument to
the grep command portion of the command line:
find . -type f -print |xargs grep xyz /dev/null
In this form, the first file name is that produced by find, and the
second file name is the null file.
(XPG4 only.) If the -q option is specified, the exit status will be
zero if an input line is selected, even if an error was detected.
Otherwise, default actions will be performed.
If the -w option is specified with non-word constituent characters,
then the output is unexpected.
sed(1), sh(1), regcomp(3C), environ(5), lang(5), regexp(5).
grep: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
egrep: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
fgrep: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
Hewlett-Packard Company - 5 - HP-UX Release 11i: November 2000