# Reading a number of files and and searching for text

**URL:** <https://www.ruby-forum.com/t/reading-a-number-of-files-and-and-searching-for-text/135824>\
**Category:** Ruby\
**Created:** [April 25, 2008, 11:28am UTC](https://www.ruby-forum.com/t/reading-a-number-of-files-and-and-searching-for-text/135824 "2008-04-25T11:28:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 25, 2008, 11:28am UTC](https://www.ruby-forum.com/t/reading-a-number-of-files-and-and-searching-for-text/135824/1 "2008-04-25T11:28:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 25, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/reading-a-number-of-files-and-and-searching-for-text/135824/2 "2008-04-25T12:06:39Z")

</div>

On 25 Apr 2008, at 10:28, Paul Donaghy wrote:

> ## This feature Passed
> 
> Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 26, 2008, 4:22am UTC](https://www.ruby-forum.com/t/reading-a-number-of-files-and-and-searching-for-text/135824/3 "2008-04-26T04:22:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [April 26, 2008, 7:25am UTC](https://www.ruby-forum.com/t/reading-a-number-of-files-and-and-searching-for-text/135824/4 "2008-04-26T07:25:13Z")

</div>

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
