Re: Reversing the lines in a file
Re: Reversing the lines in a file
- Subject: Re: Reversing the lines in a file
- From: Mr Tea <email@hidden>
- Date: Fri, 10 May 2002 01:34:44 +0100
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. (OS X). I'm ducking now, in anticipation of a barrage of faster
and more efficient ways to do this.
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 theFlipper to reverse of every paragraph of thetext
set theNewText to ""
repeat with thePara in theFlipper
set theNewText to theNewText & thePara & return
end repeat
set eof file theFile to 0
write theNewText to file theFile
close access file theFile
on error errmsg
display dialog errmsg
end try
end repeat
end open
I haven't put in a check to see if the items being dropped are actually text
files, as I don't know a reliable way to do this in X, now that the 'File
Type' code is no longer mandatory. Perhaps a more knowledgable scripter
could step in here...
Regards
Mr Tea
--
_______________________________________________
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.