grep - Search a file for a pattern

Synopsis

grep [-E|-F] [-c|-l|-q] [ -R [-H | -L | -P] ] [-ihnsvwxy] [-e pattern_list] [-f pattern_file] [pattern] [file ...]

Description

The grep utility searches 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 newline. A null RE matches every line. Each input line that matches at least one of the patterns is written to the standard output.

If -E and -F options are both specified, the last one specified is used.

Options

-E
Use Extended Regular Expressions (ERE).
-F
Do not recognize regular expressions.
-H
If the -R option is specified, symbolic links on the command line are followed. Symbolic links encountered in the tree traversal are not followed.
-L
If the -R option is specified, both symbolic links on the command line and symbolic links encountered in the tree traversal are followed.
-P
If the -R option is specified, no symbolic links are followed.
-R
If file designates a directory, grep searches each file in the entire subtree connected at that point.
-c
Only a count of selected lines is written to standard output.
-e
pattern_list specifies one or more search patterns. Each pattern should be separated by a newline character.
-f
pattern_file specifies a file containing search patterns. Each pattern should be separated by a newline character.
-h
Do not print filename headers.
-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 the -c, -l, or -s options are specified.
-q
Quiet mode where no messages are printed. Only the exit status is returned.
-s
Suppress the error messages ordinarily written for nonexistent or unreadable files. Other messages are not suppressed.
-v
Selected lines are those not matching the specified patterns.
-w
The expression is searched for as a whole word (as if surrounded by "[[:<:]]" and "[[:>:]]").
-x
Match line if pattern is the only thing on the line. This option takes precedence over the -w option. If both are specified, the -w option is ignored.
-y
Ignore case (same as -i).

Operands

Each file specifies the path to a text file. If no file operandss are specified, the standard input is used.

Exit Status

Extended Regular Expressions (ERE)

The following characters are interpreted by grep:

$
Align the match from the end of the line.
^
Align the match from the beginning of the line. (NOTE: This character may not work correctly from a 5250 terminal session.)
|
Add another pattern (see example below).
?
Match one or less sequential repetitions of the pattern.
+
Match one or more sequential repetitions of the pattern.
*
Match zero or more sequential repetitions of the pattern.
.
Match any single character.
[ ]
Match any single character or range of characters enclosed in the brackets.

Escape special characters which have meaning to grep, that is, the set of {$,.,^,[,],|,?,+,*,(,)}.

Related information

Examples

  1. Find all occurrences of the word patricia in a file.
    grep patricia myfile
    
  2. Find all occurrences of the pattern ".Pp" at the beginning of a line. The single quotation marks assure the entire expression is evaluated by grep instead of by the shell. The carat (^) means from the beginning of a line.
    grep '^.Pp' myfile
    
  3. Find either 19, 20 or 25 in the file calendar.
    grep '19|20|25' calendar
    
  4. Find the total number of lines that matches a character in the range of "a" to "z".
    grep -c '[a-z]' reference/alphabet.text
    
  5. Display all lines that have a dollar sign ($) character in them. You must escape the dollar sign character so grep will not interpret the character. Also, display the line number as well as the line that contains the match.
    grep -n '\$' valid.file