2010-02-14 10 views
5

간혹 AWK를 사용하여 데이터 파일의 열을 추출 및/또는 역순으로 사용합니다.

awk '{print $2,",",$1}' filename.txt 

어떻게하면 Emacs Lisp을 사용하여 동일한 작업을 수행 할 수 있습니까?

(defun awk (filename col1 &optional col2 col3 col4 col5) 
    "Given a filename and at least once column, print out the column(s) 
values in the order in which the columns are specified." 
... 
) 
;; Test awk 
(awk "filename.txt" 1); Only column 1 
(awk "filename.txt" 2 1); Column 2 followed by column 1 
(awk "filename.txt" 3 2 1); Columns 3,2 then 1 

샘플 filename.txt :

a b c 
1 2 5 

샘플 출력 :

b , a 
2 , 1 

답변

2

어떻게 당신이 사용하고자합니까? 명령 줄 스크립트로 사용할 계획입니까? 어떤 경우에는 hello world question처럼 패키지해야합니다.

또는, 당신은

이 코드는 다 기초를 얻을 수 ... 경우에 당신은 아마 새로운 버퍼의 출력을하려면, 대화 형을 사용할 계획이다. 사용 모델에 맞게 업데이트해야합니다.

(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))) 
+0

내가 들어'그와 전류 - buffer'해야 'save-excursion'과'set-buffer' 대신에 사용할 수 있습니다. – pheaver

+0

"(-1+ ...)"이 오타가 있습니까? –

+0

@melling 예, 감사합니다. –

0

나는 Trey의 솔루션을 가져 와서 유닉스 쉘에서 실행되는 스크립트를 만들었습니다. 명령 줄 인수가 인 결과를 적절한 매개 변수로 변환하는 방법을 잘 모르기 때문에 명령 줄 매개 변수를 사용하지 않습니다.

 

#!/usr/bin/emacs --script 

;; ./awk.el; # Change the last line of this file to contain the desired values. 
;; 
(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 
       (princ ", ")) 
      (princ (nth (1- (car c)) things)) 
      (setq comma t) 
      (setq c (cdr c))) 
      (princ "\n") 
      (forward-line)))) 
    (kill-buffer buf))) 

(awk "/tmp/foo.txt" 2 1) 
 
0

사용 dash.els.el에서 기능 기본적으로

(defun print-columns (s &rest is) 
    (s-join "\n" 
      (--map (s-join ", " 
         (-select-by-indices is (cons it (s-split " " it t)))) 
       (s-lines s)))) 

(print-columns "a b c\n1 2 3" 3 2 1 0) ; output: 
;; c, b, a, a b c 
;; 3, 2, 1, 1 2 3 

은 (개행 문자 구분) 레코드들의 시퀀스로서 취급 텍스트 awk가 필드의 연속되는 각각의 레코드 (공백으로 구분) . 위의 예에서 c은 레코드 a b c의 필드입니다. print-columns은 텍스트를 받고 s-lines으로 개행 문자로 구분하고 각 레코드의 특정 필드를 선택하고 s-join과 쉼표로 결합하여 결과를 개행 문자와 결합합니다. 가장 중요한 기능은 인덱스의 목록과 같은 순서로 자신의 인덱스를 반환하여 목록에서 요소를 선택합니다 dash-select-by-indices입니다 :

(-select-by-indices '(2 1 0) '(a b c d e)) ; => (c b a)