file grep

  • Thread starter Abhijeet Dharmapurikar
  • Start date
A

Abhijeet Dharmapurikar

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.
 
A

Abhijeet Dharmapurikar

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
 
R

Robert Klemme

2007/8/16 said:
-----Original Message-----
From: (e-mail address removed)
[mailto:[email protected]] 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
 
K

Kaldrenon

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?
 
R

Robert Klemme

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.

Kind regards

robert
 
K

Kaldrenon

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.

That's good to know, glad I asked. Thanks!
 
A

Alexander Mcconaughey

This can be solved with a begin rescue statement though:

begin
# File manipulation
rescue => e
file.close unless file.nil?
end
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top