# Updating header in file

**URL:** <https://www.ruby-forum.com/t/updating-header-in-file/111147>\
**Category:** Ruby\
**Created:** [August 3, 2007, 2:56am UTC](https://www.ruby-forum.com/t/updating-header-in-file/111147 "2007-08-03T02:56:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [August 3, 2007, 2:56am UTC](https://www.ruby-forum.com/t/updating-header-in-file/111147/1 "2007-08-03T02:56:40Z")

</div>

Let me try this in another way. I have a file  
(test.txt) where each line contains comma separated  
values and I am using CSV to work though that stored  
file. The first line of the file is a header used by  
the CSV program and as hash keys in the program that  
creates the file.

What I am looking to do is update test.txt in two way.  
First I add a line at the end of the file with  
today’s values for each entry in the first line  
(header). Like 1, 20, 300, etc. Which I can do with

file = File.open(“test.tet”)  
file.puts(x) #x being string with the comma separated  
values.

Second I will sometimes find a need to add another  
header/hash key to the end of the first line. That is  
replace the first line (header) with a new one.

The only way that I can think of is to create a new  
file, enter a new first line and then move line 2  
thought the last line from file 1 into file 2 and then  
rename file 2 to file 1.

I was hoping there was a way to replace the first line  
in a text file.

Thanks Jeff

---

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [August 3, 2007, 4:10am UTC](https://www.ruby-forum.com/t/updating-header-in-file/111147/2 "2007-08-03T04:10:50Z")

</div>

On Aug 2, 2007, at 7:55 PM, Jeffrey B. wrote:

> The only way that I can think of is to create a new  
> file, enter a new first line and then move line 2  
> thought the last line from file 1 into file 2 and then  
> rename file 2 to file 1.

This is the right strategy. You’ve just described a solid file  
changing idiom we all use regularly. Code it up and pat yourself on  
the back for a job well done.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [August 3, 2007, 4:18am UTC](https://www.ruby-forum.com/t/updating-header-in-file/111147/3 "2007-08-03T04:18:37Z")

</div>

look into using the .slice(# , #) method. so long as you know the  
length of the first line u can probably just slice it out

---

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [August 3, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/updating-header-in-file/111147/4 "2007-08-03T15:00:05Z")

</div>

2007/8/3, Jon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> look into using the .slice(# , #) method. so long as you know the  
> length of the first line u can probably just slice it out

This is a dangerous approach as it will work for the minority of cases  
only (i.e. when old and new line have the exact same length). Though  
you could make it work for cases where the new line is shorter than  
the old line things get messy if you try to cope with the opposite  
case.

Ah, and Jeffrey, do yourself a favor and use the block form of  
File.open (btw, your sample does not work anyway since  
File.open(file\_name) will open for reading only).

File.open(foo, “a”) {|fi| fi.puts x}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rorjeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@rorjeff](https://www.ruby-forum.com/u/rorjeff)\
**Post date:** [August 3, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/updating-header-in-file/111147/5 "2007-08-03T17:18:24Z")

</div>

On 8/2/07, Jeffrey B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (header). Like 1, 20, 300, etc. Which I can do with  
> file, enter a new first line and then move line 2  
> thought the last line from file 1 into file 2 and then  
> rename file 2 to file 1.
> 
> I was hoping there was a way to replace the first line  
> in a text file.
> 
> Thanks Jeff

[http://rubyreports.org/](http://rubyreports.org/). Ruport will let you parse in the file, do  
all sorts of data manipulation (including everything you described)  
and write it back out as csv.
