自从我转换为Emacs教会以来,我一直在尝试从内部做所有事情,我想知道如何快速高效地进行文本处理.
作为一个例子,让我们在几分钟前在org-mode上编辑我正在编辑的列表.
** Diego: b QI ** bruno-gil: b QI ** Koma: jo ** um: rsrs pr0n ** FelipeAugusto: esp ** GustavoPupo: pinto tr etc ** GP: lit gtk ** Alan: jo mil pc ** Jost: b hq jo 1997 ** Herbert: b rsrs pr0n ** Andre: maia mil pseudo ** Rodrigo: c ** caue: b rsrs 7arte pseudo ** kenny: cri gif ** daniel: gtk mu pr0n rsrs b ** tony: an 1997 esp ** Vitor: b jo mimimi ** raphael: b rpg 7arte ** Luca: b lit gnu pc prog mmu 7arte 1997 ** LZZ: an qt ** William: b an jo pc 1997 ** Epic: gtk ** Aldo: b pseudo pol mil fur ** GustavoKyon: an gtk ** CarlosIsaksen : an hq jo 7arte gtk 1997 ** Peter: pseudo pol mil est 1997 gtk lit lang ** leandro: b jo cb ** frederico: 7arte lit gtk ** rol: b an pseudo mimimi 7arte ** mathias: jo lit ** henrique: 1997 h gtk qt ** eumané: an qt ** walrus: cri de ** FilipePinheiro: lit pseudo ** Igor: pseudo b ** Erick: b jo rpg q 1997 gtk ** Gabriel: pr0n rsrs qt ** george: clo mimimi ** anão: hq jo 1997 rsrs clô b ** jeff: 7arte gtk ** davidatenas: an 7arte 1997 esp qt ** HHahaah: b ** Eduardo: b
在bash中,我首先用单引号回显整个粘贴的东西,然后将其传递给awk,循环遍历每一行并将每个部分添加到正确的临时变量中,然后将其弄乱,直到它像我想要的那样.
echo '** Diego: b QI ** bruno-gil: b QI ** Koma: jo ** um: rsrs pr0n ** FelipeAugusto: esp ** GustavoPupo: pinto,tr etc ** GP: lit gtk ** Alan: jo mil pc ** Jost: b hq jo 1997 ** Herbert: b rsrs pr0n ** Andre: maia mil pseudo ** Rodrigo: c ** caue: b rsrs 7arte pseudo ** kenny: cri gif ** daniel: gtk mu pr0n rsrs b ** tony: an 1997 esp ** Vitor: b jo mimimi ** raphael: b rpg 7arte ** Luca: b lit gnu pc prog mmu 7arte 1997 ** LZZ: an qt ** William: b an jo pc 1997 ** Epic: gtk ** Aldo: b pseudo pol mil fur ** GustavoKyon: an gtk ** CarlosIsaksen : an hq jo 7arte gtk 1997 ** Peter: pseudo pol mil est 1997 gtk lit lang ** leandro: b jo cb ** frederico: 7arte lit gtk ** rol: b an pseudo mimimi 7arte ** mathias: jo lit ** henrique: 1997 h gtk qt ** eumané: an qt ** walrus: cri de ** FilipePinheiro: lit pseudo ** Igor: pseudo b ** Erick: b jo rpg q 1997 gtk ** Gabriel: pr0n rsrs qt ** george: clo mimimi ** anão: hq jo 1997 rsrs clô b ** jeff: 7arte gtk ** davidatenas: an 7arte 1997 esp qt ** HHahaah: b ** Eduardo: b ' | awk '{sub(":","");for (i=3;i<=NF;i++) members[$i] = members[$i] " " $2}; END{for (j in members) print j ": " members[j]}' | sort
……和TA-DA!预期的输出不到2分钟,以直观和渐进的方式完成.你能告诉我如何在elisp中做这样的事情,最好是在emacs缓冲区,优雅和简单吗?
谢谢!
解决方法
我要做的第一件事就是利用org-mode的标签支持.代替
** Diego: b QI
你将会拥有
** Diego :b:QI:
哪个组织模式识别为标签“b”和“QI”.
要将当前格式转换为标准组织模式,您可以使用
以下(假设您的源缓冲区称为“asdf”)
(with-current-buffer "asdf" (beginning-of-buffer) (replace-string " " ":") (beginning-of-buffer) (replace-string "**:" "** ") (beginning-of-buffer) (replace-string "::" " :") (beginning-of-buffer) (replace-string "\n" ":\n") (org-set-tags-command t t))
这不是很好或有效,但它完成了工作.
之后,您可以使用以下内容生成具有该格式的缓冲区
你想从shell脚本:
(let ((results (get-buffer-create "results")) tags) (with-current-buffer "asdf" (beginning-of-buffer) (while (org-on-heading-p) (mapc '(lambda (item) (when item (add-to-list 'tags item))) (org-get-local-tags)) (outline-next-visible-heading 1))) (setq tags (sort tags 'string<)) (with-current-buffer results (erase-buffer) (mapc '(lambda (item) (insert (format "%s: %s\n" item (with-current-buffer "asdf" (org-map-entries '(substring-no-properties (org-get-heading t)) item))))) tags) (beginning-of-buffer) (replace-regexp "[()]" "")))
这会将结果放在一个名为“results”的缓冲区中,如果没有,则创建它
已经存在.基本上,它正在收集缓冲区“asdf”中的所有标签,
对它们进行排序,然后循环遍历每个标记并搜索每个标题
“asdf”中的标记并将其插入“结果”.
通过一些清理,这可以成为一个功能;基本上只是用参数替换“asdf”和“results”.如果你需要那样做,我可以做到那.