Re: Reversing the lines in a file
Re: Reversing the lines in a file
- Subject: Re: Reversing the lines in a file
- From: "Marc K. Myers" <email@hidden>
- Date: Fri, 10 May 2002 12:24:17 -0400
- Organization: [very little]
>
Date: Fri, 10 May 2002 14:14:29 +1000
>
Subject: Re: Reversing the lines in a file
>
From: Lachlan Deck <email@hidden>
>
To: email@hidden
>
>
Hi there,
>
>
> From: Mr Tea <email@hidden>
>
>
>
> This from Rice, Gary - dated 10/5/02 12.29 am:
>
>
>
>> I have a text file. Each line in the file ends with a <CR>. It is a
>
>> large
>
>> file. I want to reverse the order of the file. That is, I want the
>
>> last line
>
>> in the file to appear at the begining and the first line in the file
>
>> to be
>
>> at the end.
>
>>
>
>> Can I do this using AppleScript?
>
>
>
> Reckon so. How large is large, and what OS are you on?
>
>
>
> The following script might do what you need. Paste into a script window,
>
> save as an application, and drag one or more text files onto it to
>
> process
>
> them. I ran it on a text file with 1290 lines and it reversed them in
>
> around
>
> 6 seconds.
>
>
The thing that your solution does is add an extra return So, if you run
>
the script a few times on the one file, you'll get extra space at the
>
top which shouldn't be there. So, you need to add in an if statement so
>
that it doesn't append a return character for the last repeat.
This version would eliminate the need for inserting the carriage returns
and should run a bit faster:
on open theFiles
repeat with theFile in theFiles
try
set theFile to theFile as string
set thetext to read (open for access file theFile with write permission)
set {od, AppleScript's text item delimiters} to [optn-L]
{AppleScript's text item delimiters, {return}}
set theText to reverse of (get text items of thetext)
set theText to thetext as text
set AppleScript's text item delimiters to od
set eof file theFile to 0
write theText to file theFile
close access file theFile
on error errmsg
close access file theFile
display dialog errmsg
end try
end repeat
end open
Marc K. Myers <email@hidden>
http://AppleScriptsToGo.com
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074
[5/10/02 12:20:08 PM]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.