# Extract lines with regular expressions

**URL:** <https://www.ruby-forum.com/t/extract-lines-with-regular-expressions/135321>\
**Category:** Ruby\
**Created:** [April 21, 2008, 7:49am UTC](https://www.ruby-forum.com/t/extract-lines-with-regular-expressions/135321 "2008-04-21T07:49:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [April 21, 2008, 7:49am UTC](https://www.ruby-forum.com/t/extract-lines-with-regular-expressions/135321/1 "2008-04-21T07:49:19Z")

</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 ([fepus.net](http://www.fepus.net/ruby1line.txt))

# print only lines that match a regular expression (emulates ‘grep’)

```
$ 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|  
puts “#{vl\_stops}”  
search = Regexp.new(“^\b(#{vl\_stops}:)\w\*”)  
files.each do |file|  
f = File.open(directory+file)  
f.grep(search)  
puts #what goes here? line?  
end  
f.close  
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!

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [April 21, 2008, 8:30am UTC](https://www.ruby-forum.com/t/extract-lines-with-regular-expressions/135321/2 "2008-04-21T08:30:49Z")

</div>

On 21.04.2008 07:49, Michael Dore wrote:

> and it does exactly what I want, but…  
> puts #what goes here? line?  
> Thanks!  
> files.each do |file|  
> File.open(directory+file) do |f|  
> puts f.grep(search)  
> end

Or, for large files

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

```

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [April 21, 2008, 10:54am UTC](https://www.ruby-forum.com/t/extract-lines-with-regular-expressions/135321/3 "2008-04-21T10:54:37Z")

</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|  
if line.include?(search\_string)  
puts line  
end  
end
