# Checking File for a line

**URL:** <https://www.ruby-forum.com/t/checking-file-for-a-line/121065>\
**Category:** Ruby\
**Created:** [November 23, 2007, 11:29am UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065 "2007-11-23T11:29:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 23, 2007, 11:29am UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/1 "2007-11-23T11:29:44Z")

</div>

Need to check a file for a line  
Lets say “this is an example”

I am going to just take every line into an array  
and then have a For loop to check the array for that line.

Im pretty sure this will work but is there any cleaner way to do it?

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 23, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/2 "2007-11-23T12:00:57Z")

</div>

Peter L. wrote:

> Im pretty sure this will work but is there any cleaner way to do it?

File.readlines('file.txt.).each do |line|  
puts ‘got it!’ if line =~ /regexp/  
end

Hope the helps.

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 23, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/3 "2007-11-23T13:08:28Z")

</div>

2007/11/23, Lee J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Peter L. wrote:
> 
> > Im pretty sure this will work but is there any cleaner way to do it?
> 
> File.readlines('file.txt.).each do |line|  
> puts ‘got it!’ if line =~ /regexp/  
> end

This is more efficient - no need to load the whole file into mem to  
find a single line:

File.foreach “file.txt” do |line|  
if /rx/ =~ line  
puts “found it”  
break  
end  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 23, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/4 "2007-11-23T13:45:57Z")

</div>

On Nov 23, 1:08 pm, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> find a single line:  
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Check out

ri IO:foreach  
ri IO#each

and use what more suits your needs.

Dima

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 23, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/5 "2007-11-23T18:38:07Z")

</div>

2007/11/23, Dejan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > This is more efficient - no need to load the whole file into mem to
> 
> and use what more suits your needs.  
> I am not sure I understand this posting of yours. Are you talking to  
> the OP or to me? If you are talking to me, foreach is what I used in  
> the example above.

robert

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 23, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/6 "2007-11-23T19:41:38Z")

</div>

> This is more efficient - no need to load the whole file into mem to  
> find a single line:
> 
> File.foreach “file.txt” do |line|  
> if /rx/ =~ line  
> puts “found it”  
> break  
> end  
> end
> 
> Cheers
> 
> robert

Ahh, good point, touche my good man.

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 24, 2007, 8:59am UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/7 "2007-11-24T08:59:20Z")

</div>

2007/11/23, Lee J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > Cheers
> > 
> > robert
> 
> Ahh, good point, touche my good man.

While we’re at it: there is another solution - probably a bit more  
elegant:

require ‘enumerator’  
File.to\_enum(:foreach,“file.txt”).grep(/rx/) {|li| puts li; break}

🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 25, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/8 "2007-11-25T19:11:15Z")

</div>

On Nov 24, 8:58 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > end  
> > > File.to\_enum(:foreach,“file.txt”).grep(/rx/) {|li| puts li; break}
> 
> 🙂
> 
> Kind regards
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

That’s Ruby beauty, express yourself in a way that suits you the most  
and still we all can understand your dialect and enjoy.

Dima

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 25, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/checking-file-for-a-line/121065/9 "2007-11-25T20:44:38Z")

</div>

Dejan D. wrote:

> On Nov 24, 8:58 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > end  
> > > > File.to\_enum(:foreach,“file.txt”).grep(/rx/) {|li| puts li; break}
> > 
> > 🙂
> > 
> > Kind regards
> > 
> > robert
> > 
> > –  
> > use.inject do |as, often| as.you\_can - without end
> 
> That’s Ruby beauty, express yourself in a way that suits you the most  
> and still we all can understand your dialect and enjoy.

Speak for yourself. I found that solution particularly distasteful. It  
turned a simple, easy to read solution, into a convoluted one-liner  
mess. And on my system the enumerator solution is 50% slower. But,  
that seems to be the Ruby Way, so at some point I guess I’ll have to  
drink the koolaid and declare how beautiful solutions like that are.
