[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

parse text with ruby

kostas

6/1/2007 6:56:00 PM

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

--
Posted via http://www.ruby-....

5 Answers

Ryan Davis

6/1/2007 7:08:00 PM

0


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*


Joel VanderWerf

6/1/2007 7:26:00 PM

0

Ryan Davis wrote:
>
> 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*

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*

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Ryan Davis

6/1/2007 7:53:00 PM

0


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.


mitchell

6/1/2007 7:53:00 PM

0

GrzechG

6/9/2007 10:35:00 PM

0

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