Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建AppleScript以循环浏览BBEdit文档并移动一个"-&引用;从数字的结尾到开头?_Applescript_Bbedit - Fatal编程技术网

如何创建AppleScript以循环浏览BBEdit文档并移动一个"-&引用;从数字的结尾到开头?

如何创建AppleScript以循环浏览BBEdit文档并移动一个"-&引用;从数字的结尾到开头?,applescript,bbedit,Applescript,Bbedit,我有一个脏报告文件,其中包含格式不正确的负数。最终目标是将这些导入Excel进行分析。我正在使用BBEdit在导入Excel之前清理报表。我想创建一个apple脚本来循环遍历报告,并将“-”从数字的后面移到前面 脏报告示例行: B-EXCAL 02 3684 2.0000- 49.02- 108.00- 58.98- 54.6- B-MISMH 09-3300 33.0000 722.91 1353.00

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

脏报告示例行:

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,我完全不知所措


谢谢你的帮助/

试试这个。我假设您可以将bbedit(或任何地方)中的文本作为变量“originalText”输入applescript。然后,您必须将“fixedText”放回它所属的位置。注意:在我的代码中,我假设“tab”字符将origText的每一行中的单词/列分隔开,正如Michael J.Barber当前对其进行格式化一样。如果是另一个字符(如空格字符),则必须将代码中的单词选项卡更改为空格

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中的制表符,因为它们在复制/粘贴过程中无法存活。因此,删除单词/列之间的空格并插入一个选项卡。然后代码将正常工作。

我不确定您是否需要AppleScript。BBEdit的“查找/替换”窗口是否适用于此?请尝试以下操作:

Find: ([0-9\.]+)\-
Replace: \-\1 查找:([0-9\.]+)\-
替换:\-\1 “Grep”和“Wrap-around”应该是在“Replace”文本区域下方选择的唯一内容。然后单击“全部替换”


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

您在这里对单词的定义是什么?空格被认为是每个单词的边界。。除了@mcgrailm所写的之外,awk,尤其是sed是完成这类任务的绝佳工具。不过,我不确定grep会有什么帮助。从表的布局来看,该文件似乎使用了选项卡作为列分隔符。对吗?