# Ruby-esque approaches to adding a line at the beginning of a

**URL:** <https://www.ruby-forum.com/t/ruby-esque-approaches-to-adding-a-line-at-the-beginning-of-a/87368>\
**Category:** Ruby\
**Created:** [March 26, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/ruby-esque-approaches-to-adding-a-line-at-the-beginning-of-a/87368 "2006-03-26T22:55:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [March 26, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/ruby-esque-approaches-to-adding-a-line-at-the-beginning-of-a/87368/1 "2006-03-26T22:55:32Z")

</div>

On our local lug mail list a discussion came up about using Ruby (as  
opposed  
to Perl) for an application that involved inserting a new line at the  
front  
of an existing file.

(As a newbie), I proposed an approach like this (with help from the Ruby  
Cookbook which I’m working my way through).

t = “New first line of file.\n”  
t = t + open(“sample\_file”) { |f| f.read }  
open(“sample\_file”, “w”) { |f| f.write(t)}

However, it doesn’t appear (to me) to be very Ruby-esque–I’m wondering  
how  
others on this list might approach such a task.

Randy K.

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [March 26, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/ruby-esque-approaches-to-adding-a-line-at-the-beginning-of-a/87368/2 "2006-03-26T23:20:55Z")

</div>

On Mar 26, 2006, at 12:55 PM, Randy K. wrote:

> t = “New first line of file.\n”  
> t = t + open(“sample\_file”) { |f| f.read }  
> open(“sample\_file”, “w”) { |f| f.write(t)}
> 
> However, it doesn’t appear (to me) to be very Ruby-esque–I’m  
> wondering how  
> others on this list might approach such a task.

I think it’s as good as anything. You could scrunch it up a bit like  
this:

```
 t = "New First Line\n" + File.read("sample_file")
 ...

```

–Steve

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [March 26, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/ruby-esque-approaches-to-adding-a-line-at-the-beginning-of-a/87368/3 "2006-03-26T23:40:06Z")

</div>

2006/3/26, Randy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> However, it doesn’t appear (to me) to be very Ruby-esque–I’m wondering how  
> others on this list might approach such a task.
> 
> Randy K.

If the intention is to do it in the command line, you can write:

ruby -i -pe ‘puts “New first line” if ARGF.file.lineno == 1’ file

## and that will work even with multiple files

Gerardo S.  
“Between individuals, as between nations, respect for the rights of  
others is peace” - Don Benito Juárez

> **[Santana's Tech Notes](http://santanatechnotes.blogspot.com/)**
>
> Small notes about technical related stuff I do, mainly OpenBSD and Ruby

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [March 27, 2006, 12:09am UTC](https://www.ruby-forum.com/t/ruby-esque-approaches-to-adding-a-line-at-the-beginning-of-a/87368/4 "2006-03-27T00:09:06Z")

</div>

On Mon, 27 Mar 2006 05:55:30 +0900, Randy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On our local lug mail list a discussion came up about using Ruby (as opposed  
> to Perl) for an application that involved inserting a new line at the front  
> of an existing file.

Well, assuming reading the entire file into memory is acceptable,  
we can use a fairly standard approach without a temporary file:

#!/usr/bin/ruby -w

file = ‘somefile’  
first = “new start\n”

File.open(file,‘r+’) do |f|  
contents = f.read  
f.seek 0, IO::SEEK\_SET  
f.truncate 0  
f.puts first, contents  
end

And, if you like, you can wrap the manipulations with #flock

File.open(file,‘r+’) do |f|  
f.flock File::LOCK\_EX  
contents = f.read  
f.seek 0, IO::SEEK\_SET  
f.truncate 0  
f.puts first, contents  
f.flock File::LOCK\_UN  
end

Of course, this is a generalized version for any content manipulation  
you  
might want to do on the contents, not just adding a new first line.

Otherwise, loop over lines, making changes and writing to a temp file,  
and  
afterwards, #rename the temp file back to the original.

Or, use the command line -i switch:

$ cat somefile  
one  
two  
three

$ ruby -pi -e ‘puts “zero\n” if $. == 1’ somefile

$ cat somefile  
zero  
one  
two  
three

regards,  
andrew
