From: Robert Klemme Date: 2007-08-17T22:47:53+09:00 Subject: Re: file grep 2007/8/16, Felix Windt : > > -----Original Message----- > > From: list-bounce@example.com > > [mailto:list-bounce@example.com] On Behalf Of Abhijeet Dharmapurikar > > Sent: Thursday, August 16, 2007 12:51 PM > > To: ruby-talk ML > > Subject: file grep > > > > I have a file that has a list of filenames- filenames.txt I > > have another file that has list of all the files in the > > system with their complete filepaths - allfilespath.txt > > > > I want to print the fullpaths of all the files listed in > > filenames.txt In shell scripting I would have done somethign like this > > > > for name in filenames.txt > > do > > grep $name allfilespath.txt > > if [ $? == 1 ] > > then > > echo $output_from_grep > > fi > > done > > > > > > How do I do this in ruby ? If possible (again if possible) > > explain what the code does. If not I will google and update > > what each line does. > > Disclaimer: Reading the complete files in to arrays like this may cost a > bunch of memory depending on file size Here's another solution that avoids reading the large file into mem but it does not write out names ordered: require 'set' names = Set.new(File.readlines("filenames.txt").each {|s| s.chomp!}) File.open("allfilespath.txt") do |io| io.each do |line| line.chomp! puts line if names.includes? File.basename(line) end end Kind regards robert