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
stdinand sending the result tostdout, you can use the –script command line option to Emacs, which will enable you to write code that reads fromstdinand writes tostdoutandstderr.Here is an example program which is like
cat, except that it reverses each line:Now, if you had a file named
stuff.txtwhich containedAnd you invoked the shell script written above like so (assuming it is named
rcat):you will see the following printed to stdout:
So, contrary to popular belief, you can actually do batch file processing on
stdinand not actually have to read the entire file in at once.