Recursive Grep

This program, called akgrep.php, will do a recursive grep through a project, then filter out unwanted lines. Parameters are:

#! /usr/bin/php
<?php
// usage: ./akgrep.php -s<search string> -e<exclude string> <path to Akelos project>
// The -e parameter may be omitted if the default $exclude_str is acceptable.
  $exclude_str = 'log|.svn|~|compiled';
  foreach($argv as $param) {
    if(substr($param,0,2) == '-e')
      $exclude_str = substr($param,2);
    elseif(substr($param,0,2) == '-s')
      $string = substr($param,2);
    else
      $filename = $param;
  }
  $excludes = explode('|',$exclude_str);
 
  $cmd = "grep -r $string $filename";
  unset($result);
  exec($cmd,$result);
  while(sizeof($result) > 0) {
    $line = array_shift($result);
    $filepos = strpos($line,':');
    $filename = substr($line,0,$filepos);
    foreach($excludes as $exclude){
      if(!strpos($filename,$exclude) === false){
        $line = '';
      }
    }
    if(strlen(trim($line)) > 0)
      echo "$line\n";
  }
?>
 
recursive-grep.txt · Last modified: 2008/02/18 00:31 by alake