awk '{print $2,",",$1}'在 Emacs Lisp 中?
有时,我使用 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您打算如何使用这个?您打算将其用作命令行脚本吗?在这种情况下,您需要像 hello world 问题 那样打包它。
或者,您是否计划以交互方式使用它,在这种情况下您可能希望在新缓冲区中输出...
此代码完成了基础知识。您需要更新它以匹配您的使用模型。
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.
我采用了 Trey 的解决方案并生成了一个从 Unix shell 运行的脚本。它不接受命令行参数,因为我不确定如何将 command-line-args-left 结果转换为正确的参数。
I took Trey's solution and produced a script that runs from a Unix shell. It doesn't take command line parameters because I wasn't sure how to convert the command-line-args-left results into a proper parameter.
使用
dash.el
和s.el
:默认情况下,awk 将文本视为记录序列(以换行符分隔),每个记录都是一系列字段(用空格分隔)。因此,在上面的示例中,
c
是记录ab c
的一个字段。函数print-columns
接收文本,用换行符分隔s-lines
,从每条记录中选择某些字段,用逗号将它们与s-join
,用换行符连接结果。最重要的功能是dash
的-select-by-indices
,通过索引从列表中选取元素,并以与索引列表相同的顺序返回:Use functions from
dash.el
ands.el
:By default, awk treats text as a sequence of records (separated by newline), each record being a sequence of fields (separated by space). So in the above example
c
is a field of the recorda b c
. The functionprint-columns
receives a text, separates by newlines withs-lines
, selects certain fields from each record, joins them with a comma withs-join
, joins the result with newlines. The most important function isdash
's-select-by-indices
, which picks elements from a list by their indexes and returns in the same order as the list of indices: