# Parse text with ruby

**URL:** <https://www.ruby-forum.com/t/parse-text-with-ruby/105730>\
**Category:** Ruby\
**Created:** [June 1, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730 "2007-06-01T20:56:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 1, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730/1 "2007-06-01T20:56:20Z")

</div>

Hello friends,

I have the following txt log file ==\>

# ================================================================================= [DEBUG] 5/24/07 5:35:50 PM : Finished to read one line : r 250 1006 -63 -65 91 -6 -8 319\* [DEBUG] 5/24/07 5:35:50 PM : Successfully opened DB. [DEBUG] 5/24/07 5:35:50 PM : Going to read one line. [DEBUG] 5/24/07 5:35:50 PM : Going to close DB. [DEBUG] 5/24/07 5:35:50 PM : Successfully closed DB. [DEBUG] 5/24/07 5:35:50 PM : Going to open DB. [DEBUG] 5/24/07 5:35:50 PM : Finished to read one line : r 250 1015 -52 -72 91 -6 -8 319\* [DEBUG] 5/24/07 5:35:50 PM : Going to read one line. [INFO] 5/24/07 5:35:50 PM : Call RaceField constructor. [INFO] 5/24/07 5:35:50 PM : Opponent has no record of today. [DEBUG] 5/24/07 5:35:50 PM : Finished to read one line : r 250 1019 -61 -83 92 -7 -8 319\* [DEBUG] 5/24/07 5:35:50 PM : Going to read one line. [DEBUG] 5/24/07 5:35:50 PM : Going to close DB.

I would like to parse this file with ruby, and to produce a new txt file  
where i have only ==\>

r 250 1006 -63 -65 91 -6 -8 319\*  
r 250 1015 -52 -72 91 -6 -8 319\*  
r 250 1019 -61 -83 92 -7 -8 319\*

How can i do that?

thank you  
i am a ruby newbie

kostas

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 1, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730/2 "2007-06-01T21:08:58Z")

</div>

On Jun 1, 2007, at 14:56 , kostas wrote:

> I would like to parse this file with ruby, and to produce a new txt  
> file  
> where i have only ==\>
> 
> r 250 1006 -63 -65 91 -6 -8 319\*  
> r 250 1015 -52 -72 91 -6 -8 319\*  
> r 250 1019 -61 -83 92 -7 -8 319\*

Use the right tool(s) for the job:

% cut -f 5 -d: x.txt | grep . | cut -c2-  
r 250 1006 -63 -65 91 -6 -8 319\*  
r 250 1015 -52 -72 91 -6 -8 319\*  
r 250 1019 -61 -83 92 -7 -8 319\*

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 1, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730/3 "2007-06-01T21:27:00Z")

</div>

Ryan D. wrote:

> Use the right tool(s) for the job:
> 
> % cut -f 5 -d: x.txt | grep . | cut -c2-  
> r 250 1006 -63 -65 91 -6 -8 319\*  
> r 250 1015 -52 -72 91 -6 -8 319\*  
> r 250 1019 -61 -83 92 -7 -8 319\*

Who knows if that’s going to work right if other output is mixed in?

$ ruby -ne 'puts $1 if /Finished to read one line : (._)/’ x.txt  
r 250 1006 -63 -65 91 -6 -8 319_  
r 250 1015 -52 -72 91 -6 -8 319\*  
r 250 1019 -61 -83 92 -7 -8 319\*

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 1, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730/4 "2007-06-01T21:55:02Z")

</div>

Hi,

> r 250 1019 -61 -83 92 -7 -8 319\*  
> You could iterate through each line, scanning for a second ‘:’ and grab  
> all the text after that.

-Mitchell;

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 1, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730/5 "2007-06-01T21:54:01Z")

</div>

On Jun 1, 2007, at 15:26 , Joel VanderWerf wrote:

> Who knows if that’s going to work right if other output is mixed in?

Presumably the OP.

---

<div class="post-metadata">

**Author:** ![kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/bc8723/32.png) [@kostas](https://www.ruby-forum.com/u/kostas)\
**Post date:** [June 10, 2007, 12:37am UTC](https://www.ruby-forum.com/t/parse-text-with-ruby/105730/6 "2007-06-10T00:37:02Z")

</div>

kostas wrote:  
…

> =================================================================================  
> [DEBUG] 5/24/07 5:35:50 PM : Finished to read one line : r 250 1006 -63  
> -65 91 -6 -8 319\*  
> [DEBUG] 5/24/07 5:35:50 PM : Successfully opened DB.  
> …  
> I think that you should tell us some about this log. Eg if '_’ character  
> at the end of line appears always in lines what you want analize?  
> (and don’t appear in others).  
> The second important thing is a process of cerate log - if date and time  
> format are ‘hard coded’ or if they use locale?  
> …  
> r 250 1019 -61 -83 92 -7 -8 319_

to see this info at the output i use two ‘:’ characters with separate  
char behind it. It looks for me to the ‘hard coded’ in app.

Eg code :

logFile = File.new(“path to log file”)

logFile.each do |line|  
if line =~ /._:\s._:\s._/  
line.sub!(/._:\s.\*:\s/,’’)  
puts line  
end  
end
