From: Robert Klemme Date: 2012-08-13T03:02:14+09:00 Subject: Re: Delete a lines from a files On Sun, Aug 12, 2012 at 7:49 PM, Admin Tensor wrote: > Robert Klemme wrote in post #1072081: >> If you really only need to delete a single line from a file sed is >> probably the easiest choice: >> >> without backup >> $ sed -i '/^john/ d' your_file >> >> with backup >> $ sed -i.bak '/^john/ d' your_file > I think because this discussion is about Ruby and the OP may or may not > have access to sed, we can follow Dave Thomas' way on the command line: > > ruby -e 'BEGIN{$/=nil}; puts STDIN.readlines.to_s.gsub(/^john.*$/, "")' > < your_file Why do you set the input record separator to nil? How then should readlines work? Also, I hope you are aware that readlines will read in the whole input before outputting anything. This is quite inefficient for medium to large files. ARGF might be better, too. > I haven't tried the code myself yet, so it has to be taken with a grain > of salt; and as written, it only outputs to stdout. Well, there is a Ruby solution closer to sed solution I posted earlier: # without backup $ ruby19 -i -ne 'puts $_ unless /^john/' your_file # with backup $ ruby19 -i.bak -ne 'puts $_ unless /^john/' your_file Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/