How do I quickly search the end of a huge text file?

B

Brian Green

I am trying to create a ruby script that will search a maya ascii file
for specific text. The problem I'm running into is that it's running to
slow for the system at work. I know that all the information I need is
in the last 5% of the text file - but I haven't been able to figure out
a way to either jump to near the end, and then start search through
lines or even better iterating backwards through the file till I find
what I'm looking for... here is the code I'm currently using - which
works but slowly - any suggestions for how to speed this up would be
greatly appreciated! :D

require "FileUtils"
require "ftools"

def FindRenderLayers (root)
layersFile = []
dirLocation = root.gsub(/(\\)$/, '')
list = Dir.entries(dirLocation)

list.each do |file|
if file =~ /\.ma$/
fileName = root + file
layersFile.push file
File.open(fileName) do |file|
while line = file.gets
if line =~ /connectAttr
(\"renderLayerManager.rlmi\[[0-9]\]\")/
if $1 != "defaultRenderLayer"
editedLine = "-" + $1
layersFile.push editedLine
end
end
end
end
end
end
return layersFile
end

root = "C:\\Users\\Brian\\Documents\\Ruby\\"
puts FindRenderLayers(root)
 
B

Brian Green

Thank you very much!! That's exactly what I was looking for!

I just added

file.seek(-2000,IO::SEEK_END)

right after the line

fileSize = File.size(fileName)

and it worked perfectly! It's running about 18x faster - which is a huge
improvement - I think the guys at work will be satisifed with it's speed
now!

Thanks again Lex!! :D

Lex said:
Lex said:
Here is an usage example :

begin
file = File.open(ARGV[0])
rescue
puts "file does not exist or is not a file\n"
end

file.seek(-25,IO::SEEK_END)
puts file.readlines

The code will read the rest of the files from that location . Try it on
a file and see .

I meant the rest of the lines . Sorry .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top