Next 15.H: Sorting Files with sort  Up 15: File Utilities  Prev 15.F: Finding Files with find  Contents

Specialized

§ 15.G: Searching Files with egrep


The egrep command is a great way to find specific information in a file. One use of egrep would be finding specific data in a program output file (say data labeled "total"). For now we will do a relatively simple search for the name "Jack". The syntax for the egrep command is the command, followed by the string you want to search for, followed by the file(s) you want to search. To search in all files of our directory we use the wildcard *.
> egrep Jack *

Unix should respond with:

sample.doc:Jack and Jill went up the hill
sample.doc:Jack fell down and broke his crown
(maybe a few more lines if your name happens to be Jack). Notice that the output consists of the the file name followed by the line which contains the characters.

Be careful when using egrep. Like most of Unix it is case sensitive. Notice the the command:
> egrep jack *

finds nothing. If you do want egrep to be case sensitive, you can use the option -i to make it ignore the case. Try:
> egrep -i jack *

Be aware that you may have difficulty searching for phrases containing punctuation and special characters. egrep may misinterpret your punctuation (for instance egrep will interpret > to be an output redirection not a part of your string). If you are using egrep on the physics cluster, enclosing your string in single quotes (egrep 'answer >' *.dat) will generally work. If you are working elsewhere check your man pages for the proper syntax.

Like the other utilities, egrep is a powerful command which can be used for a wide variety of purposes. As you become more accustomed to Unix, you will discover an increasing variety of uses for egrep and as you have need for specialized searches, check the man pages to see if egrep options to do what you want. egrep, like the other Unix utilities is a very powerful command. has more information about the egrep command and its many uses.


Next 15.F: Sorting Files with sort  Up 15: File Utilities  Prev 15.F: Finding Files with find  Contents

Comments and questions to Dr. Elias N. Houstis at enh@cs.purdue.edu.