我想寫Python的正則表達式搜索的Common Lisp的版本和替換,以文件的就地修改:替換正則表達式,就地,與Common Lisp的
import fileinput, re
for line in fileinput.input(inplace=1, backup='.bak'):
line = re.sub(r"foo", "bar", line, re.M)
print (line)
這是常見的Lisp代碼我能想出來:
(require :cl-ppcre)
(defun in-place-subst (file)
(with-open-file (stream file :direction :io :if-exists :overwrite)
(loop for line = (read-line stream nil)
while line do
(write-line (cl-ppcre:regex-replace-all "foo" line "bar") stream))))
它的工作,有點。現在替換文本將被添加到文件末尾。我眼前的問題是我無法弄清楚如何將替換成的內容。
爲了更好的解釋,如果file.txt
包含:
1 foo
2 bar
3 foobar
調用
(in-place-subst "file.txt")
後,我得到:
1 foo
2 bar
3 foobar
1 bar
2 bar
3 barbar
取而代之的是正確的更換:
1 bar
2 bar
3 barbar
我試圖用一切可能的with-open-file
選項(從Successful Lisp),沒有成功:
Keyword Value Action if File Exists
---------- ------------------ ---------------------------------------
:IF-EXISTS NIL return NIL
:IF-EXISTS :ERROR signal an error
:IF-EXISTS :NEW-VERSION next version (or error)
:IF-EXISTS :RENAME rename existing, create new
:IF-EXISTS :SUPERSEDE replace file upon CLOSE
:IF-EXISTS :RENAME-AND-DELETE rename and delete existing, create new
:IF-EXISTS :OVERWRITE reuse existing file (position at start)
:IF-EXISTS :APPEND reuse existing file (position at end)
可能有人請給我在正確的方向,使該功能將在正確的方式呈現file.txt
?
另外,什麼是常見Lisp 慣用這樣做的方式,當然假設cl-ppcre
可用?
是否有更簡潔的方式使用Common Lisp進行就地正則表達式替換?
'FILE-POSITION'讀取並設置文件位置。你可能想試驗一下。 –
謝謝您的建議。我嘗試過使用'(文件位置流:啓動)',嘗試重置流的文件位置,沒有太多進展。我相信可以用更多的代碼來做到這一點(也許在內存中做一個副本,做替換和寫回),但是我希望找到一個更簡潔,也許更習慣的方式。 – gsl
保存文件位置。閱讀該行。將文件位置重置爲開頭。寫新的一行。請注意,它不會更改行的大小或擴展文件。如果你的更換時間更短或更長,那麼你需要處理... –