tags:

views:

125

answers:

3

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 here. Or should I try something entirely different? And how to remove the "" from the print statement? Thanks!

+4  A: 

Here's what I came up with. Looks a lot more idiomatic to me:

(with-temp-buffer
  (let ((dest-buffer (current-buffer)))
    (with-temp-buffer
      (insert-file-contents "/path/to/source/file")
      (while (search-forward-regexp ".*\n\\|.+" nil t)
        (let ((line (match-string 0)))
          (with-current-buffer dest-buffer
            (insert (process line)))))))
  (write-file "/path/to/dest/file" nil))
Sean
+1  A: 

Emacs Lisp is not suitable for processing file-streams. The whole file must be read at once:

(defun my-line-fun (line)
  (concat "prefix: " line))

(let* ((in-file "in")
       (out-file "out")
       (lines (with-temp-buffer 
        (insert-file-contents in-file)
        (split-string (buffer-string)  "\n\r?"))))
  (with-temp-file out-file
    (mapconcat 'my-line-fun lines "\n")))
Jürgen Hötzel
`split-string` without any arguments defaults to splitting on `split-string-default-separators`, which is `"[ \f\t\n\r\v]+"` by default. You probably want to pass `"[\n\r]+"` in explicitly as the second argument.
haxney
And technically, "Emacs Lisp is not suitable for processing file-streams" is not true; you could use a process filter, but it is much more complicated and reading the whole file at once is probably the easiest way to do things. If reading a stream (such as a network socket) is really needed, you would probably have to use a process filter (see the Elisp manual).
haxney
Thanks: fixed split-string usage.
Jürgen Hötzel
Actually, you can process `stdin`.
Trey Jackson
+4  A: 

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.

Trey Jackson
...Fantastic...
Stephen