0

我有一个脏报告文件,其中包含格式不正确的负数。最终目标是将这些导入 Excel 进行分析。我正在使用 BBEdit 在导入 Excel 之前清理报告。我想创建一个苹果脚本来循环遍历报告并将“-”从数字的后面移到前面。

脏报告示例行:

B-EXCAL 02 3684        2.0000-      49.02-       108.00-        58.98-  54.6-
B-MISMH 09-3300       33.0000      722.91       1353.00        630.09   46.6 

期望的输出:

B-EXCAL 02 3684       -2.0000      -49.02       -108.00        -58.98  -54.6
B-MISMH 09-3300       33.0000      722.91       1353.00        630.09   46.6 

我有 JavaScript 和 VBScript 经验,所以我对脚本进行成像,类似于这个 psudo 脚本:

Get contents of current window from BBEdit
   for each word in contents
       if char(len(word)) = "-"
           newWord = "-" + rightTrim(word, 1) 
           replace(word, newWord)
       end if
   end for
end

这是我第一次使用 AppleScript,我完全不知所措。

谢谢您的帮助/

4

2 回答 2

1

我不确定您是否需要 AppleScript。BBEdit 的查找/替换窗口会为此工作吗?尝试以下操作:

查找:([0-9\.]+)\-
替换:\-\1

“Grep”和“Wrap around”应该是“替换”文本区域下方唯一选择的内容。然后单击“全部替换”。

如果您需要一次对一堆报告执行此操作,请使用相同的查找/替换模式和多文件搜索。如果我没有正确理解您的问题,请告诉我,但我认为您可以在没有 AppleScript 的情况下完成此操作。

于 2012-01-06T01:25:53.807 回答
0

尝试这个。我假设您可以将来自 bbedit(或任何地方)的文本作为变量“originalText”获取到 applescript。然后你必须把“fixedText”放回它所属的地方。注意:在我的代码中,我假设“制表符”字符将 origText 每一行中的单词/列分开,因为 Michael J. Barber 已将其格式化。如果它是另一个字符(如空格字符),那么您必须将代码中的单词tab更改为space

set originalText to "B-EXCAL 02 3684    2.0000- 49.02-  108.00- 58.98-  54.6-
B-MISMH 09-3300 33.0000 722.91  1353.00 630.09  46.6"
set fixedText to fixNegativeSigns(originalText)

on fixNegativeSigns(theText)
    set listText to paragraphs of theText
    set fixedText to ""

    set {tids, text item delimiters} to {text item delimiters, tab}
    repeat with i from 1 to count of listText
        set listItems to {}
        set thisList to text items of (item i of listText)
        repeat with j from 1 to count of thisList
            set thisItem to item j of thisList
            if text -1 of thisItem is "-" then
                set thisItem to "-" & text 1 thru -2 of thisItem
            end if
            set end of listItems to thisItem
        end repeat
        set fixedText to fixedText & (listItems as text) & character id 10
    end repeat
    set text item delimiters to tids
    return text 1 thru -2 of fixedText
end fixNegativeSigns

注意:在上面测试我的代码时,您应该将其复制/粘贴到 Applescript 编辑器中。您将不得不修复originalText中的制表符,因为它们无法在复制/粘贴中保留下来。所以删除单词/列之间的空格并插入一个制表符。然后代码将正常工作。

于 2011-09-23T05:03:38.663 回答