Hi Guys,
I have a number of results files inside a /results folder such as
/results/results1.txt
/results/results2.txt
…
… and so on.
Inside each of these files are messages either
This feature Passed
This feature Failed
What I want to do is read all of these files /results/results* and pick
out the messages that say FAILED and store them in a variable or
whatever so I can print them to screen.
Any you guys any bright “easy” ideas?
Help much appreciated.Thanks.
On 25 Apr 2008, at 10:28, Paul Donaghy wrote:
This feature Passed
Posted via http://www.ruby-forum.com/.
If really that’s all you want to do then I’d just use grep rather than
Ruby:
grep FAILED results/*.txt
Alex G.
Department of Biochemistry
University of Cambridge
on Behalf Of Paul Donaghy:
I have a number of results files inside a /results folder such as
/results/results1.txt
/results/results2.txt
… and so on.
Inside each of these files are messages either
This feature Passed
This feature Failed
What I want to do is read all of these files
/results/results* and pick
out the messages that say FAILED and store them in a variable or
whatever so I can print them to screen.
i assume you want to learn ruby.
if not, pls ignore this email.
if yes,
i also assume you want this in ruby so that you can run your “grep” in
windows (where grep is not readily available).
you may want to start by
reading grep.c and convert the c code to ruby (bottom up)
or you can start plainly by coding straight in ruby (top down),
refining/sprinkling details as you advance…
eg, here is a rudimentary grep program, it will print the filename, the
line number, and the line that matched a pattern.
C:\family\ruby>cat -n mygrep.rb
1 re = ARGV.shift or raise “pattern not entered”
2 not ARGV.empty? or raise “files not entered”
3
4 re = Regexp.new re
5 Dir.glob(ARGV).each do |f|
6 File.open(f).each_with_index do |line,index|
7 puts “#{f}:#{index+1} #{line}” if re =~ line
8 end
9 end
this is a sample run. here, i want to find the text “each” on all
test*.rb files.
C:\family\ruby>mygrep.rb
C:/family/ruby/mygrep.rb:1: pattern not entered (RuntimeError)
C:\family\ruby>mygrep.rb each
C:/family/ruby/mygrep.rb:2: files not entered (RuntimeError)
C:\family\ruby>mygrep.rb each test*.rb
test.rb:5 Dir.glob(ARGV).each do |f|
test.rb:6 File.open(f).each_with_index do |line,index|
test3.rb:11 sio.each(" “) do |w|
testx.rb:10 sio.each(” ") do |w|
testx.rb:68 def meth_each(s)
testx.rb:76 sp.each do |w|
testx.rb:101 x.report(“each”) { meth_each(s) }
Any you guys any bright “easy” ideas?
Your next task is to gather those output above and save them on a
container, like an array or a hash; now that is easy 
kind regards -botp
Paul Donaghy wrote:
Hi Guys,
I have a number of results files inside a /results folder such as
/results/results1.txt
/results/results2.txt
…
… and so on.
Inside each of these files are messages either
This feature Passed
This feature Failed
What I want to do is read all of these files /results/results* and pick
out the messages that say FAILED and store them in a variable or
whatever so I can print them to screen.
Any you guys any bright “easy” ideas?
Help much appreciated.Thanks.
Dir.glob(’/results/results*.txt’) do |fname|
IO.foreach(fname) do |line|
if line.include?(“Passed”)
puts line
end
end
end