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

R

Randy Kramer

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 Kramer
 
S

Stephen Waits

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
 
G

Gerardo Santana Gómez Garrido

2006/3/26 said:
On our local lug mail list a discussion came up about using Ruby (as oppo= sed
to Perl) for an application that involved inserting a new line at the fro= nt
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 =3D "New first line of file.\n"
t =3D 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 h= ow
others on this list might approach such a task.

Randy Kramer


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 =3D=3D 1' file

and that will work even with multiple files
 
A

Andrew Johnson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top