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.
···
--
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
···
-----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.
--
Posted via http://www.ruby-forum.com/\.
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 Windt 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
--
Posted via http://www.ruby-forum.com/\.
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
···
2007/8/16, Felix Windt <fwmailinglists@gmail.com>:
> -----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
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?
···
On Aug 17, 9:47 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
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
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
···
On 17.08.2007 16:30, Kaldrenon wrote:
On Aug 17, 9:47 am, "Robert Klemme" <shortcut...@googlemail.com> > wrote:
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
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?
That's good to know, glad I asked. Thanks!
···
On Aug 17, 1:30 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> 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?
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.
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 Klemme <shortcut...@googlemail.com> 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!
--
Posted via http://www.ruby-forum.com/\.