Nila
2012-Oct-02 04:00 UTC
How to edit each line of a file in ruby, without using a temp file
Hi, Is there a way to edit each line in a file, without involving 2 files? Say, the original file has, test01 test02 test03 I want to edit it like test01,a test02,a test03,a Tried something like this, but it replaces some of the characters. File.open(''mytest.csv'', ''r+'') do |file| file.each_line do |line| file.seek(-line.length, IO::SEEK_CUR) file.puts ''a'' end end Writing it to a temporary file and then replace the original file works, However, I need to edit the file quite often and therefore prefer to do it within the file itself .Any pointers are appreciated. Thank you! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aGb5C9X9JZUJ. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2012-Oct-02 07:29 UTC
Re: How to edit each line of a file in ruby, without using a temp file
On 2 October 2012 05:00, Nila <enilanthi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Is there a way to edit each line in a file, without involving 2 files? Say, > the original file has, > > test01 > test02 > test03 > > I want to edit it like > test01,a > test02,a > test03,a > > Tried something like this, but it replaces some of the characters. > File.open(''mytest.csv'', ''r+'') do |file| > file.each_line do |line| > file.seek(-line.length, IO::SEEK_CUR) > file.puts ''a'' > end > end > > Writing it to a temporary file and then replace the original file works, > However, I need to edit the file quite often and therefore prefer to do it > within the file itself .Any pointers are appreciated.It sounds like you might be better using records in a database for each line and re-generate the file when it is needed as a file. Rails is quite good at modifying records in a database :) Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2012-Oct-02 14:12 UTC
Re: How to edit each line of a file in ruby, without using a temp file
On Oct 2, 2012, at 12:00 AM, Nila wrote:> Hi, > > Is there a way to edit each line in a file, without involving 2 files? Say, the original file has, > > test01 > test02 > test03 > > I want to edit it like > test01,a > test02,a > test03,a > > Tried something like this, but it replaces some of the characters. > File.open(''mytest.csv'', ''r+'') do |file| > file.each_line do |line| > file.seek(-line.length, IO::SEEK_CUR) > file.puts ''a'' > end > end > > Writing it to a temporary file and then replace the original file works, However, I need to edit the file quite often and therefore prefer to do it within the file itself .Any pointers are appreciated.You can stash the data in an array, manipulate it there, and then write it back to the file store after. Ignore the HEREDOC parts of this, and substitute your file read/write bits, and this should work: foo = <<EOF one two three EOF bar = [] foo.each_line do |line| bar.push (line.strip + '',a'') end foo = bar.join("\n") puts foo Walter> > Thank you! > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aGb5C9X9JZUJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
ni ed
2012-Oct-08 06:13 UTC
Re: How to edit each line of a file in ruby, without using a temp file
Thank you very much for the help. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker
2012-Oct-08 12:11 UTC
Re: How to edit each line of a file in ruby, without using a temp file
Walter Davis wrote in post #1078369:> On Oct 2, 2012, at 12:00 AM, Nila wrote: > >> test02,a >> Writing it to a temporary file and then replace the original file works, > However, I need to edit the file quite often and therefore prefer to do > it within > the file itself .Any pointers are appreciated. > You can stash the data in an array, manipulate it there, and then write > it back to the file store after. Ignore the HEREDOC parts of this, and > substitute your file read/write bits, and this should work: > > foo = <<EOF > one > two > three > EOF > bar = [] > foo.each_line do |line| > bar.push (line.strip + '',a'') > end > foo = bar.join("\n") > puts fooKeep in mind that doing it this way will load the entire file into RAM, may not be an issue given today''s servers have lots of RAM. However, doing what Colin suggested, using a database, would almost certainly be the more efficient solution. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.