Pattern Search: grep, fgrep, egrep



next up previous contents index
Next: Manipulate Columns: Cut Up: Unix Toolkit Previous: Word Count: wc

Pattern Search: grep, fgrep, egrep

The grep command is so widely used it has become a verb. To grep a file is to find lines in it which match a specified pattern. This simple function quickly becomes indispensable once you learn how to use it. Yet in its attempting to be overly friendly (some may say to confuse the casual user), Unix systems provide three different versions of grep: the original grep, fast grep fgrep, and extended grep egrep. Other than nostalgia, there seems little reason to use the original grep. Since egrep is generally as fast as fgrep, we simplify things by just using egrep. The entire grep family accepts either file names or standard input. For example, to search all Fortran files in the current directory for the word gauz:
         

% egrep gauz *.f   	Search all Fortran files for gauz.   
bij.f:        common /gauz/ npts(3), kode(3)   	   
readin.f:     common /gauz/ npts(3), kode(3)   	   
udz1v.f:      common /gauz/ npts(3), kode(3)   	   
vkabs.f:      common /gauz/ npts(3), kode(3)   	   

Because egrep is case sensitive, ``gauz'' will not match ``GAUZ.'' To make egrep ignore case, use the -i option (or -y on some systems):

% egrep -i bigmat *.f   	Search for bigmat, ignore case.   
readin.f:      float BIGMAT[1000][1000])   	   
readin.f:      ferd[i] = ferd[i] * BigMat[i][j]   	   

It is also possible to use egrep to match one of several patterns and to use wild cards in the patterns: 

% egrep "parallel|distributed" Mail/*   	    Search Mail for either   
   	    parallel or distributed.   
oldmail: parallel processors.   	    A match for parallel.   
oldmail: distributed computing.   	    A match for distributed.   

  Next we use regular expressions and wild cards to search for patterns. First we search for all lines that start with ``alpha'':

% egrep '^ alpha' data   	Search through file data.   
alpha = 0.76353 sigma = 6.8336   	   
alpha = 0.78453 sigma = 7.0835   	   
alpha = 0.80463 sigma = 7.5638   	   

Since only a single file is searched in this example, egrep does not bother printing the file name.

There is also a veto option -v to egrep which excludes lines containing the objectionable regular expression:

% egrep -v '^ C' prog.f   	Print lines not starting with ``C.''   
% egrep -v '^ $' prog.f   	Print all except the blank lines.   



next up previous contents index
Next: Manipulate Columns: Cut Up: Unix Toolkit Previous: Word Count: wc