0

I have a dirty report file that contains improperly formatted negative numbers. The ultimate goal is to import these to Excel for analysis. I am using BBEdit to clean the report before importing into Excel. I would like to create an apple script to loop through the report and move the "-" from the back of the number to the front.

Dirty Report Example Rows:

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 

Desired output:

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 

I have JavaScript and VBScript experience so I imaging the script working something like this psudo script:

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

This is my first experience with AppleScript and I am at a complete loss.

Thanks for the help/

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
  • 1
    what do you define as a word here ? is the space considered to be the bounding of each word .. alos you may want to look into using grep , awk or similar command language to accomplish your goal. – mcgrailm Sep 23 '11 at 00:44
  • Adding to what @mcgrailm wrote, awk and especially *sed* are great tools for this sort of task. I'm not sure how grep would help, though. – Michael J. Barber Sep 23 '11 at 07:15
  • From the layout of the tables, it looks like the file uses tabs as column separators. Is that correct? – Michael J. Barber Sep 23 '11 at 07:38

2 Answers2

1

I'm not sure you need AppleScript. Will BBEdit's find/replace window work for this? Try the following:

Find: ([0-9\.]+)\-
Replace: \-\1

"Grep" and "Wrap around" should be the only things selected below the "Replace" text area. Then click "Replace All."

If you need to do it to a bunch of reports at once, use the same find/replace patterns with multi file search. If I'm not understanding your question correctly, let me know, but I think you can get this done without AppleScript.

Dave Wolfe
  • 11
  • 1
0

Try this. I'm assuming you can get the text from bbedit (or wherever) into applescript as the variable "originalText". Then you'll have to put the "fixedText" back wherever it belongs. NOTE: in my code I assume the "tab" character separates the words/columns in each line of the origText as Michael J. Barber has it currently formatted. If it is another character (like a space character) then you will have to change the word tab in the code to 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

NOTE: when testing my code above you should copy/paste it into Applescript Editor. You will have to fix the tab characters in originalText because they do not survive the copy/paste. So remove the spaces between the words/columns and insert a tab. Then the code will work correctly.

regulus6633
  • 18,848
  • 5
  • 41
  • 49