How to resolve the algorithm Globally replace text in several files step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Globally replace text in several files step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

Replace every occurring instance of a piece of text in a group of text files with another one.

For this task we want to replace the text   "Goodbye London!"   with   "Hello New York!"   for a list of files.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Globally replace text in several files step by step in the Icon and Unicon programming language

Source code in the icon programming language

procedure main()
globalrepl("Goodbye London","Hello New York","a.txt","b.txt") # variable args for files
end

procedure globalrepl(old,new,files[])  

every fn := !files do 
   if s := reads(f := open(fn,"bu"),stat(f).size) then {
      writes(seek(f,1),replace(s,old,new))
      close(f)
      }
   else write(&errout,"Unable to open ",fn)
end

link strings # for replace


  

You may also check:How to resolve the algorithm Test a function step by step in the Java programming language
You may also check:How to resolve the algorithm Create an object at a given address step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the Nim programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Wren programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Common Lisp programming language