grep — Search a file for a specified pattern

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

Purpose

grep –F 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. grep uses standard string search functions. The search stops after a null character is encountered. grep should not be used on lines that contain embedded null characters.

grep –E works similarly, but uses extended regular expression matching. This is described in Regular Expressions (regexp). If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash (\) in front of the character. It is usually simpler to use grep –F when you don't need special pattern matching.

grep combines the functions of the UNIX commands egrep and fgrep. If you do not specify either –E or –F, grep behaves like grep –E 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 takes the first nonoption 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:
–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
Matches using extended regular expressions.
–e pattern
Specifies one or more patterns separated by newlines for which grep is to search.

You can 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
Matches using fixed strings.
–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 fileline number.
–q
Suppresses output and simply returns appropriate return code.
–s
Suppresses the display of any error messages for nonexistent or unreadable files.
–v
Complements the sense of the match—that is, 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:
grep -E "earth|air|fire|water" astro.log

Localization

grep uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_COLLATE
  • LC_CTYPE
  • LC_MESSAGES
See Localization for more information.

Exit Values

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 pattern.
  • –f option was missing patternfile.
  • Out of memory for input or to hold a pattern.
  • patternfile could not be opened.
  • Incorrect regular expression.
  • Incorrect command-line option.
  • The command line had too few arguments.
  • The 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 2 even if it succeeds in finding matches in other input files.

Messages and Return Codes

Possible error messages include:
input lines truncated—result questionable
One or more input lines were longer than grep could handle; the line has been truncated or split into two 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 release memory so that grep has more space to work with.

Limits

The longest input record (line) is restricted by the system variable LINE_MAX. It is always at least 2048 bytes. Longer lines are treated as two or more records.

Portability

POSIX.2, X/Open Portability Guide, UNIX systems.

The –b option is an extension of the POSIX standard.

Related Commands

ed, find, regexp (see Regular Expressions (regexp))