File 如何在common lisp中进行批量文件编辑?
我想知道如何在CommonLisp中批量编辑文件。不久前我需要这个,并使用perl和bash来实现它。出于好奇,我想知道常见的lisp解决方案 我使用了以下方法:File 如何在common lisp中进行批量文件编辑?,file,common-lisp,File,Common Lisp,我想知道如何在CommonLisp中批量编辑文件。不久前我需要这个,并使用perl和bash来实现它。出于好奇,我想知道常见的lisp解决方案 我使用了以下方法: find -name '*.lisp' -execdir perl -0777 -pi.bak -e 's/foo/bar/mi' '{}' '+' 它就像一个符咒 上面的命令将目录(及其子目录)中的所有文件馈送到perl程序中。perl程序搜索regex“foo”并将其替换为regex“bar”,然后将新的(编辑过的)文件保存到位
find -name '*.lisp' -execdir perl -0777 -pi.bak -e 's/foo/bar/mi' '{}' '+'
它就像一个符咒
上面的命令将目录(及其子目录)中的所有文件馈送到perl程序中。perl程序搜索regex“foo”并将其替换为regex“bar”,然后将新的(编辑过的)文件保存到位
感谢您为CL解决方案提供的指导。从,foo和bar开始,写下内容,然后。。。其余.-) 您可以使用
sbcl--noinfo--quit--eval
而不是perl
至于脚本内容,类似这样的内容应该可以工作:
(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"))))
或者,如果可以逐行输入文件:
(progn
(require :cl-ppcre)
(princ (ppcre:regex-replace-all "foo" (read-line) "bar")))
unix命令行是一种很好的方法,但是如果必须使用Lisp,请查看
mapcar
,目录
,打开文件
,读取字符
,读取行
,写入字符
,写入行
,和替换位于的手册中的。您可能希望使用简单的文字来解释上述行的作用,但不要假设每个人都知道