# Basic iteration through text

**URL:** <https://www.ruby-forum.com/t/basic-iteration-through-text/173446>\
**Category:** Ruby\
**Created:** [August 9, 2009, 5:00am UTC](https://www.ruby-forum.com/t/basic-iteration-through-text/173446 "2009-08-09T05:00:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [August 9, 2009, 5:00am UTC](https://www.ruby-forum.com/t/basic-iteration-through-text/173446/1 "2009-08-09T05:00:21Z")

</div>

Howdy!

So I am trying to parse a bit of an ugly text file to a new file, and  
hence read each line of the old file, check the line for some conditions  
and then execute some other stuff, and then write out some results to a  
new file. Here is what I came up with:

my\_file = File.new(“New\_file.txt”, “w+”)

File.foreach(“LOG02.txt”) do |line|  
if line =~/^$GPRMC/.  
my\_file.puts line  
else  
print “I did not read this line: #{line}”  
end  
end

I have a feeling that I am missing something stupid but not quite sure  
what it is.

Good karma for a solution 🙂

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [August 9, 2009, 5:40am UTC](https://www.ruby-forum.com/t/basic-iteration-through-text/173446/2 "2009-08-09T05:40:57Z")

</div>

Ben Martinek wrote:

> Howdy!
> 
> So I am trying to parse a bit of an ugly text file to a new file, and  
> hence read each line of the old file, check the line for some conditions  
> and then execute some other stuff, and then write out some results to a  
> new file. Here is what I came up with:
> 
> my\_file = File.new(“New\_file.txt”, “w+”)
> 
> File.foreach(“LOG02.txt”) do |line|  
> if line =~/^$GPRMC/.  
> my\_file.puts line  
> else  
> print “I did not read this line: #{line}”  
> end  
> end
> 
> I have a feeling that I am missing something stupid but not quite sure  
> what it is.
> 
> Good karma for a solution 🙂

Okay solved my problem, it was something stupid, like the period I  
didn’t see after my line match statement.

Nothing to see here people, move along…
