Globally replace text in several files: Difference between revisions

m
→‎Version 1: changed comments and indentation, added whitespace, simplified the program, made the (hdr) eyecatcher better. -- ~~~~
(Added Vedit macro language)
m (→‎Version 1: changed comments and indentation, added whitespace, simplified the program, made the (hdr) eyecatcher better. -- ~~~~)
Line 649:
 
=={{header|REXX}}==
===Versionversion 1===
This example works under "DOS" and/or "DOS" under Microsoft Windows.
<lang rexx>/*REXX program to read the files specified and globally replace a string*/
old = 'Goodbye London!' /*old text to be replaced. */
new = 'Hello New York!' /*new text used for replacement. */
parse arg fileList; files=words(fileList); pad=left('',20)
hdr='────── file' /*eyecatcher.*/
 
do f=1 for files; aFile=translate(word(fileList, f),,','); say; say
say hdr'... file is being read: ' aFile pad "("f 'out of' files "files)."
call linein aFile,1,0 /*position the file for input. */
changes=0 /*number of# changes in the file. (so far).*/
 
do kj=1 while lines(aFile)\==0 /*read a file (if it exists). */
@.kj = linein(aFile) /*read a record from the file. */
if pos(old,@.kj)==0 then iterate /*Anything to change? No, skip.*/
changes = changes+1 /*bump the change counter. */
@.kj = changestr(old,@.kj,new) /*change this record, old ──► new*/
end /*kj*/
 
recs=k-1
say '... file was being read: ' aFile", with " recs 'records.'
if changes==0 then do
say '... file not changed: ' aFile
iterate /*f*/
end
 
say hdr '... file was being was read: ' aFile", with " recs j-1 'records.'
if changes == 0 then do
say '... filesay hdr ' not changed: ' aFile
end iterate /*f*/
iterate /*f*/ end
call lineout aFile,,1 /*position the file for output. */
say hdr '... file being changed: ' aFile
do r=1 for j-1; call lineout aFile,@.r; end /*re-write this record to outputfile. */
 
say hdr '...was file was changed: ' aFile " with" changes 'lines changed.'
do r=1 for recs /*write the (whole) file. */
call lineout aFile,@.r /*write this record to output. */
end /*r*/
 
say '... file was changed: ' aFile " with" changes 'lines changed.'
say
end /*f*/
/*stick a fork in it, we're done.*/</lang>
Line 715 ⟶ 708:
'''output''' when using the input of: <tt> one.txt two.txt </tt>
<pre style="overflow:scroll">
...────── file is being read: one.txt (1 out of 2 files).
...────── file was being was read: one.txt, with 3213 records.
... ────── file notbeing changed: one.txt
────── file was changed: one.txt with 2 lines changed.
 
 
...────── file is being read: two.txt (2 out of 2 files).
...────── file was being was read: two.txt, with 21259 records.
... ────── file being changed: two.txt
... ────── file was was changed: two.txt with 56 lines changed.
</pre>