Too large to display

I’m trying to script a search of a large text file. My problem can be summarized like this:

set logFile to alias “disk:folder:file”
set theLog to read logFile
→ result too large to display
set oneItem to paragraph 100 of theLog
→ “abcd”
theLog contains oneItem
→ true
set nutherItem to paragraph -2 of theLog
→ “wxyz”
paragraph -2 of theLog contains nutherItem
–>true
theLog contains nutherItem
→ false

The last line of the script is what I actually want to do: Find out whether the string nutherItem is anywhere in the log file, but that doesn’t seem to work if the string is too far down in the log file, perhaps in the part that can’t be displayed. Yet the string is there because I can get hold of it if I can guess which paragraph it’s in.

For now, all I can think of is looping thru the list a paragraph (or line) at a time but that obviously slows things down considerably. I’m hoping someone here has a faster solution.

Have you tried this in a script saved as an application or have your tests been limited to running the script in Script Editor? What happens if you save this as an application and run it?

set logFile to alias "disk:folder:file"
set theLog to read logFile
set exists_ to (theLog contains "search string") as text
display dialog exists_

If this isn’t acceptable, you might need to read the file in chunks (should be better than paragraphs).

– Rob

I should have mentioned that it’s running as a Folder Action script. At first, there was a problem with “out of memory” errors, but I remembered reading a tip (perhaps here) about using ResEdit to increase the memory allocatioin for the Folder Actions extension. That eliminated the memory errors, but doesn’t seem to do anything for the false negatives.

Reading in chunks is probably the way to go. If you have an example handy, I’d be most grateful.

Sorry for interrupting, I just wanted to toss an option out there that I like to use. Tanaka’s OSAX used to contain a command called getlines, now named MT Pick Lines, that not only lets you know if a piece of text is found in a file, itwill extract a list of lines from the text file that contain your keyword - all in one command. If you are looking for a vanilla solution this isn’t it but it is record fast.

set searchString to "text to search for"
set instancesFound to MT Pick Lines source "Mac HD:Desktop Folder:some text file" keyword searchString

(*result= list of # items
item 1="This line contains text to search for"
item 2="Text to search for on this line"
item 3="I am looking for text to search for"
*)

You can download Tanaka’s OSAX right here at Macscripter.

I currently use this method on a very large text file , (3MB and growing) and it is still pretty darn quick.

Best of luck

I’ve never needed to do this so I don’t have any tested code to offer. If you haven’t done so, you might want to inspect the related commands which are found in Standard Additions (open for access, close access, read, get eof). Hopefully someone has a thoroughly debugged handler to share.

– Rob

Yes! Tanaka’s is just the ticket. I’m already using it for fast directory searches, and Pick Lines is just as fast.

Reading in chunks is something I’m gonna have to learn how to do, I suppose, but not today.

Thanks, folks.