From: "Gerardo Santana Gómez Garrido" Date: 2006-03-27T06:39:54+09:00 Subject: Re: Ruby-esque approaches to adding a line at the beginning of a file. 2006/3/26, 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 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 Santana "Between individuals, as between nations, respect for the rights of others is peace" - Don Benito Ju�rez http://santanatechnotes.blogspot.com/