Delete a line from a file

Hi

i have a file this

/dev/cciss/c0d10p1 /root/file/file-55 ext3
defaults,noatime,nodiratime 1 0
/dev/cciss/c0d11p1 /root/file/file-11 ext3
defaults,noatime,nodiratime 1 0
#/dev/cciss/c0d12p1 /root/file/file-SPARE ext3
defaults,noatime,nodiratime 1 0

Now i want to delete
#/dev/cciss/c0d12p1 /root/file/file-SPARE ext3
defaults,noatime,nodiratime 1 0

which has SPARE word

so i am doing this .

require ‘fileutils’

f=File.new("fstab ")
f.each do |line|
instattr <<line
if line =~/SPARE/
DLETE THE LINE
end

    end

end

But how to delete that line ???

Can any one please help me with this
Thanks

Read the file into a string, modify the string, overwrite the file with
the new string.

Joel P. wrote in post #1074655:

Read the file into a string, modify the string, overwrite the file with
the new string.

For a small file its fine. But if the file is big then
That’s the solution?

On Tue, Sep 4, 2012 at 6:23 PM, Ferdous ara [email protected]
wrote:

Joel P. wrote in post #1074655:

Read the file into a string, modify the string, overwrite the file with
the new string.

For a small file its fine. But if the file is big then
That’s the solution?

Then read line by line writing to a temporary file, skipping the line
you want to delete, then rename the file.

Jesus.

Am 04.09.2012 18:23, schrieb Ferdous ara:

Joel P. wrote in post #1074655:

Read the file into a string, modify the string, overwrite the file with
the new string.

For a small file its fine. But if the file is big then
That’s the solution?

You could read and write line by line (writing to a new file),
omitting the line you want to delete, then overwrite
the old file with the new file.

But IMO Joel’s suggestion is simpler. Is the file usually that big?

But IMO Joel’s suggestion is simpler. Is the file usually that big?

I have some massive files (1-3 MB in size) that I have to manipulate and
Ruby
handles them without any problems and very quickly (much faster than a
few other
languages I’ve tried this with).

Wayne

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs