# Checking File for a line

**URL:** <https://rubytalk.org/t/checking-file-for-a-line/42439>\
**Category:** ruby-talk\
**Created:** [23 November 2007 10:29 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439 "2007-11-23T10:29:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Loftus](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@Peter\_Loftus](https://rubytalk.org/u/Peter_Loftus)\
**Post date:** [23 November 2007 10:29 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/1 "2007-11-23T10:29:40Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [23 November 2007 11:00 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/2 "2007-11-23T11:00:56Z")

</div>

Peter Loftus wrote:

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

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

Hope the helps.

Regards,  
Lee

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 November 2007 12:08 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/3 "2007-11-23T12:08:02Z")

</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|  
&nbsp;&nbsp;if /rx/ =~ line  
&nbsp;&nbsp;&nbsp;&nbsp;puts "found it"  
&nbsp;&nbsp;&nbsp;&nbsp;break  
&nbsp;&nbsp;end  
end

Cheers

robert

> **···**
>
> 2007/11/23, Lee Jarvis \<jarvo88@gmail.com\>:
> 
> > Peter Loftus wrote:  
> > \> Im pretty sure this will work but is there any cleaner way to do it?
> > 
> > File.readlines('file.txt.).each do |line|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts 'got it!' if line =~ /regexp/  
> > end
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Dejan\_Dimic](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Dejan\_Dimic](https://rubytalk.org/u/Dejan_Dimic)\
**Post date:** [23 November 2007 12:45 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/4 "2007-11-23T12:45:03Z")

</div>

Check out

ri IO:foreach  
ri IO#each

and use what more suits your needs.

Dima

> **···**
>
> On Nov 23, 1:08 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > 2007/11/23, Lee Jarvis \<jarv...@gmail.com\>:
> > 
> > \> Peter Loftus 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|  
> > &nbsp;&nbsp;if /rx/ =~ line  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "found it"  
> > &nbsp;&nbsp;&nbsp;&nbsp;break  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Cheers
> > 
> > robert
> > 
> > --  
> > use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [23 November 2007 18:41 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/5 "2007-11-23T18:41:37Z")

</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|  
> &nbsp;&nbsp;if /rx/ =~ line  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "found it"  
> &nbsp;&nbsp;&nbsp;&nbsp;break  
> &nbsp;&nbsp;end  
> end
> 
> Cheers
> 
> robert

Ahh, good point, touche my good man.

Regards,  
Lee

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 November 2007 17:37 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/6 "2007-11-23T17:37:08Z")

</div>

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

> **···**
>
> 2007/11/23, Dejan Dimic \<dejan.dimic@gmail.com\>:
> 
> > On Nov 23, 1:08 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:  
> > \> 2007/11/23, Lee Jarvis \<jarv...@gmail.com\>:  
> > \>  
> > \> \> Peter Loftus 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  
> > \>  
> > \> --  
> > \> 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.
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [24 November 2007 07:58 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/7 "2007-11-24T07:58:43Z")

</div>

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

> **···**
>
> 2007/11/23, Lee Jarvis \<jarvo88@gmail.com\>:
> 
> > \> 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.
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Dejan\_Dimic](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Dejan\_Dimic](https://rubytalk.org/u/Dejan_Dimic)\
**Post date:** [25 November 2007 18:10 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/8 "2007-11-25T18:10:02Z")

</div>

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

> **···**
>
> On Nov 24, 8:58 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > 2007/11/23, Lee Jarvis \<jarv...@gmail.com\>:
> > 
> > \> \> 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.
> > 
> > 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
> > 
> > --  
> > use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [25 November 2007 19:44 UTC](https://rubytalk.org/t/checking-file-for-a-line/42439/9 "2007-11-25T19:44:35Z")

</div>

Dejan Dimic wrote:

> **···**
>
> > On Nov 24, 8:58 am, Robert Klemme \<shortcut...@googlemail.com\> 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.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
