# File parsing

**URL:** <https://www.ruby-forum.com/t/file-parsing/111588>\
**Category:** Ruby\
**Created:** [August 8, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/file-parsing/111588 "2007-08-08T21:06:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [August 8, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/file-parsing/111588/1 "2007-08-08T21:06:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [August 8, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/file-parsing/111588/2 "2007-08-08T21:19:06Z")

</div>

Matthew L. 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

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [August 8, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/file-parsing/111588/3 "2007-08-08T21:37:59Z")

</div>

It works! Thanks!
