On Sun, Aug 12, 2012 at 7:49 PM, Admin Tensor <[email protected]> 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/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en