Guys, you really need to stop wanking off over your great Ruby skills
and maybe post a solution that an average user can actually understand
and integrate in his code.
We all know you are the greatest Ruby hackers on earth with the fanciest
one-liners ever. But I think we're here to help and not to masturbate
all day long.
What about a simple solution?
old_file = 'test.txt'
new_file = 'test2.txt'
File.open new_file, 'w' do |file|
# loop over the lines of old_file and wrote those
# not beginning with "john" to new_file
File.foreach old_file do |line|
file << line unless line.start_with? 'john'
end
end
Write your changed data to a file with different name, the rename (move) the new file to the original file. This will reduce the probability of file corruption.
Henry
···
On 12/08/2012, at 1:28 AM, "Jan E." <lists@ruby-forum.com> wrote:
Hi,
You cannot delete a line from a file. But you can overwrite the file and
skip this line.
Guys, you really need to stop wanking off over your great Ruby skills
and maybe post a solution that an average user can actually understand
and integrate in his code.
LOl thanks , those one liner was bit hard for me to understand ..
We all know you are the greatest Ruby hackers on earth with the fanciest
one-liners ever. But I think we're here to help and not to masturbate
all day long.
What about a simple solution?
old_file = 'test.txt'
new_file = 'test2.txt'
File.open new_file, 'w' do |file|
# loop over the lines of old_file and write those
# not beginning with "john" to new_file
File.foreach old_file do |line|
file << line unless line.start_with? 'john'
end
end
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:
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.
Well, I haven't tried the code yet. For your questions, probably better
to ask Dave Thomas... I took it from his one liners.