[HRPM] grrep

Troy E. Webster twebster at pcs.cnu.edu
Sun May 28 16:56:49 CDT 2000


Hey all,

Here's a little script I'd like somefeedback  on. If there's any way to
make this more bullet-proof or functonal, please give me your thoughts.

Thanks,
Troy

#!/usr/local/bin/perl -w
##################################################################
# To use this script, just type grrep STRING FILETYPE
# FILETYPE is optional. The script will search for that 
# string in every file, beginning in the current directory,
# and continuing recursively. It will print out
# the path, filename, and the line of any matches
# it finds.
#
# AUTHOR: Troy Webster (modified and rewritten from 
#                       code authored by Mat Maravillas)
# Date: May 2000 
##################################################################
if (@ARGV < 1)  
{
    print <<ERROR_MSSG;
    
******************************************************************

You did not enter enough arguments. grrep must be called in the form:

    grrep [STRING] [TYPE] <- optional

STRING is the string you wish to look for.  
** NOTE: case sensitive **

TYPE(optional)  is the type of file you want to search in, such as
 .h or .pl

Remember, grrep is recursive, so every file in the directory you 
specify will be examined.

*****************************************************************

ERROR_MSSG

  exit;
}

$findstring = $ARGV[0];
if (@ARGV == 2)
{
    $do_pages = $ARGV[1];
}
else
{
    $do_pages = "";
}

$dont_pages = ".o";
$filesfound = 0;

$currentDirectory = `pwd`;
chomp($currentDirectory);
print "\n\n*** List of successful matches ***\n\n";

&goIntoDirectory("$currentDirectory");

if ($filesfound != 0)
{
    print "\n*** $filesfound matching files ***\n";
}

else
{
    print "\n*** No matching files ***\n";
}


#****************************************************
# subroutine goIntoDirectory
# 
# this sub is recursive and traverses the filesystem 
# starting at the directory passed in as its' 
# parameter, looking for a pattern match and printing
# out successful matches.
#****************************************************

sub goIntoDirectory  
{
  local($directory) = @_;
  
  opendir (THISDIR, "$directory") or print "Couldn't get into $directory\n";

 ### gives you is the list of files from the directory and it skips
 ### the '.' and '..' directories
  local(@allfiles) = grep !/^\.\.?$/, readdir(THISDIR); 
  closedir THISDIR;

  local($thisfile);

  foreach $file (@allfiles)  
  {
      local($tmp) = $file;
      local($uno) = chop($tmp);
      
      ### check for directory ###
    if (opendir(ANOTHERDIR, "$directory/$file"))  
    {          
      closedir (ANOTHERDIR);
      
      ### recurse down ###
      &goIntoDirectory("$directory/$file");   
    }

    elsif ($uno =~ /$dont_pages/)
    {}
   
    elsif (($file =~ /$do_pages/) || ($do_pages eq ""))  
    {
        my $flag = 0;

        unless (open(FILENAME, "$directory/$file"))  
        {
            print "Unable to look into $directory/$file \n";
            $flag = 1;
        }

        $thisfile = 0;
        
        while (($flag != 1) && ($tempstring = <FILENAME>))  
        {
            if ($tempstring =~ /$findstring/)  
            {
                if ($thisfile == 0)
                {
                    print "$directory/$file:\n\t $tempstring \n";
                    $filesfound++;
                    $thisfile = 1;
                }
            }
        }
        close FILENAME;
    } 
  }
}

###################### End grrep ################################




More information about the Norfolk-pm mailing list