# Parsing an text file

**URL:** <https://www.ruby-forum.com/t/parsing-an-text-file/90761>\
**Category:** Ruby\
**Created:** [April 1, 2007, 2:43am UTC](https://www.ruby-forum.com/t/parsing-an-text-file/90761 "2007-04-01T02:43:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kickflip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7bcc69/32.png) [@kickflip](https://www.ruby-forum.com/u/kickflip)\
**Post date:** [April 1, 2007, 2:43am UTC](https://www.ruby-forum.com/t/parsing-an-text-file/90761/1 "2007-04-01T02:43:00Z")

</div>

Hi,  
I cannot find a way to easily parse a text file. I am just starting to  
learn ruby so please bear with me.

My text file looks like this:

0 8 2 9 3 0 0 4  
9 2 8 3 9 3 0 2  
2 3 4 9 8 8 9 0

Basically just numbers and spaces.

So far, I know how to read the file and put each line into an array.

def readfile(file)  
arr = IO.readlines(file)  
parsing(arr)  
end

def parsing(arr)  
string1 = arr[0].to\_s  
string2 = arr[1].to\_s  
string3 = arr[2].to\_s  
ans.each\_byte {|b|p b.chr}  
end

What I wanted to do is, for example, want to find all 8’s and change it  
to 5’s.  
I am stuck on how I can use the string and search each char’s to find  
the 8’s and then change it. Also I would like to know if there is a for  
loop or something to go through each array and store it into a new  
string because there might be more than 3 lines. I have search through  
documentations and trying to find examples but there doesn’t seem to be  
much help.

Any help, especially examples, would be appreciate. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kickflip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7bcc69/32.png) [@kickflip](https://www.ruby-forum.com/u/kickflip)\
**Post date:** [April 1, 2007, 2:47am UTC](https://www.ruby-forum.com/t/parsing-an-text-file/90761/2 "2007-04-01T02:47:47Z")

</div>

Johnathan Wagner wrote:

> What I wanted to do is, for example, want to find all 8’s and change it  
> to 5’s.  
> I am stuck on how I can use the string and search each char’s to find  
> the 8’s and then change it. Also I would like to know if there is a for  
> loop or something to go through each array and store it into a new  
> string because there might be more than 3 lines. I have search through  
> documentations and trying to find examples but there doesn’t seem to be  
> much help.
> 
> Any help, especially examples, would be appreciate. Thanks in advance.

The code below reads through your file, replaces 8’s with 5’s and then  
writes out the lines to a new file:

File.open(“newfile,txt”,“w”) do |out|  
File.open(“myfile.txt”).each do |line|  
out \<\< line.gsub(“8”,“5”)  
end  
end

---

<div class="post-metadata">

**Author:** ![kickflip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7bcc69/32.png) [@kickflip](https://www.ruby-forum.com/u/kickflip)\
**Post date:** [April 1, 2007, 3:02am UTC](https://www.ruby-forum.com/t/parsing-an-text-file/90761/3 "2007-04-01T03:02:05Z")

</div>

Thanks for your fast answer.

One last question, if you can help me.

How can I compare each characters in a string?

Drew O. wrote:

> Johnathan Wagner wrote:
> 
> > What I wanted to do is, for example, want to find all 8’s and change it  
> > to 5’s.  
> > I am stuck on how I can use the string and search each char’s to find  
> > the 8’s and then change it. Also I would like to know if there is a for  
> > loop or something to go through each array and store it into a new  
> > string because there might be more than 3 lines. I have search through  
> > documentations and trying to find examples but there doesn’t seem to be  
> > much help.
> > 
> > Any help, especially examples, would be appreciate. Thanks in advance.
> 
> The code below reads through your file, replaces 8’s with 5’s and then  
> writes out the lines to a new file:
> 
> File.open(“newfile,txt”,“w”) do |out|  
> File.open(“myfile.txt”).each do |line|  
> out \<\< line.gsub(“8”,“5”)  
> end  
> end

---

<div class="post-metadata">

**Author:** ![kickflip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7bcc69/32.png) [@kickflip](https://www.ruby-forum.com/u/kickflip)\
**Post date:** [April 1, 2007, 3:08am UTC](https://www.ruby-forum.com/t/parsing-an-text-file/90761/4 "2007-04-01T03:08:12Z")

</div>

Johnathan Wagner wrote:

> Thanks for your fast answer.
> 
> One last question, if you can help me.
> 
> How can I compare each characters in a string?

You can compare each character individually by using the each\_byte  
method, however that gives you the ASCII representation of the  
character. In your case I would split each line by spaces to get each  
number as a substring like so:

File.open(“myfile.txt”).each do |line|  
line.split(" ").each do |num|  
# do your work here  
end  
end
