# Extract lines with regular expressions

**URL:** <https://rubytalk.org/t/extract-lines-with-regular-expressions/45998>\
**Category:** ruby-talk\
**Created:** [21 April 2008 05:49 UTC](https://rubytalk.org/t/extract-lines-with-regular-expressions/45998 "2008-04-21T05:49:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Dore](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Michael\_Dore](https://rubytalk.org/u/Michael_Dore)\
**Post date:** [21 April 2008 05:49 UTC](https://rubytalk.org/t/extract-lines-with-regular-expressions/45998/1 "2008-04-21T05:49:20Z")

</div>

I am a newb trying to take a list of data in a text file and print out a  
new list of lines that contain certain characters...  
I am not too worried about the actual regex - I can figure that out -  
but I cannot seem to get it to implement correctly.

I found the following online ([http://www.fepus.net/ruby1line.txt](http://www.fepus.net/ruby1line.txt))

# print only lines that match a regular expression (emulates 'grep')  
&nbsp;&nbsp;&nbsp;&nbsp;$ ruby -pe 'next unless $\_ =~ /regexp/' \< file.txt

and it does exactly what I want, but...

how do I use this (or something similar, ie grep) in a .rb script?

I have tried this:  
vl\_stops.each do |vl\_stops|  
&nbsp;&nbsp;&nbsp;puts "#{vl\_stops}"  
&nbsp;&nbsp;&nbsp;search = Regexp.new("^\b(#{vl\_stops}:)\w\*")  
&nbsp;&nbsp;&nbsp;files.each do |file|  
&nbsp;&nbsp;&nbsp;f = File.open(directory+file)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.grep(search)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts #what goes here? line?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.close  
&nbsp;&nbsp;&nbsp;end  
puts  
end  
based on something I found here  
([http://www.rousette.org.uk/blog/archives/2004/10/21/gtd-using-text-files/](http://www.rousette.org.uk/blog/archives/2004/10/21/gtd-using-text-files/))

But I am not sure how to seal the deal, as it were...  
Any thoughts would be appreciated.  
Thanks!

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

---

<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:** [21 April 2008 06:30 UTC](https://rubytalk.org/t/extract-lines-with-regular-expressions/45998/2 "2008-04-21T06:30:11Z")

</div>

files.each do |file|  
&nbsp;&nbsp;&nbsp;&nbsp;File.open(directory+file) do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts f.grep(search)  
&nbsp;&nbsp;&nbsp;&nbsp;end

Or, for large files

&nbsp;&nbsp;&nbsp;&nbsp;files.each do |file|  
&nbsp;&nbsp;&nbsp;&nbsp;File.open(directory+file) do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts line if search ~= line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 21.04.2008 07:49, Michael Dore wrote:
> 
> > I am a newb trying to take a list of data in a text file and print out a  
> > new list of lines that contain certain characters...  
> > I am not too worried about the actual regex - I can figure that out -  
> > but I cannot seem to get it to implement correctly.
> > 
> > I found the following online ([fepus.net](http://www.fepus.net/ruby1line.txt%5C))
> > 
> > # print only lines that match a regular expression (emulates 'grep')  
> > &nbsp;&nbsp;&nbsp;&nbsp;$ ruby -pe 'next unless $\_ =~ /regexp/' \< file.txt
> > 
> > and it does exactly what I want, but...
> > 
> > how do I use this (or something similar, ie grep) in a .rb script?
> > 
> > I have tried this:  
> > vl\_stops.each do |vl\_stops|  
> > &nbsp;&nbsp;&nbsp;puts "#{vl\_stops}"  
> > &nbsp;&nbsp;&nbsp;search = Regexp.new("^\b(#{vl\_stops}:)\w\*")  
> > &nbsp;&nbsp;&nbsp;files.each do |file|  
> > &nbsp;&nbsp;&nbsp;f = File.open(directory+file)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.grep(search)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts #what goes here? line?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.close  
> > &nbsp;&nbsp;&nbsp;end  
> > puts  
> > end  
> > based on something I found here  
> > ([http://www.rousette.org.uk/blog/archives/2004/10/21/gtd-using-text-files/\](http://www.rousette.org.uk/blog/archives/2004/10/21/gtd-using-text-files/%5C))
> > 
> > But I am not sure how to seal the deal, as it were...  
> > Any thoughts would be appreciated.  
> > Thanks!

---

<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:** [21 April 2008 08:54 UTC](https://rubytalk.org/t/extract-lines-with-regular-expressions/45998/3 "2008-04-21T08:54:38Z")

</div>

Michael Dore wrote:

> I have tried this:
> 
> vl\_stops.each do |vl\_stops|

First, you might try dreaming up more than one variable name.

Second, if you are searching for simple strings, like 'hello', you can  
do this:

search\_string = 'hello'

IO.foreach("data.txt") do |line|  
&nbsp;&nbsp;if line.include?(search\_string)  
&nbsp;&nbsp;&nbsp;&nbsp;puts line  
&nbsp;&nbsp;end  
end

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