home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / vmsnet / tpu / 512 < prev    next >
Encoding:
Internet Message Format  |  1992-11-19  |  3.5 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!ux1.cso.uiuc.edu!moe.ksu.ksu.edu!mccall!info-tpu-newsgate!list
  2. Newsgroups: vmsnet.tpu
  3. Subject: Replacing strings in multiple files..
  4. Message-ID: <01GRDLG99ZS2000EQW@Post-Office.UH.EDU>
  5. From: "R.W.Bell" <bob_bell@uh.edu>
  6. Date: 20 Nov 1992 14:11:13 -0600 (CST)
  7. Reply-To: "R.W.Bell" <Bob_Bell@UH.EDU>
  8. Organization: The Internet
  9. Return-Path: <TPU-Mgr@SHSU.edu>
  10. Errors-To: TPU-Mgr@SHSU.edu
  11. X-Listname: Text Processing Utility (TPU) Language Discussion List
  12. X-Vms-To: UHOU::IN%"info-tpu@odin.shsu.edu"
  13. X-Vms-Cc: SINARRWB
  14. Mime-Version: 1.0
  15. Content-Transfer-Encoding: 7BIT
  16. Lines: 95
  17.  
  18.  
  19. Someone wanted to replace a string in multiple file at DCL ??
  20. Ken Selvia wrote this, works just dandy !!!
  21.  
  22. Enjoy..Bob Bell
  23.  
  24. ---------------------------------
  25.  
  26. $EDIT/TPU/NOSECTION/COMMAND=SYS$INPUT/NODISPLAY
  27. !
  28. ! This program is put into the public domain.
  29. ! You may do anything you want with it.
  30. !
  31. ! Send comments or suggestions to UCS_KAS@SHSUODIN  (BITNET)
  32. ! Ken Selvia
  33. ! 5-APR-1988
  34. !
  35. PROCEDURE tpu$init_procedure
  36. LOCAL next_file, search_string, replace_string,
  37.       lower_string, total_files, total_strings, changed,
  38.       search_size, case_match;
  39. SET(FACILITY_NAME, "REPLACE");
  40. SET(SUCCESS,OFF);
  41. IF GET_INFO (COMMAND_LINE,"FILE_NAME") = "" THEN
  42.     MESSAGE(" ");
  43.     MESSAGE(" REPLACE - Multiple file search and replace utility.");
  44.     MESSAGE(" ");
  45.     MESSAGE(" Usage:    REPLACE file.name  (wildcards allowed)");
  46.     MESSAGE("           SEARCH STRING");
  47.     MESSAGE("           REPLACEMENT STRING");
  48.     MESSAGE(" ");
  49.     QUIT;
  50. ENDIF;
  51. MESSAGE ("Enter search and replace strings.");
  52. MESSAGE ("The search is not case sensitive if search string is lower case.");
  53. search_string := READ_LINE ("Enter search string: ");
  54. replace_string := READ_LINE ("Enter replacement string: ");
  55. IF LENGTH (search_string) = 0 THEN
  56.         MESSAGE ("No search string given. Aborting...");
  57.         QUIT;
  58. ENDIF;
  59. total_files := 0;
  60. total_strings := 0;
  61. lower_string := search_string;
  62. CHANGE_CASE (lower_string,LOWER);
  63. case_match := (lower_string = search_string);
  64. search_size := LENGTH (search_string);
  65. work_buffer := CREATE_BUFFER ("work");
  66. POSITION (work_buffer);
  67. LOOP
  68.     ERASE (work_buffer);
  69.     next_file := FILE_SEARCH (GET_INFO (COMMAND_LINE,"FILE_NAME"));
  70.     EXITIF next_file = "";
  71.     READ_FILE (next_file);
  72.     POSITION (BEGINNING_OF (work_buffer));
  73.     SET (OUTPUT_FILE,CURRENT_BUFFER,FILE_PARSE (next_file));
  74.     changed := substitute(search_string,
  75.                           replace_string,
  76.                           search_size,
  77.                           case_match);
  78.     IF changed > 0 THEN
  79.         total_files := total_files + 1;
  80.         total_strings := total_strings + changed;
  81.         WRITE_FILE (work_buffer);
  82.     ENDIF;
  83.     MESSAGE (FAO ("Replaced !ZL string!%S in file !AS",changed,next_file));
  84. ENDLOOP;
  85. SET(NO_WRITE, work_buffer);
  86. MESSAGE (FAO ("Total files modified: !ZL", total_files));
  87. MESSAGE (FAO ("Total strings replaced: !ZL", total_strings));
  88. QUIT;
  89. ENDPROCEDURE
  90.  
  91. PROCEDURE substitute(target,replacement,size,ignore_case)
  92. local modified_count;
  93. ON_ERROR
  94.     RETURN (modified_count);
  95. ENDON_ERROR
  96. modified_count := 0;
  97. LOOP
  98.     IF ignore_case THEN
  99.         POSITION (SEARCH (target ,FORWARD,NO_EXACT));
  100.     ELSE
  101.         POSITION (SEARCH (target,FORWARD,EXACT));
  102.     ENDIF;
  103.     ERASE_CHARACTER (size);
  104.     COPY_TEXT (replacement);
  105.     modified_count := modified_count + 1;
  106. ENDLOOP;
  107. ENDPROCEDURE
  108. !    This line is only executed when compiling the TPU program.
  109.      SAVE ("sys$disk:replace");
  110.      QUIT;
  111. $REPLACE :== "EDIT/TPU/SECT=''F$ENVI(""DEFAULT"")'REPLACE/NODISP/NOCOMM"
  112. $EXIT
  113.