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.
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.
Posted via http://www.ruby-forum.com/.
Disclaimer: Reading the complete files in to arrays like this may cost a
bunch of memory depending on file size
read in an array that contains one element per line from the list of
filesnames
filenames = File.new(“filenames.txt”).readlines
read in an array that contains one element per line from the list of
full
filepaths
paths = File.new(“allfilespath.txt”).readlines
cycle through all filenames
filenames.each do |filename|
File.basename with one argument returns the last element of the
filepath, so the filename in a path
the find_all method from the Enumerable mixin passes each element of
the
collection to the block, and returns an array of those where the block
evaluates to true
paths.find_all{|path| File.basename == filename}.each do |match|
# cycle through the matches, and print them
puts “#{filename} matches #{match}”
end
end
HTH,
Felix
Thanks Felix I just had to change one line
< paths.find_all{|path| File.basename == filename}.each do |match|
paths.find_all{|path| File.basename(path) == filename}.each do |match|
but it worked like a charm.
Regards,
Abhijeet
Felix W. wrote:
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.
Posted via http://www.ruby-forum.com/.
Disclaimer: Reading the complete files in to arrays like this may cost a
bunch of memory depending on file size
read in an array that contains one element per line from the list of
filesnames
filenames = File.new(“filenames.txt”).readlines
read in an array that contains one element per line from the list of
full
filepaths
paths = File.new(“allfilespath.txt”).readlines
cycle through all filenames
filenames.each do |filename|
File.basename with one argument returns the last element of the
filepath, so the filename in a path
the find_all method from the Enumerable mixin passes each element of
the
collection to the block, and returns an array of those where the block
evaluates to true
paths.find_all{|path| File.basename == filename}.each do |match|
# cycle through the matches, and print them
puts “#{filename} matches #{match}”
end
end
HTH,
Felix
On Aug 17, 9:47 am, “Robert K.” [email protected]
wrote:
end
end
Kind regards
robert
Is there a reason to do it this way instead of the slightly more
compact way below?
File.open(“allfilespath.txt”).each_line do |line|
line.chomp!
puts line if names.includes? File.basename(line)
end
Is there a difference in behavior, or only in appearance?
2007/8/16, Felix W. [email protected]:
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
On Aug 17, 1:30 pm, Robert K. [email protected] wrote:
The more compact version does not properly close the file. Although you
fixed compact version does not close the file if the block is left via
an exception.
That’s good to know, glad I asked. Thanks!
On 17.08.2007 16:30, Kaldrenon wrote:
line.chomp!
File.open(“allfilespath.txt”).each_line do |line|
line.chomp!
puts line if names.includes? File.basename(line)
end
Is there a difference in behavior, or only in appearance?
The more compact version does not properly close the file. Although you
can fix that by doing
File.open(“allfilespath.txt”).each_line do |line|
line.chomp!
puts line if names.includes? File.basename(line)
end.close
^^^^^
there is still a difference: my version will close the file regardless
how the block is left (i.e. even in case of an exception) while the
fixed compact version does not close the file if the block is left via
an exception.
Kind regards
robert
This can be solved with a begin rescue statement though:
begin
File manipulation
rescue => e
file.close unless file.nil?
end
Kaldrenon wrote:
On Aug 17, 1:30 pm, Robert K. [email protected] wrote:
The more compact version does not properly close the file. Although you
fixed compact version does not close the file if the block is left via
an exception.
That’s good to know, glad I asked. Thanks!