• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Long text file manipulation...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Long text file manipulation...


  • Subject: Re: Long text file manipulation...
  • From: "Arthur J Knapp" <email@hidden>
  • Date: Thu, 05 Apr 2001 15:21:48 -0400

> Date: Wed, 4 Apr 2001 08:54:29 -0400
> From: bRaD Weston <email@hidden>
> Subject: Long text file manipulation...

> I'm working on a project in which I need to manipulate large text files, one
> line at a time. I've been looking for some sort of OSAX that can do this
> because I cannot afford the memory and time requirements to read in lots of
> big (2.5 MB) text files and then save them out, but have been unsuccessful.
>
> Essentially, I just want to read the first line of the file and delete it from
> the file and add another line to the bottom of the file.

The following code is dangerous, first: because it was written by me,
and secondly: because it directly modifies the file, rather than the
much preferred tactic of writing content to a temporary file. You have
been warned... ;-)

-- For debugging
--
open (choose file)

on open (theFiles)

repeat with aFile in (theFiles as list)

cutFirstLine_pasteLastLine(aFile, "dude")

end repeat

end open


-- Main parser
--
on cutFirstLine_pasteLastLine(fAlias, pasteLine)

-- The maximum amount that will be read/written at
-- any one time. Whatever amount of memory you believe
-- is "safe".
--
set readBuffer to 512 -- something safe

-- We open two separate accesses to the same file. This
-- allows us to avoid updating a file marker, ie: each
-- read operation will move the file marker of the "read"
-- access to where we want it, and each write operation
-- will move the file marker of the "write" access to
-- where we want it.
--
set frefRead to open for access fAlias
set frefWrite to open for access fAlias with write permission

try
-- Read a line. This also moves the "read" file marker
-- to just after the first line.
--
set temp_string to read frefRead until ("" & return)

-- Save the length. We use this at the end to tell us
-- where to start writing the new end-line.
--
set deletedLength to length of temp_string

try
-- We just keep reading/writing until an eof error.
--
repeat
-- Because we always read BEFORE we write, no content
-- is overwritten before it is read.
--
write (read frefRead for readBuffer) to frefWrite
end repeat
end try

-- The last write operation from above will cause an eof
-- error because of the "for readBuffer" part. However,
-- after the error, the file marker will still be where
-- it was previously, so we simple read the remainder of
-- the file, (we know that the length will be less than
-- readBuffer).
--
write (read frefRead) to frefWrite

-- Now that we have "shifted" the file's entire contents,
-- we shorten the file's length by the deleted amount.
--
set eof frefWrite to ((get eof frefWrite) - deletedLength)

-- The next write operation will now occur at the end
-- of the file.
--
-- Note: This assume that the last line of the file has
-- a return character. If it doesn't, you may need
-- to add one, ie: ( "\r" & pasteLine )
--
write pasteLine to frefWrite

on error m number n
close access frefRead
close access frefWrite
error m number n
end try
close access frefRead
close access frefWrite
end cutFirstLine_pasteLastLine


Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden

Hey, check out:
http://www.eremita.demon.co.uk/scripting/applescript/


  • Prev by Date: Re: Long text file manipulation--BIGGER QUESTION
  • Next by Date: Re: Admin
  • Previous by thread: Re: Long text file manipulation--BIGGER QUESTION
  • Next by thread: PostScript files
  • Index(es):
    • Date
    • Thread