Re: Reversing the lines in a file
Re: Reversing the lines in a file
- Subject: Re: Reversing the lines in a file
- From: Lachlan Deck <email@hidden>
- Date: Fri, 10 May 2002 14:14:29 +1000
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.
A fair proportion of that time would be watching the trampoline in the
dock ;-)
It would probably be quicker to make it a compiled script that is run
from the ScriptMenu where it gathers the current selection from the
finder as a list.
(OS X). I'm ducking now, in anticipation of a barrage of faster
and more efficient ways to do this.
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.
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
with regards,
--
Lachlan Deck
email@hidden
_______________________________________________
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.