Globally replace text in several files: Difference between revisions

m
→‎version 1: used zero-based indexing (to make program simplier).
m (→‎version 1: added/changed comments, indentations, and whitespace.)
m (→‎version 1: used zero-based indexing (to make program simplier).)
Line 989:
===version 1===
This example works under "DOS" and/or "DOS" under Microsoft Windows.
<br><br>FilenamesFile names that contain blanks should have their blanks replaced with commas.
<lang rexx>/*REXX program reads the files specified and globally replaces a string. */
old= "Goodbye London!" /*the old text to be replaced. */
Line 1,000:
call linein fn,1,0 /*position the file for input. */
changes=0 /*the number of changes in file so far.*/
do rec=10 while lines(fn)\==0 /*read a file (if it exists). */
@.rec=linein(fn) /*read a record (line) from the file. */
if pos(old, @.rec)==0 then iterate /*Anything to change? No, then skip. */
Line 1,006:
@.rec=changestr(old, @.rec, new) /*change the @.rec record, old ──► new.*/
end /*rec*/
 
rec=rec-1 /*adjust number records (because of DO)*/
say '──────── file has been read: ' fn", with " rec 'records.'
if changes==0 then do; say '──────── file not changed: ' fn; iterate; end
Line 1,012:
say '──────── file being changed: ' fn
 
do r=10 for rec; call lineout fn, @.r /*re─write the contents of the file. */
end /*r*/