How to bulk edit files in general lisp?

I would like to know how to bulk edit common lisp files. I needed this a while ago and used perl and bash for this. I would like to learn about the general lisp solution out of curiosity.

I used the following:

find -name '*.lisp' -execdir perl -0777 -pi.bak -e 's/foo/bar/mi' '{}' '+' 

and it worked like a charm.

The above command transfers all the files in the directory (and its subdirectories) to the perl program. The perl program looks for the regular expression "foo" and replaces it with the regular expression bar, and then saves the new (edited) file in place.

Thank you for any recommendations you can provide in the CL solution.

+4
source share
2 answers

How to start with a walk-directory , slurp file , close the file , replace foo with the panel, write the contents back, and then ... relax .; -)

+2
source

Instead of perl you can use sbcl --noinform --quit --eval .

As for the contents of the script, something like this should work:

 (progn (require :cl-ppcre) (let* ((file (nth 4 *posix-argv*)) (buf (make-array (file-length file) :element-type 'character :adjustable t :fill-pointer t))) (setf (fill-pointer buf) (with-open-file (in file) (read-sequence buf in))) (princ (ppcre:regex-replace-all "foo" buf "bar")))) 

Or, if you can feed the file line by line:

 (progn (require :cl-ppcre) (princ (ppcre:regex-replace-all "foo" (read-line) "bar"))) 
+1
source

Source: https://habr.com/ru/post/1400905/


All Articles