Previous Table of Contents Next

Module 60


The external grep command is used to search ASCII text files for a given pattern. All lines containing the pattern are written to the standard output. The pattern may be a word or string of characters. Regular expressions may be used inside the pattern to specify certain sequences of characters. If grep searches more than one file, each displayed line is preceded by the filename. If no filenames are given, grep reads from the standard input. Thus you can use grep in a pipe command.

The name grep is a combination of editor command characters. It is from the editor command :g/RE/p, which translates to global Regular Expression print. Inside the editor this command would search the entire file for all lines matching the RE pattern. The -v option of grep is from the editor command :v/RE/p, which searches the entire file for all lines except the ones containing the RE pattern.

The grep command performs line oriented searches. Therefore, a phrase that spans more than one line is not matched. When deciding what string to use for the search you should try to keep it as short as possible yet as unique as possible.


Following is the general format of the grep command.

     grep [ -bchilnsv ] pattern file_list

BSD (Berkeley)
grep [ -bcilnsvw ] pattern file_list


The following options may be used to control how grep functions.

-b Displays the block number in which the pattern was found before the line that contains the matching pattern.
-c Displays only a total count of matching lines for each file processed.
-h Suppresses the displaying of filenames which precede lines that match the specified patterns when multiple files are searched.
-i Ignores the difference between uppercase and lowercase characters during comparisons.
-l Displays only the names of the files containing the specified pattern. The lines containing the patterns are not displayed.
-n Displays the line number before each line containing the pattern.
-s Suppresses the displaying of diagnostic error messages for nonexistent and nonreadable files.
-v Displays only the lines that do not match the pattern. The v command in the ex editor performs the same type of function. It is an exception search. Search for every line except the ones containing the given pattern.

BSD (Berkeley)
-e -string This allows you to specify a string that begins with a dash. Normally any argument beginning with a dash is interpreted as an option not a string or argument.
-w The expression is searched for as a word. The RE metacharacters \< and \> are assumed to surround the expression.


The following list describes the arguments that may be passed to the grep command.

pattern Any combination of characters, numbers, and regular expression patterns. grep searches for strings of text that match the pattern expression.
file_list One or more files to search for the given pattern



The following table contains each regular expression and the task that it performs when used inside a pattern. A regular expression consists of regular alphanumeric characters matching themseleves and special characters matching certain patterns of text. Regular Expressions are often referred to as REs in UNIX terminology. Thus we use the RE notation for uniformity and briefness.


c Matches the character c.
string Matches the specified string of characters.


Metacharacters are the special characters used in regular expression patterns that have special meanings. Metacharacters are often referred to as special or magic characters or wild cards.


\ Escapes the meaning of a metacharacter.
^ Matches the beginning of the line.
$ Matches the end of the line.
. Matches any single character.
[class] A character class. Matches any one character in the class.
[c1-c2] Match any one of the ASCII characters in the range defined within the brackets.
[^class] Does NOT match any of the ASCII characters listed within the brackets. Ranges may be specified.
* Matches zero or more occurrences of the preceding regular expression.
\(RE\) Groups the regular expression RE for later reference; each RE is referenced by a corresponding number (see \n). The first group is referenced by \1, the second group by \2, and so on. You can have a maximum of nine groups in one regular expression pattern.
\n Matches the nth grouped \(RE\) within the same regular expression.
RE\{m\} Matches exactly m occurrences of the preceding one-character RE.
RE\{m,\} Matches m or more occurrences of the preceding one-character RE.
RE\{m,n\} Matches m through n occurrences of the preceding one-character RE.

To have a metacharacter interpreted as a normal character, proceed it with a backslash (\).


The input lines read by grep are limited to the system's BUFSIZ definition. If a line is longer than BUFSIZ characters, the line is truncated before the comparison is made. The BUFSIZ used by your system can usually be found in the /usr/include/stdio.h file.


Refer to the nawk, ed, ex, egrep, fgrep, sed, and ksh commands described in modules 6, 39, 43, 40, 52, 117, and 71.


The grep command can read from the standard input or a specified list of files. It writes to the standard output.


The return code is set to 0 if any patterns where found; if no patterns where found it is 1. If syntax errors are present in the pattern or if files are not accessible, a return code of 2 is returned, even if patterns where found.


You can use grep to search text for a specific word or string of characters. The text may be contained in a file or be from the standard input. The various uses of grep include finding lines or files that contain a specific pattern, counting the number of lines that contain the pattern in a file, showing the line numbers of lines that contain the pattern, and showing the block that contains the pattern. These uses may be inverted to function on all lines that do not contain the specified pattern also.

The uses of grep are far reaching, from just searching a file for lines that contain a pattern to using grep inside shell scripts to verify a pattern exists. It is one of those commands that doesn't seem useful until you use it a few times, then it becomes one of your more commonly used commands.


In this activity you use the grep command to display the BUFSIZ for your system, then to locate your login in the passwd file. Various pattern examples are also used to show you how to use the regular expressions. Begin at the shell prompt.

1.  Type grep BUFSIZ /usr/include/stdio.h and press Return. This returns the maximum number of characters a line can contain that grep can search for in a given line.
2.  Type grep mylogin /etc/passwd and press Return. Notice your account entry in the /etc/passwd file is displayed.
3.  Type grep -v mylogin /etc/passwd and press Return. Notice that all lines except the one containing your account are displayed on your screen.
4.  Type who | grep tty07 and press Return. You may have to change the tty07 pattern to a different pattern, but this command returns who is logged in on the terminal device tty07.
5.  Turn to Module 40 to continue the learning sequence.

Previous Table of Contents Next