From: "Peña, Botp" Date: 2008-09-05T13:03:52+09:00 Subject: Re: How do I quickly search the end of a huge text file? From: Brian Green [mailto:gallagherjb@gmail.com] # 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. why do you say it is slow? what is your comparison? where is your benchmark? how many files do you have? how large are the files? how much disk space do you have? how much memory do you have? how fast is your cpu? # I know that all the information I need is # in the last 5% of the text file - but I haven't been able to are you sure of the 5% ? where is your proof? # figure out a way to either jump to near the end, and then # start search through lines low level, use IO:SEEK_END # or even better iterating backwards through the file till I find # what I'm looking for... arggh. but your comparison will be forward. otherwise, you'll have to reverse your search/regex pattern. implement a reverse readline/gets. # here is the code I'm currently using - which # works are you sure it works? see my comment below, inline of your code. # 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" pls forgive me at this point because i am at a lost 1. how could $1, which is patterned after \"renderLayerManager.rlmi\[[0-9]\]\", be ever be equal to "defaultRenderLayer" ?? 2. and besides why need to compare again, if you can ask it straight from your regex comparison? # editedLine = "-" + $1 # layersFile.push editedLine # end # end # end # end # end # end # return layersFile # end # # root = "C:\\Users\\Brian\\Documents\\Ruby\\" # puts FindRenderLayers(root) kind regards -botp