grep

NAME

grep, egrep, fgrep - file-pattern searcher

SYNOPSIS

grep [-F|-E] [-c|-l|-q] [-hinosvx] -e pattern_list
	 [-f pattern_file] ... [file ...]

grep [-F|-E] [-c|-l|-q] [-hinosvx] [-e pattern_list]
	 -f pattern_file ... [file ...]

grep [-F|-E] [-c|-l|-q] [-hinosvx] pattern_list
	 [-f pattern_file ...] [file ...]

DESCRIPTION

The grep(1) utilities search the given input files, selecting lines that match one or more patterns; the type of patterns is controlled by the options specified. It is not considered an error if file is a file of type directory and no warning will be printed. By default, a pattern matches an input line if any regular expression (RE) in the pattern matches the input line without its trailing newline. 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 (basic regular expressions, described in the Interix User's Guide), use the grep(1) utility. To use extended regular expressions, use the -E flag, or invoke the program as egrep(1). To search for a fixed string instead of a pattern, use the -F flag, or invoke the command as fgrep(1). A fixed string is a string of characters; each character is matched only by itself.

OPTIONS

-c
Only a count of selected lines is written to standard output.
-E
Use extended regular expressions. Cannot be used with-F.
-e expression
Specify a pattern used during the search of the input. Multiple -e options can be used to specify multiple patterns; an input line is selected if it matches any of the specified patterns.
-F
Search for a fixed string rather than a regular expression. Cannot be used with -E.
-f pattern_file
The pattern is read from the file named by the path name pattern_file. Trailing newlines in the pattern_file are ignored.
-h
Suppress file names in output. Usually, if more than one file is searched, grep(1) prints the file name before printing the matched line. This option suppresses that behavior.
-i
The case of letters is ignored in making comparisons; uppercase and lowercase letters are considered identical.
-l
List only the names of the files containing lines that match. Path names are listed once per file searched. If the standard input is searched, the path name stdin is written. Useful for command lines such as the following:
vi 'grep -l -e "[Ee]xactly" *.txt'
-n
Write the line number before each line of output; the first line in each file is 1 even if multiple files are specified.
-o
Force file names in output. Usually, if more than one file is searched, grep(1) prints the file name before printing the matched line. This option forces the file name to be printed even if only one file is searched. For standard input, grep(1) prints "(standardinput):".
-q
Quiet: write nothing to standard output and exit with 0 if a line is selected.
-s
Silent mode. Suppress error messages ordinarily written for nonexistent or unreadable files. Other error messages are not suppressed.
-v
Selected lines are those not matching the specified patterns.
-x
"Exact" matching: the fixed string or the regular expression must match the entire line before it is considered a match. For example, the string "abc" matches a line that contains only "abc" but not a line that contains "abcd".

If no file arguments are specified, the standard input is used.

REGULAR EXPRESSIONS

Regular expressions are described in more detail on the manual page re_format(1).

EXTENDED REGULAR EXPRESSIONS

The following characters are interpreted by egrep(1):

$
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 fewer 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 that have meaning for extended regular expressions, the set of {$,.,^,[,],|,?,+,*,(,)}.

DIAGNOSTICS

The grep(1) utility exits with one of the following values:

0
One or more lines were selected.
1
No lines were selected.
>1
An error occurred.

If the utility is invoked as egrep(1), the -E flag is assumed. If the utility is invoked as fgrep(1), the -F flag is assumed.

SEE ALSO

ed(1)

sed(1)

re_format(5)