【问题标题】:emacs - add decimal value to column of numbersemacs - 将十进制值添加到数字列
【发布时间】:2012-08-16 19:14:23
【问题描述】:

我正在处理一些具有以下结构的 nastran 输入文件:

GRID,1,,0.000,0.000,0.000,0
GRID,2,,0,000,1.653,0.000,0

我想做的是在单个列中添加一个特定的值,例如,将 1.653 添加到第 4 列并得到:

GRID,1,,0.000,1.653,0.000,0
GRID,2,,0,000,3.306,0.000,0

我找到了一些示例,展示了如何为 integers 执行此操作,但似乎无法使其适用于我上面概述的示例。

提前感谢您的帮助!

【问题讨论】:

    标签: emacs macros


    【解决方案1】:

    使用链接问题中的答案,可以像这样使用替换来完成,例如:

    C-M-%

    ^\([^,]*,[^,]*,[^,]*,[^,]*,\)\([^,]*\)\(.*\)

    返回

    \,(concat \1 (number-to-string (+ 1.653 (string-to-number \2))) \3)

    请注意,在您的示例中,第二行的列(逗号)比第一行多。可能是笔误。

    【讨论】:

    • 是的,额外的评论应该是句号。感谢您的回复。有没有关于如何构建这些表达式的好资源?我认为这是我的主要问题......
    • 可能有这样的资源,但我现在不能告诉你。您可能想询问 emacs 帮助列表中的人:news.gmane.org/gmane.emacs.help
    【解决方案2】:

    分别作为命令

    (defun raise-column ()
      (interactive "*")
      (save-excursion
        (save-restriction
          (widen)
          (goto-char (point-min))
          (while (re-search-forward "^\\([^,]*,[^,]*,[^,]*,[^,]*,\\)\\([^,]*\\)\\(.*\\)" nil t 1)
            (replace-match (concat (match-string-no-properties 1) (number-to-string (+ 1.653 (string-to-number (match-string-no-properties 2)))) (match-string-no-properties 3)))))))
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多