Want a replacemet script

Started by aron, May 21, 2006, 09:40:01 PM

Previous topic - Next topic

aron

Hi I'm quite new to this newLISP thing, but I was thinking mabe make a good replacement script/program, someone maybe alredy done one?



This is what I want:



Arguments:

Start string, End String, Replace string/textfile, file_list_to_replace_in



Description:

Replaces everyting betwean Start string and End string with the text from the replace string/textfile in all files from the file list and on all places in those files.

 

Some kind of example:
(replace-strings '<div class="thing" id="start">' '<!-- end of start -->' 'start.txt' ('index.html' 'page1.html' 'page3.html'))


Replaces everyting betwean all <div class="thing" id="start"> and <!-- end of start --> with the text from the start.txt in index.html,page1.html and page3.html.

 

Mabe make an option with a target file were a new file is created instead of changing the first one... this sounds easier.



All ideeas are wery appreciateed
newLISP browser: (print (replace \"<[^>]*>\" (get-url (read-line)) \"\" 0))

HJH

#1
Quote from: "aron"


All ideeas are wery appreciateed


Well, perhaps

http://www.newlisp.org/DesignPatterns.html#text_processing">http://www.newlisp.org/DesignPatterns.h ... processing">http://www.newlisp.org/DesignPatterns.html#text_processing



is a start.



--HJH

Lutz

#2
This is more or less what you want:



(define (replace-string start-str end-str repl-str file-list)
    (dolist (fle (file-list))
        (set 'page (read-file fle))
        (replace (append start-str "(.*?)" end-str) ; regular expr. pattern
                 page                               ; text file
                (append start-str repl-str end-str) ; the replacement text
                        0)    ; regex option number
        (write-file fle page) ; write file back todisk
    )
)


I have not tried it, but think it contains all the important elements. The newLISP 'replace' function does all the work in the replacement part, appendind 'start-str', 'repl-str', 'end-st' to form the new string. I assume that 'start-str' and 'end-str' where also part of the replacement.



The function shown iterates over files and only does the work for one string to replace 'repl-str'. You could have another 'dolist' inside the first one for the files to iterate over different versions of 'rep-str'.



Lutz



ps: the link HJH gave you describes the same 'replace' techique of doing the work in the replacement expression to collect links in a web-page.

aron

#3
Thanks for the code Lutz, I still need some time to grasp it but it looks very nice. testing.. testing
newLISP browser: (print (replace \"<[^>]*>\" (get-url (read-line)) \"\" 0))

cormullion

#4
I just tested your code, Lutz (a job that I can do easier than write the code in the first place!). I think:


(dolist (fle (file-list))

should be


(dolist (fle file-list)

With that typo (?) fixed, the code worked perfectly! What's left is just a question of passing a list of pukka file-names. I used (directory) and (real-path), but the OP's MMV.