gpt4 book ai didi

awk '{print $2,",",$1}' 在 Emacs Lisp 中?

转载 作者:行者123 更新时间:2023-12-04 21:26:43 36 4
gpt4 key购买 nike

有时我会使用 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

sample filename.txt :
a   b  c
1 2 5

示例输出:
b , a
2 , 1

最佳答案

你打算如何使用它?您是否打算将其用作命令行脚本?在这种情况下,您需要像这样打包它 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)))

关于awk '{print $2,",",$1}' 在 Emacs Lisp 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2260294/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com