previous contents up next

Unix for Advanced Users

15. Advanced Commands and Usage

15.5. The grep Family

What is Grep?

grep stands for "Global Regular Expression Print". It is the universal Unix command to extract specific patterns out of strings using regular expressions. (The word grep also comes from the vi/ex command to print all lines matching a certain pattern

g/re/p

where "re" is a "regular expression".)

Other flavors of grep: fgrep and egrep

In addition to the standard grep command there are other flavors of grep like egrep and fgrep.

fgrep = Fixed GREP

fgrep searches for fixed strings only. The "f" does not stand for "fast" - in fact, "fgrep foobar *.c" is usually slower than "egrep foobar *.c" (Yes, this is kind of surprising. Try it.)

fgrep still has its uses though, and may be useful when searching a file for a larger number of strings than egrep can handle.

egrep = Extended GREP

egrep uses fancier regular expressions than grep. Many people use egrep all the time, since it has some more sophisticated internal algorithms than grep or fgrep, and is usually the fastest of the three programs.

Using Grep

To search a text file for a string of characters or a regular expression use grep as follows:

grep pattern filename(s)

Using this command you can check to see if a text file holds specific information. grep is often used to search the output from a command.

The following are the most commonly used options of the grep command.

-iIgnore the case of the letters when searching the file.
-nOutput the line number of each line where a match is found in addition to the line itself.
-vOutput all the lines that DO NOT contain the search-string.
-wOutput all the lines that contain the word being searched for.

Examples

a) Search for all the occurrences of the string able in the file Chapter1.txt.

grep able Chapter1.txt

b) Search for all the occurrences of the string able in the file Chapter1.txt, ignoring the case of the letters in making comparisons.

grep -i able Chapter1.txt

c) Search for all the occurrences of the string able in the file Chapter1.txt. Output the line number of each line where a match is found in addition to the line itself.

grep -n able Chapter1.txt

d) Output all the lines that DO NOT contain the string able in the file Chapter1.txt.

grep -v able Chapter1.txt

e) Output all the lines that contain the word able in the file Chapter1.txt.

grep -w able Chapter1.txt

f) Output all the lines that contain the string was able in the file Chapter1.txt.

grep 'was able' Chapter1.txt

g) All of the previous examples can be made applicable to a group of files by using the star convention. The following searches for the string able in all the files whose names start with Chapter.

grep able Chapter*

The string you are searching for can be more than one word (i.e. there is a space or other delimiter character such as a comma or a hyphen in the string). In such cases, single quotes (') may be placed around the string. It is also useful to know that the string is treated as a regular expression resulting in characters such as . and * having special meaning.

Examples

To search a file for a simple text string:

grep copying help

This searches the file help for the string copying and displays each line on your terminal.

To search a file using regular expression:

grep -n '[dD]on\'t' tasks

This uses a regular expression to find and display each line in the file tasks that contains the pattern don't or Don't. The line number for each line is also displayed. The expression is quoted to prevent the shell expanding the metacharacters [, ] and '. Double quotes are used to quote the single quote in dDon't.

To use the output of another command as input to the grep command:

ls -l | grep ^d

This lists all the directories in the current directory for which other users have execute permission. The expression is quoted to prevent the shell interpreting the ^ metacharacter.

To redirect the results of a search to a file:

grep Smith /etc/passwd > smurffs

This searches the passwd file for each occurrence of the name Smith and places the results of this search in the file smurffs. There being a lot of Smiths everywhere this is quite a large file.

Using regular expressions with the grep command

The following characters can be used to create regular expressions for searching on patterns with grep. Always quote the regular expression. This prevents the shell from interpreting the special characters before it is passed to the grep command.

c       any non-special character represents itself
\\c     turns off the meaning of any special character
^       beginning of a line
$       end of a line
.       matches any single character except a newline
[...]   matches any of the enclosed characters
[^...]  matches any character that is not enclosed
[n-n]   matches any character in this range
*       matches any number of the preceding character

Examples

To search using enclosed characters:

grep -n '[dD]on't' tasks

Finds and display each line in the file tasks that contains the pattern don't or Don't. The line number for each line is also displayed.

To search from the beginning of a line:

ls -l | grep ^d

This lists all the directories in the current directory for which other users have execute permission.

To search for lines ending in a particular pattern:

ls -l | grep '[^.xdh]$'

This lists all the files and directories in the current directory which do not end in .xdh

previous contents up next