Re: [UNIX] search/replace command

From: Nocturnal Occulto (ronnyi@ifi.uio.no)
Date: 05/15/99


Angus Mezick wrote:

> perl -pi.bak -e 's/GET_FUN/GET_OBJ_FUN/g' *.[ch]
> --Angus
>
> Roger Smith <sexycoder@HOTMAIL.COM> on 05/14/99 01:40:49 AM
>
> I looking for a way to search through my source code and replace every
> instance of "GET_FUN" with "GET_OBJ_FUN".  I suppose grep might have
> something to do with it.  Thanks in advance ;)
>
> -Roger
> <bobbit>
>
>      +------------------------------------------------------------+
>      | Ensure that you have read the CircleMUD Mailing List FAQ:  |
>      |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
>      +------------------------------------------------------------+

.#!/usr/bin/perl
if($#ARGV < 0){
   print ("Enter string to search for:\n ");
   $search = <STDIN>;
   print ("\nEnter string to replace it with:\n ");
   $replace = <STDIN>;
   print ("\nEnter directory path to search:\n ");
   $dirpath = <STDIN>;
   $doit[0] = "$search^^$replace^^$dirpath";
}else{
   $filename = $ARGV[0];

   if($filename !~ /\w/){
      print ("You must specify a configuration file.\n");
      print ("If you wish to enter parameters manually, don't put
arguments.\n");
      exit(1);
   }

   $dirpath = $ARGV[1];

   if($filename !~ /\./){$filename .= '.rep';}
   $filename =~ s/\s//g;

   open(FIL, "<$filename") || die ("Can't open $filename\n");
   @doit = <FIL>;
   close(FIL);
}

print ("\nStarting search and replace...(a LaFay production)\n");

$count = 0;

loopz:
foreach $line (@doit){
   $line =~ s/(\n|\r|\t)//g;

   ($search,$replace,$newdirpath) = split(/\^\^/,$line);
   if($newdirpath ne ""){$dirpath = $newdirpath;}

   if($dirpath eq ""){$dirpath = '.';}
   $dirpath =~ s/\s//g;

   if($dirpath !~ /(\/)$/){$dirpath .= '/';}

   print ("Opening $dirpath\n");
   opendir(DIR, "$dirpath") || next;
   print ("Okay\n");

   @files = readdir(DIR);
   closedir(DIR);

   @files = grep(!/^(\.|\.\.)$/,@files);


   @files = grep(/(\.h|\.c)$/,@files);

loopy:
   foreach $file (@files){

      if(!(-f "$dirpath$file")){

  if(-d "$dirpath$file"){
     $doit[$#doit + 1] = "$search^^$replace^^$dirpath$file";
  }
  next;
      }

      open(FIL,"<$dirpath$file") || next;

      @data = ();

      @data = <FIL>;
      close(FIL);

loopx:
      if($num = grep(/$search/,@data)){

  foreach $line (@data){
     if($line =~ s/$search/$replace/gi){$count++;}
  }

  open(FIL,">$dirpath$file") || next;
  seek(FIL,0,0);
  print FIL @data;
  close(FIL);
      } #end loopx

   } #end loopy
} #end loopz

print ("\nDONE! Total matches replaced: $count\n");


Nocturnal


--
--------------- Codito, ergo sum - "I code, therefore I am" --------------------

http://birk113.studby.uio.no/~ronnyi                           ronnyi@ifi.uio.no

---------------- In a world without fences, who need gates? --------------------


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST