# How to open Text File and then insert line in first position?

**URL:** <https://www.ruby-forum.com/t/how-to-open-text-file-and-then-insert-line-in-first-position/146899>\
**Category:** Ruby\
**Created:** [August 26, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/how-to-open-text-file-and-then-insert-line-in-first-position/146899 "2008-08-26T16:16:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pabloq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pabloq](https://www.ruby-forum.com/u/pabloq)\
**Post date:** [August 26, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/how-to-open-text-file-and-then-insert-line-in-first-position/146899/1 "2008-08-26T16:16:39Z")

</div>

Hi,

I need to add a header to files, but I didn’t find how open files in  
append  
mode (‘a’) but positioned in first position.  
Does anyone know how to do that?

Thanks

---

<div class="post-metadata">

**Author:** ![pabloq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pabloq](https://www.ruby-forum.com/u/pabloq)\
**Post date:** [August 26, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/how-to-open-text-file-and-then-insert-line-in-first-position/146899/2 "2008-08-26T17:27:47Z")

</div>

Pablo Q. wrote:

> Hi,
> 
> I need to add a header to files, but I didn’t find how open files in  
> append  
> mode (‘a’) but positioned in first position.  
> Does anyone know how to do that?
> 
> Thanks

I would write the headers to a new file and then the content.  
Then move the file over to the old file.

Easier to do it in shell (linux/mac/unix)  
echo $header \> tmpfile  
cat oldfile \>\> tmpfile  
mv tmpfile oldfile

If you need to do it in ruby see [Open file, get first line, delete first line close file - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/163631)  
for related discussion. Last post (at this time) by Erik has the sort  
of code you need - just need to modify it a little.

There is this ‘r+’ mode which allows you to write to other parts of the  
file besides appending but I don’t think it’s going to help. Someone  
might want to pick me up on that.

Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![pabloq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pabloq](https://www.ruby-forum.com/u/pabloq)\
**Post date:** [August 26, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/how-to-open-text-file-and-then-insert-line-in-first-position/146899/3 "2008-08-26T17:49:18Z")

</div>

Thanks to both,

This is what I was looking for.

---

<div class="post-metadata">

**Author:** ![pabloq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pabloq](https://www.ruby-forum.com/u/pabloq)\
**Post date:** [August 26, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/how-to-open-text-file-and-then-insert-line-in-first-position/146899/4 "2008-08-26T17:40:32Z")

</div>

On Aug 26, 8:11 am, “Pablo Q.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Pablo Q.

Try this:

file\_to\_read = ‘/path/to/file.txt’  
header = “Text to put at top of file\n\n”  
file = IO.read(file\_to\_read)  
open(file\_to\_read, ‘w’) { |f| f \<\< header \<\< file}

Cheers,

Craig
