Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8463977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:36:03+00:00 2026-06-10T14:36:03+00:00

What’s the lisp way of replacing a string in a file. There is a

  • 0

What’s the lisp way of replacing a string in a file.

There is a file identified by *file-path*, a search string *search-term* and a replacement string *replace-term*.

How to make file with all instances of *search-term*s replaced with *replace-term*s, preferably in place of the old file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:36:05+00:00Added an answer on June 10, 2026 at 2:36 pm

    One more take at the problem, but few warnings first:

    1. To make this really robust and usable in the real-life situation you would need to wrap this into handler-case and handle various errors, like insufficient disc space, device not ready, insufficient permission for reading / writing, insufficient memory to allocate for the buffer and so on.

    2. This does not do regular expression-like replacement, it’s simple string replacement. Making a regular expression based replacement on large files may appear far less trivial than it looks like from the start, it would be worth writing a separate program, something like sed or awk or an entire language, like Perl or awk 😉

    3. Unlike other solutions it will create a temporary file near the file being replaced and will save the data processed so far into this file. This may be worse in the sense that it will use more disc space, but this is safer because in case the program fails in the middle, the original file will remain intact, more than that, with some more effort you could later resume replacing from the temporary file if, for example, you were saving the offset into the original file in the temporary file too.


    (defun file-replace-string (search-for replace-with file
                                &key (element-type 'base-char)
                                  (temp-suffix ".tmp"))
      (with-open-file (open-stream
                       file
                       :direction :input
                       :if-exists :supersede
                       :element-type element-type)
        (with-open-file (temp-stream
                         (concatenate 'string file temp-suffix)
                         :direction :output
                         :element-type element-type)
          (do ((buffer (make-string (length search-for)))
               (buffer-fill-pointer 0)
               (next-matching-char (aref search-for 0))
               (in-char (read-char open-stream nil :eof)
                        (read-char open-stream nil :eof)))
              ((eql in-char :eof)
               (when (/= 0 buffer-fill-pointer)
                 (dotimes (i buffer-fill-pointer)
                   (write-char (aref buffer i) temp-stream))))
            (if (char= in-char next-matching-char)
                (progn
                  (setf (aref buffer buffer-fill-pointer) in-char
                        buffer-fill-pointer (1+ buffer-fill-pointer))
                  (when (= buffer-fill-pointer (length search-for))
                    (dotimes (i (length replace-with))
                      (write-char (aref replace-with i) temp-stream))
                    (setf buffer-fill-pointer 0)))
                (progn
                  (dotimes (i buffer-fill-pointer)
                    (write-char (aref buffer i) temp-stream))
                  (write-char in-char temp-stream)
                  (setf buffer-fill-pointer 0)))
            (setf next-matching-char (aref search-for buffer-fill-pointer)))))
      (delete-file file)
      (rename-file (concatenate 'string file temp-suffix) file))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.