set theText to "206487 11010
206486 11061
206485 11068
206242 10837
206241 10838
206243 10840
206251 11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852"
set myNewText to ""
repeat with aP in paragraphs of theText
set myNewText to myNewText & (text 8 thru -1 of aP's contents) & return
end repeat
(I am assuming you have 4 empty spaces between the repetition of the text blocks on the same line). In case it is a tab, it - most likely - still works. Otherwise let us know.
AppleScript is really simple, but not nearly as easy as some newbies think.
One problem has already been pointed out by idicore. He has to guess how many spaces or tabs there are in your data. The second problem I will point out is that it is advisable to leave the endings of the paragraphs original, and not spank the endings at random. The following script does not guess nothing:
set endindgs to {linefeed, return}
set theText to "206487 11010
206486 11061
206485 11068
206242 10837
206241 10838
206243 10840
206251 11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852"
set textItems to theText's characters
set |paragraphs| to paragraphs of theText
set countParagraphs to count |paragraphs|
-- build new text
set newText to ""
if countParagraphs > 1 then
-- the offset of each paragraph in the original text
set theOffset to 0
repeat with i from 1 to countParagraphs - 1
set theParagraph to item i of |paragraphs|
-- Determine single line ending
set paragraphCount to (count theParagraph)
set lineEnding to item (paragraphCount + 1 + theOffset) of textItems
-- check if the ending is double (like \r\n)
set theNext to item (paragraphCount + 2 + theOffset) of textItems
if theNext is in endindgs then set lineEnding to lineEnding & theNext
-- add current retrived text
set newText to newText & word 2 of theParagraph & lineEnding
set theOffset to theOffset + paragraphCount + (count lineEnding)
end repeat
end if
-- add last paragraph (without ending)
set newText to newText & word 2 of item -1 of |paragraphs|
My suggestion which works with both tab and space separators.
-- I abbreviated theText for brevity's sake
-- paragraph 1 uses space separator; paragraph 2 uses tab separator; paragraph 3 uses both
set theText to "206487 11010
206486 11061
206485 11068
206242 10837"
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tab, space}
set theNewText to {}
repeat with aParagraph in (paragraphs of theText)
set the end of theNewText to text item -1 of aParagraph -- last 5 characters
-- set the end of theNewText to text 2 thru -1 of (text item -1 of aParagraph) -- last 4 characters
end repeat
set AppleScript's text item delimiters to linefeed
set theNewText to theNewText as text
set AppleScript's text item delimiters to ATID
theNewText
For those who is interested to test with different tab, space, \r, \n and \r\n combinations:
set endindgs to {linefeed, return}
set theText to "206487 11010" & return & linefeed & "206486 11061
206485 11068
206242 10837
206241 10838
206243 10840
206251 11167
206239 88806
206240 88807
206244 88808
206245 88809
206248 88854
206249 88818
206238 88819
206250 88849
206247 88852"
set textItems to theText's characters
set |paragraphs| to paragraphs of theText
set countParagraphs to count |paragraphs|
-- build new text
set newText to ""
if countParagraphs > 1 then
-- the offset of each paragraph in the original text
set theOffset to 0
repeat with i from 1 to countParagraphs - 1
set theParagraph to item i of |paragraphs|
-- Determine single line ending
set paragraphCount to (count theParagraph)
set lineEnding to item (paragraphCount + 1 + theOffset) of textItems
-- check if the ending is double (like \r\n)
set theNext to item (paragraphCount + 2 + theOffset) of textItems
if theNext is in endindgs then set lineEnding to lineEnding & theNext
-- add current retrived text
set newText to newText & word 2 of theParagraph & lineEnding
set theOffset to theOffset + paragraphCount + (count lineEnding)
end repeat
end if
-- add last paragraph (without ending)
set newText to newText & word 2 of item -1 of |paragraphs|
Thank you for helping out! I added “set the clipboard to newText” to get the result to the clipboard. The result prints out at in the Result window and not in the TextWrangler document. How do I get the result show up in the TextWrangler document?
KniazidisR’s solutions set a variable to ‘theText’s text items’ without first specifying a value for the ‘text item delimiters’. When the delimiters are at their default value of {“”}, a ‘text item’ is the same as a ‘character’. Since this is apparently what’s meant here, it would be both safer and more correct (and possibly more efficient) to use the term ‘characters’ instead. It always means characters no matter how the text item delimiters may happen to be set at the time.
I have not TextWrangler installed to play with it. But… I think you can use set text of front document to newText. This way I think your original text should be replaced with new. If you want to keep original text unchanged, then you can wrote something like this:
set newDocument to make new document
set text of newDocument to newText