[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

File parsing

Matthew Lagace

8/8/2007 7:07:00 PM

Hello,

I have a tab delimited file, I have no headers in the file, and one
column is the datestamp of the record. Now this datestamp is not in the
correct format, I know how to format the datestamp to the way I want,
but how do I replace the incorrect datestamp in the file with the new
datestamp I have formatted.

For e.g.: The datestamp in the file is 2007-12-30 12:32:12,
Here is the datestamp that I want to replace it with instead 20071230

How do I replace the old datestamp with my new datestamp in a file?

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

2 Answers

Drew Olson

8/8/2007 7:19:00 PM

0

Matthew Lagace wrote:
> Hello,
>
> I have a tab delimited file, I have no headers in the file, and one
> column is the datestamp of the record. Now this datestamp is not in the
> correct format, I know how to format the datestamp to the way I want,
> but how do I replace the incorrect datestamp in the file with the new
> datestamp I have formatted.
>
> For e.g.: The datestamp in the file is 2007-12-30 12:32:12,
> Here is the datestamp that I want to replace it with instead 20071230
>
> How do I replace the old datestamp with my new datestamp in a file?
>
> Thanks

Maybe this?

File.open("newfile.txt","w") do |out|
File.open("curfile.txt","r").each do |line|
out << line.gsub(/(\d{4})-(\d{2})-(\d{2})
\d{2}:\d{2}:\d{2}/,'\1\2\3')
end
end
--
Posted via http://www.ruby-....

Matthew Lagace

8/8/2007 7:39:00 PM

0


It works! Thanks!
--
Posted via http://www.ruby-....