NAME
grep —
file pattern searcher
SYNOPSIS
grep |
[-bchilnosvw] [-e
pattern] [file ...] |
egrep |
[-bchilnosv] [-e
pattern] [-f
pattern_file] [file ...] |
fgrep |
[-bchilnosvx] [-e
pattern] [-f
pattern_file] [file ...] |
DESCRIPTION
Thegrep utilities search the given input files
selecting lines which match one or more patterns; the type of patterns is
controlled by the options specified. By default, a pattern matches an input
line if any regular expression (RE) in the pattern matches the input line
without its trailing <new-line>. A null RE matches every line. Each
input line that matches at least one of the patterns is written to the
standard output.
For simple patterns or
ex(1)
or ed(1) style regular expressions, the grep
utility is used. The egrep utility can handle
extended regular expressions and embedded <newline>s in patterns. The
fgrep utility is quick but can handle only fixed
strings. A fixed string is a string of characters, each character is matched
only by itself. The pattern value can consist of multiple lines with
embedded <newline>s. In this case, the <newline>s act as
alternation characters, allowing any of the pattern lines to match a portion
of the input.
The following options are available:
-b- The block number on the disk in which a matched pattern is located is displayed in front of the respective matched line.
-c- Only a count of selected lines is written to standard output.
-eexpression- Specify a pattern used during the search of the input. Multiple
-eoptions can be used to specify multiple patterns; an input line is selected if it matches any of the specified patterns. -fpattern_file- The pattern is read from the file named by the pathname pattern_file.
Trailing newlines in the pattern_file are ignored.
(
Egrepandfgreponly). -h- Never print filename headers with output lines.
-i- The case of letters is ignored in making comparisons - that is, upper and lower case are considered identical.
-l- Only the names of files containing selected lines are written to standard
output. Pathnames are listed once per file searched. If the standard input
is searched, the pathname ‘
-’ is written. -n- Each output line is preceded by its relative line number in the file; each
file starting at line 1. The line number counter is reset for each file
processed. This option is ignored if
-c,-l, or-sis specified. -o- Always print filename headers with output lines.
-s- Silent mode. Nothing is printed (except error messages). This is useful for checking the error status.
-v- Selected lines are those not matching the specified patterns.
-x- Only input lines selected against an entire fixed string or regular
expression are considered to be matching lines.
(
Fgreponly). -w- The expression is searched for as a word (as if surrounded by `\<' and
`\>', see ex(1).) (
Greponly)
If no file arguments are specified, the standard input is used.
The grep utility exits with one of the
following values:
0- One or more lines were selected.
1- No lines were selected.
>1- An error occurred.
EXTENDED REGULAR EXPRESSIONS
The following characters are interpreted by
egrep:
$- Align the match from the end of the line.
^- Align the match from the beginning of the line.
|- Add another pattern (see example below).
?- Match 1 or less sequential repetitions of the pattern.
+- Match 1 or more sequential repetitions of the pattern.
*- Match 0 or more sequential repetitions of the pattern.
[]- Match any single character or range of characters enclosed in the brackets.
\- Escape special characters which have meaning to
egrep, the set of {$,.,^,[,],|,?,+,*,(,)}.
EXAMPLES
To find all occurrences of the word patricia in a file:
grep patricia myfileTo find all occurrences of the pattern
‘.Pp’ at the beginning of a line:
grep '^\.Pp'The apostrophes assure the entire expression is
evaluated by grep instead of by the users shell. The
carat or hat
‘’ means
from the beginning of a
line, and the
‘^’ escapes
the ‘\’ which
would otherwise match any character..
A simple example of an extended regular expression:
egrep '19|20|25'
calendarPeruses the file calendar looking for either 19, 20 or 25.
SEE ALSO
HISTORY
The grep command appeared in
Version 6 AT&T UNIX.
BUGS
Lines are limited to 256 characters; longer lines are truncated.