Parsing text files, line by line

I have a very large text file, too large to put into a applescript var. I need to parse each line, and do something based on its contents. I’m having trouble finding a way to read each line without loading the entire text file into a var.

How large is the file?

7.4MB :cool:

Wow. What code are you currently using to parse it?

Nothing, I’m trying to figure out how to go about it. Thus I have come here. I’ve done smaller files before but they all have been read into a variable.

well… if you want to use word instead of textedit or w/e youre using… then i know how…

set theLine to 1

repeat while theLine < 100
tell application "Microsoft Word"
set x to line theLine of document -1
end tell
--(*do whatever you want here*)
set theLine to theLine + 1
end repeat


set theFile to (choose file) --select file
set theEOF to get eof of theFile --end of the file as character count

set theIncrement to 0 --will hold your most recent "character count"
repeat until theIncrement > theEOF --repeats until the char count is greater than the EOF
	set aLine to read theFile from theIncrement before return --will return paragraph starting at character theIncrement to end of line
	set countof_aLine to the count of aLine --counts characters in aLine
	set theIncrement to theIncrement + (countof_aLine+1) --increase your starting character count
	------put your process here------- 
end repeat


(made a minor rev…last set included the “return”)

Would you explain what you’re doing with the contents?


Using the file reference number is faster and allows you to use the file mark according to ScriptingAdditions.pdf. Something like this:

set the_file to choose file
set ref_num to open for access the_file
set t to read ref_num before return
display dialog t
on error
close access ref_num
exit repeat
end try
end repeat

Editted: BTW, paragraphs of some text files don’t end with Mac style line endings (return), so you need to use line feeds or whatever.


I gotta read the documentation more often…