tags:

views:

267

answers:

2
+1  A: 

How do you intend to use this? Are you planning on using it as a command-line script? In which case, you'll need to package it like this hello world question.

Or, are you planning on using it interactively, in which case you probably want the output in a new buffer...

This code gets the basics done. You'll need to update it to match your usage model.

(defun awk (filename &rest cols)
  "Given a filename and at least once column, print out the column(s) values
in the order in which the columns are specified."
  (let* ((buf (find-file-noselect filename)))
    (with-current-buffer buf
      (while (< (point) (point-max))
        (let ((things (split-string (buffer-substring (line-beginning-position) (line-end-position))))
              (c cols)
              comma)
          (while c
            (if comma
                (print ", "))
            (print (nth (1- (car c)) things))
            (setq comma t)
            (setq c (cdr c)))
          (print "\n")
          (forward-line))))
    (kill-buffer buf)))
Trey Jackson
I've heard that `with-current-buffer` should be used instead of `save-excursion` and `set-buffer`.
pheaver
Is "(-1+ ...)" a typo?
@melling Yes, thanks.
Trey Jackson
Why are you printing the newline after each column? Surely you mean to print it after printing all the selected columns from a line?
A. Levy
@A.Levy Surely. Updated
Trey Jackson
A: