Replace all occurrences of a regular expression within a file.

  (define (regexp-substitute/file file re . items)
    (let ((file-contents
           (call-with-input-file file
               (lambda (in) 
                 (read-string (file-size file) in)))))
      (call-with-output-file file
          (lambda (out)
            (apply regexp-substitute/global out re file-contents items)))))

----

A variant: perform a couple of simple string substitutions on a file (and don't worry about undefined behaviour or robustness in general, roll on, drunk, dazed, and confused, in a skimpy leather mini, your black beard untrimmed, remainders of soup and the few copper coins splashed over the boulevard). Tried in scsh 0.6.7.

 ;; substis : proper list of (from . to) pairs of strings
 (define (apply-substitutions-to-file substis fname)
   (let ((content/substis
          (with-input-from-file fname
            (lambda ()
              (fold (lambda (substi content)
                      (regexp-substitute/global #f (rx ,(car substi))
                                                content
                                                'pre (cdr substi) 'post))
                    (read-delimited "")
                    substis)))))
     (with-output-to-file fname ; truncate
       (lambda ()
         (display content/substis)))))

 (define (apply-substitutions-to-files substis fnames)
   (for-each (lambda (fname)
               (apply-substitutions-to-file substis fname))
             fnames))

 #!
 Examples

 (apply-substitutions-to-file '(("&eacute;" . "\" eacute \"")
                                ("&gt;" . "\" > \""))
                              "../pages/demo.scm")

 (apply-substitutions-to-files '(("&eacute;" . "\" eacute \"")
                                 ("&Eacute;" . "\" Eacute \"")
                                 ("&egrave;" . "\" egrave \"")
                                 ("&agrave;" . "\" agrave \"")
                                 ("&Agrave;" . "\" Agrave \"")
                                 ("&ograve;" . "\" ograve \"")
                                 ("&ugrave;" . "\" ugrave \"")
                                 ("&iacute;" . "\" iacute \"")
                                 ("&szlig;" . "\" szlig \"")
                                 ("&uuml;" . "\" uuml \"")
                                 ("&ouml;" . "\" ouml \"")
                                 ("&auml;" . "\" auml \"")
                                 ("&acirc;" . "\" acirc \"")
                                 ("&ecirc;" . "\" ecirc \"")
                                 ("&laquo;" . "\" laquo \"")
                                 ("&raquo;" . "\" raquo \"")
                                 ("&ldquo;" . "\" ldquo \"")
                                 ("&rdquo;" . "\" rdquo \"")
                                 ("&lsquo;" . "\" lsquo \"")
                                 ("&rsquo;" . "\" rsquo \"")
                                 ("&bdquo;" . "\" bdquo \"")
                                 ("&mdash;" . "\" mdash \"")
                                 ("&#8211;" . "\" 8211 \"")
                                 ("&#8212;" . "\" 8212 \"")
                                 ("&#8220;" . "\" 8220 \"")
                                 ("&#8221;" . "\" 8221 \"")
                                 ("&gt;" . "\" > \""))
                               (glob "../pages/*.scm"))
 !#

