Emacs中的文本批量处理? [英] Idomatic batch processing of text in Emacs?
问题描述
在Python中,您可能会执行类似于
fout = open('out','w')
fin = open('in')
for fin:
fout.write(process(line)+\\\
)
fin.close()
fout.close()
(我认为在许多其他语言也是类似的)。
在Emacs Lisp中,你会做一些像
(find-file'out')
setq fout(current-buffer)
(find-file'in')
(setq fin(current-buffer)
(whileLine
(setq begin(point))
(move-end-of-line 1)
(setq line(buffer-substring-no-properties begin(point))
;;也许
(print(process line) fout)
;;或
(save-excursion
(set-buffer fout)
(insert(process line)))
(setq moreLines(=转发线1))))
(kill-buffer fin)
(kill-buffer fout)
我从 Emacs Lisp:处理文件中获得灵感(和代码)或者我应该尝试完全不同的东西,以及如何从打印声明中删除
如果您确实要批量处理 stdin
和sendin将结果转到 stdout
,您可以使用 - 脚本 Emacs的命令行选项,这将使您可以编写从 stdin
并写入 stdout
和 stderr
。
这里是一个示例程序,就像 cat
,除了它反转每一行:
#!/ usr / local / bin / emacs --script
;; - * - mode:emacs-lisp; - * -
(defun process(string)
只是反转字符串
(concat(nreverse string-to-list string))))
(condition-case nil
(let(line)
;;评论b / c与猫不相关,但可能有用的
;; (princargv is)
;; (princ argv)
;; (princ\\\
)
;; (princcommand-line-args is)
;; (princ command-line-args)
;; (principal\\\
)
(while(setq line(read-from-minibuffer))
(princ(process line))
(princ \\ n)))
(error nil))
现在,如果你有文件名为 stuff.txt
其中包含
abcd
1234
xyz
并且你调用了上面写的shell脚本,这样(假设它被命名为 rcat
):
rcat<您将看到以下内容打印到stdout:
$($)
$ / code
dcba
4321
zyx
所以,与普遍的看法相反,你实际上可以在 stdin
上进行批处理文件处理,而不需要一次读取整个文件。
In Python, you might do something like
fout = open('out','w')
fin = open('in')
for line in fin:
fout.write(process(line)+"\n")
fin.close()
fout.close()
(I think it would be similar in many other languages as well).
In Emacs Lisp, would you do something like
(find-file 'out')
(setq fout (current-buffer)
(find-file 'in')
(setq fin (current-buffer)
(while moreLines
(setq begin (point))
(move-end-of-line 1)
(setq line (buffer-substring-no-properties begin (point))
;; maybe
(print (process line) fout)
;; or
(save-excursion
(set-buffer fout)
(insert (process line)))
(setq moreLines (= 0 (forward-line 1))))
(kill-buffer fin)
(kill-buffer fout)
which I got inspiration (and code) from Emacs Lisp: Process a File line-by-line. Or should I try something entirely different? And how to remove the ""
from the print statement?
解决方案 If you actually want batch processing of stdin
and sending the result to stdout
, you can use the --script command line option to Emacs, which will enable you to write code that reads from stdin
and writes to stdout
and stderr
.
Here is an example program which is like cat
, except that it reverses each line:
#!/usr/local/bin/emacs --script
;;-*- mode: emacs-lisp;-*-
(defun process (string)
"just reverse the string"
(concat (nreverse (string-to-list string))))
(condition-case nil
(let (line)
;; commented out b/c not relevant for `cat`, but potentially useful
;; (princ "argv is ")
;; (princ argv)
;; (princ "\n")
;; (princ "command-line-args is" )
;; (princ command-line-args)
;; (princ "\n")
(while (setq line (read-from-minibuffer ""))
(princ (process line))
(princ "\n")))
(error nil))
Now, if you had a file named stuff.txt
which contained
abcd
1234
xyz
And you invoked the shell script written above like so (assuming it is named rcat
):
rcat < stuff.txt
you will see the following printed to stdout:
dcba
4321
zyx
So, contrary to popular belief, you can actually do batch file processing on stdin
and not actually have to read the entire file in at once.
这篇关于Emacs中的文本批量处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!