grep, egrep, fgrep

match patterns in a file

Command


SYNOPSIS

grep [-bcEFilnqsvx] [-e pattern] ... [-f patternfile] ... [pattern] [file ...]

egrep [-bcilnqsvx] [-e pattern] ... [-f patternfile] ... [pattern] [file ...]

fgrep [-bcilnqsvx] [-e pattern] ... [-f patternfile] ... [pattern] [file ...]


DESCRIPTION

fgrep searches files for one or more pattern arguments. It does not use regular expressions; instead, it does direct string comparison to find matching lines of text in the input.

egrep works in a similar way, but uses extended regular expression matching (as well as the \< and \> metacharacters) as described in regexp. If you include special characters in patterns typed on the command line, escape them by enclosing them in apostrophes to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to egrep, put a backslash (\) in front of the character. It is usually simpler to use fgrep when you don't need special pattern matching.

grep is a combination of fgrep and egrep. If you do not specify either -E or -F, grep behaves like egrep, but matches basic regular expressions instead of extended ones. You can specify a pattern to search for with either the -e or -f option. If you specify neither option, grep (or egrep or fgrep) takes the first non-option argument as the pattern for which to search. If grep finds a line that matches a pattern, it displays the entire line. If you specify multiple input files, the name of the current file precedes each output line.

Options

grep accepts all of the following options while egrep and fgrep accept all but the -E and -F options.
-b

precedes each matched line with its file block number.

-c

displays only a count of the number of matched lines and not the lines themselves.

-E

causes grep to behave like egrep.

-e pattern

specifies one or more patterns for which grep is to search. You may indicate each pattern with a separate -e option character, or with newlines within pattern. For example, the following two commands are equivalent:

grep -e pattern_one -e pattern_two file
grep -e 'pattern_one
pattern_two' file
-F

causes grep to behave like fgrep.

-f patternfile

reads one or more patterns from patternfile. Patterns in patternfile are separated by newlines.

-i

ignores the case of the strings being matched.

-l

lists only the file names that contain the matching lines.

-n

precedes each matched line with its file line number.

-q

suppresses output and simply returns appropriate return code.

-s

suppresses the display of any error messages for nonexistent or unreadable files.

-v

displays all lines not matching a pattern.

-x

requires a string to match an entire line.


EXAMPLES

To display every line mentioning an astrological element:
egrep "earth|air|fire|water" astro.log


DIAGNOSTICS

Possible exit status values are:
0

The command found at least one match for pattern.

1

The command found no matches for pattern.

2

Failure due to any of the following:

-e option was missing a pattern
-f option was missing a patternfile
— out of memory for input or to hold a pattern
patternfile could not be opened
— invalid regular expression
— invalid command line option
— command line had too few arguments
— input file could not be opened
If the program fails to open one input file, it tries to go on to look at any remaining input files, but it returns 1 even if it succeeds in finding matches in other input files.

Messages

input lines truncated - result questionable

One or more input lines were longer than grep can handle; the line has been truncated or split into two lines. Shorten the line or lines, if possible. This message does not affect the exit status.

out of space for pattern "string"

grep did not have enough memory available to store the code needed to work with the given pattern (regular expression). The usual cause is that the pattern is very complex. Make the pattern simpler, or try to free up memory to give grep more space with which to work.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0.

Only the actual grep command is a part of the POSIX and x/OPEN standards. The egrep and fgrep commands are extensions. The -b option is also an extension to the POSIX and x/OPEN standards.


SEE ALSO

Commands:
ed, find

Miscellaneous:
regexp


Updated MKS Toolkit [3khat16.ico]HP3000 [3khat16.ico]3kMail [archive16.gif]