grep Command

Purpose

Searches for a pattern in a file.

Syntax

grep [ -E | -F ] [ -i ] [ -h ] [ -H ] [ -L ] [ -r | -R ] [ -s ][ -u ] [ -v ] [ -w ] 
[ -x ] [ -y ] [ [ [ -b ] [ -n ] ] | [ -c | -l | -q ] ] [ -p [ Separator ] ] { [ -e PatternList ... ] [ -f PatternFile ... ] | PatternList ... } 
[ -U [ -I ] unicode_hex_notation_pattern ] [ File ... ]

Description

The grep command searches for the pattern that is specified by the Pattern parameter and writes each matching line to standard output. The patterns are limited regular expressions in the style of the ed or egrep command. The grep command uses a compact nondeterministic algorithm.

The grep command displays the name of the file that contains the matched line if you specify more than one name in the File parameter. Characters with special meaning to the shell ($, *, [, |, ^, (, ), \) must be in quotation marks when they appear in the Pattern parameter. When the Pattern parameter is not a simple string, you must enclose the entire pattern in single quotation marks. In an expression such as [a-z], the - (minus sign) cml specifies a range, according to the current collating sequence. A collating sequence might define equivalence classes for use in character ranges. If no files are specified, grep assumes standard input.

Notes:
  1. Do not run the grep command on a special file because it produces unpredictable results. Input lines must not contain the NULL character.
  2. Input files must end with the newline character.
  3. The newline character is not matched by the regular expressions.
  4. Although some flags can be specified simultaneously, some flags override others. For example, the -l option takes precedence over all other flags. And if you specify both the -E and -F flags, the last one specified takes priority.

Flags

Table 1. Flags
Item Description
-b Precedes each line by the block number on which it was found. Use this flag to help find disk block numbers by context. The -b flag cannot be used with input from stdin or pipes.
-c Displays only a count of matching lines.
-E Treats each pattern that is specified as an extended regular expression (ERE). A NULL value for the ERE matches every line.
Note: The grep command with the -E flag is the same as the egrep command, except that error and usage messages are different and the -s flag functions differently.
-e PatternList Specifies one or more search patterns. This flag works like a simple pattern search but is useful when the pattern begins with a - (minus). Patterns must be separated by a new-line character. A NULL pattern can be specified by two adjacent new-line characters or a quotation mark followed by a new-line character ("\n). Each pattern is treated like a basic regular expression (BRE) unless the -E or -F flag is also specified. Multiple -e and -f flags are accepted by grep. All the specified patterns are used when lines are matched, but the order of evaluation is unspecified.
-F Treats each specified pattern as a string instead of a regular expression. A NULL string matches every line.
Note: The grep command with the -F flag is the same as the fgrep command, except that error and usage messages are different and the -s flag functions differently.
-f PatternFile Specifies a file that contains search patterns. Each pattern must be separated by a new-line character, and an empty line is considered a NULL pattern. Each pattern is treated like a basic regular expression (BRE), unless the -E or -F flag is also specified.
-h Prevents the name of the file that contains the matching line from being appended to that line. Suppresses file names when multiple files are specified.
-H If the -r or -R option is specified and a symbolic link that refers to a file of type directory is specified on the command line, grep command searches the files of the directory that is referenced by the symbolic link and all the files in the file hierarchy after it.
-i Ignores the case (uppercase or lowercase) of letters when making comparisons.
start of change-I end of change start of changeSpecifies that the search is based on a loose match of the specified Unicode hexadecimal notation pattern. Most of the regular expression engines offer case-insensitive matching as the only loose matching. If the expression engine offers case-insensitive matching as the only loose matching, then the expression engine must account for the large range of cased Unicode characters outside of the ASCII characters.
Note: The -I flag can be used only with the -U flag.
end of change
-l Lists just the names of files (once) which contain matching lines. Each file name is separated by a new-line character. If standard input is searched, a path name of StandardInput is returned. The -l flag with any combination of the -c flag and the -n flag behaves like the -l flag only.
-L If the -r or -R option is specified and a symbolic link that refers to a file of type directory is specified on the command line or encountered during the traversal of a file hierarchy, grep command searches the files of the directory that is referenced by the symbolic link and all the files in the file hierarchy after it. If both -H and -L are specified, the last option that is specified on the command line takes effect.
-n Precedes each line with the relative line number in the file. Each file starts at line 1, and the line counter is reset for each file processed.
-p[Separator] Displays the entire paragraph that contains the matched lines. Paragraphs are delimited by paragraph separators, as specified by the Separator parameter, which are patterns in the same form as the search pattern. Lines containing the paragraph separators are used only as separators; they are never included in the output. The default paragraph separator is a blank line.
-q Suppresses all writing to standard output, regardless of matching lines. Exits with a zero status if an input line is selected. The -q flag with any combination of the -c, -l, and -n flags behaves like the -q flag only.
-r Searches directories recursively. By default, links to directories are followed.
-R Searches directories recursively. By default, links to directories are not followed.
-s Suppresses error messages that are ordinarily written for nonexistent or unreadable files. Other error messages are not suppressed.
-u The -u flag causes output to be unbuffered.
start of change-U unicode_hex_notation_patternend of change start of changeThe -U flag turns on the Unicode flag. The regular expression writer might not use a Unicode character set to specify the pattern that needs to be searched. Also, it might not be possible to input Unicode-defined code points for every character of the major written languages by using a keyboard. Therefore, the value of the unicode_hex_notation_pattern pattern must be the hexadecimal representation of the Unicode-defined code point of a character. For example, to represent the 𝄞 character, whose Unicode-defined code point is U+1D11E, the value of unicode_hex_notation_pattern can be the hexadecimal representation as \U0001D11E, \x{1D11E}, or \u{1D11E}.
Notes:
  • The function of the grep command with the -U flag is the same as the function of the ugrep command.
  • The -U flag does not work with the legacy flags of grep command.
end of change
-v Displays all lines not matching the specified pattern.
-w Does a word search.
-x Displays lines that match the specified pattern exactly with no additional characters.
-y Ignores the case of letters when making comparisons.
PatternList Specifies one or more patterns to be used during the search. The patterns are treated as if they are specified by using the -e flag.
File Specifies a name of a file to be searched for patterns. If no File variable is given, the standard input is used.

Exit Status

This command returns the following exit values:

Table 2. Exit status
Item Description
0 A match was found.
1 No match was found.
>1 A syntax error was found or a file was inaccessible (even if matches were found).

Examples

  1. To use a pattern that contains some of the pattern-matching characters *, ^, ?, [, ], \(, \), \{, and \}, enter the following command:
    grep  "^[a-zA-Z]"  pgm.s 
    This displays every line in pgm.s file whose first character is a letter.
  2. To display all lines that do not match a pattern, enter the following command:
    grep   -v  "^#" pgm.s
    This displays every line in pgm.s file whose first character is not a # (pound sign).
  3. To display all lines in the file1 file that match either the abc or xyz string, enter the following command:
    grep -E "abc|xyz"  file1
  4. To search for a $ (dollar sign) in the test2 file, enter the following command:
    grep \\$ test2
    The \\ (double backslash) characters are necessary in order to force the shell to pass a \$ (single backslash, dollar sign) to the grep command. The \ (single backslash) character tells the grep command to treat the following character (in this example the $) as a literal character rather than an expression character. Use the fgrep command to avoid the necessity of using escape characters such as the backslash.
  5. To search recursively through /tmp to find files that have the word IBM without recursing through links that points to directories, enter the following command:
    grep –R IBM /tmp

    Or

    grep –r -H IBM /tmp
  6. To search recursively through /tmp to find files that have the word IBM and recurse through links as well, enter the following command:
    grep –r IBM /tmp

    Or

    grep -R -L IBM /tmp
  7. start of changeTo search the regex_test.txt file for the character , whose Unicode-defined code point is U+6211 and the hexadecimal representation is \u6211, enter the following command:
    grep -U "\u6211" regex_test.txt
    To search multiple characters, you can add a list of hexadecimal representations of the Unicode-defined code points without any space. For example, to search the characters and in the regex_test.txt file, enter the following command:
    grep -U “\u0918\u0930" regex_test.txt
    end of change
  8. start of change
    To specify a range of characters between the code points U+6200 and U+6300 to search in the regex_test.txt file, enter the following command:
    grep -U "[\u6200-\u6300]" regex_test.txt
    To specify a range of characters between the code points U+6200 and U+6300 that are also uppercase to search in the regex_test.txt file, enter the following command:
    grep -U "[\u0000-\U0010FFFF--\p{Lu}]" regex_test.txt
    end of change
  9. start of changeTo do a loose match search of the character 𐐥, whose Unicode-defined code point is U+10425 and the hexadecimal representation is \U00010425, enter the following command:
    grep -U -I "\U00010425" regex_test.txt
    end of change
  10. start of changeTo search the regex_test.txt file for a number with decimal digits, enter the following command:
    grep -U "\p{Nd}" regex_test.txt
    where Nd is a Unicode character property for numbers with decimal digits.end of change
  11. start of changeTo search the regex_test.txt file for Hiragana characters in the Japanese language, enter the following code:
    grep -U "\p{Hiragana}" regex_test.txt
    end of change

Files

Table 3. Files
Item Description
/usr/bin/grep Contains the grep command.