Everhart, Glenn
From:	Ron [RROCKWELL@CCTR.UMKC.EDU]
Sent:	Tuesday, July 14, 1998 8:14 PM
To:	Info-VAX@Mvb.Saic.Com
Subject:	Re: mass search and replace

John,

 it could use some tweaking perhaps, but imho, case irrelevance would/could
 be dangerous, but, however, i have attached some code below. it does not
 delete the original files, simply rewrites them. It accepts any wildcard
 vms filespec.

 i compile with the following, your milage may vary... if you have
 platform problems, lemme know. The file at bottom is named mass_replace.c

$ cc/optimize=(level=4,inline=all,unroll=0,tune=EV5) -
   /warnings=(enable=(ALL,CHECK), -
   disable=(PORTABLE,DOLLARID,MISALGNDSTRCT,NESTEDTYPE,TRUNCINTCAST)) -
   /standard=ansi89 -
   mass_replace
$ link mass_replace, -
       mass_replace.opt/opt

mass_replace.opt is a file containing the single line:

identification="v01.0.01"


 Ron Rockwell, Systems Programmer         rrockwell@umkc.edu           
 Academic Computing Services              University of Missouri at Kansas City


    o--------------------====<[ Included Text ]>====--------------------o
Recently, John Furcht wrote:

Is there a way in VMS to search and replace text in multiple files in a
directory?
Similar to the "search" command but with the option of mass substituting
text??
If not in VMS or EDT, does anyone know of a utility to do this. Even a PC
utility would do.....


    o--------------------====<[ Included Code ]>====--------------------o

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lib$routines.h>
#include <rms.h>
#include <descrip.h>

#define VERSION "01.0.01"
#define SRC_SITE "(Loki)"
#define SRC_ARCH "Alpha"
#define FACILITY "MASS_REPLACE"
#define FORMAL_NAME "MASS_REPLACE v01.0.01 (Loki), Tue, 14-Jul-1998"

char version[]=VERSION;
char src_site[]=SRC_SITE;
char src_arch[]=SRC_ARCH;
char facility[]=FACILITY;
char formal_name[]=FORMAL_NAME;

/*----------------------------------------------*/
void main(int argc, char *argv[]) {
int status;
char fspec[256];
char result[256];
char buf[256];
char buf2[256];
struct dsc$descriptor fname, ff_result;
unsigned long ctx=0;
int found;
FILE *ifp;
FILE *ofp;
char *d,*i;
char c;
int matched;
  (void)printf("%s\n",formal_name);
  if (argc>2) {
    found=0;
    (void)strcpy(fspec,argv[1]);
    fname.dsc$b_dtype = DSC$K_DTYPE_T;
    fname.dsc$b_class = DSC$K_CLASS_S;
    fname.dsc$w_length = (unsigned char)strlen(fspec);
    fname.dsc$a_pointer = fspec;
    ff_result.dsc$b_dtype = DSC$K_DTYPE_T;
    ff_result.dsc$b_class = DSC$K_CLASS_S;
    ff_result.dsc$w_length = 256;
    ff_result.dsc$a_pointer = result;
    status=-1;
    while (status!=RMS$_NMF) {
      status=lib$find_file(&fname, &ff_result, &ctx);
      if (status==RMS$_NORMAL) {
        d=result;
        while ((*d) && (*d!=';')) d++;
        *d='\0';
        found=1;
        matched=0;
        ifp=fopen(result,"r");
        if (ifp) {
          while ((!matched) && (fgets(buf,sizeof(buf),ifp))) {
            if (strstr(buf,argv[2])) matched=1;
          }
          (void)fclose(ifp);
        } else (void)printf("Could not open %s for input.\n",result);
        if (matched) {
          ifp=fopen(result,"r");
          if (ifp) {
            ofp=fopen(result,"w");
            if (ofp) {
             (void)printf("Rewriting %s...\n",result);
             while (fgets(buf,sizeof(buf),ifp)) {
                d=strstr(buf,argv[2]);
                while (d) {
                  *d='\0';
                  (void)strcpy(buf2,buf);
                  (void)strcat(buf2,argv[3]);
                  d+=strlen(argv[2]);
                  (void)strcat(buf2,d);
                  (void)strcpy(buf,buf2);
                  d=strstr(buf,argv[2]);
                }
                (void)fputs(buf,ofp);
              }
              (void)fclose(ofp);
            } else (void)printf("Could not open %s for output.\n",result);
            (void)fclose(ifp);
          } else (void)printf("Could not open %s for input.\n",result);
        }
      }
    }
    status=lib$find_file_end(&ctx);
    if (!found) (void)printf("No files found. [%s]\n",argv[1]);
  } else {
   (void)printf("Syntax: MASS_REPLACE <filespec> <str1> <str2>\n");
   (void)printf("  Replaces all lines containing <string1> with <str2> in <filespec>\n");
  }
}


Ron Rockwell                           
rrockwell@umkc.edu