How to delete a line in a file if....?

All-

Please advise as to how I can delete a line in a file in which a given string appears.

I tried using the delete_if method in the following code, but it failed. (I suspect that delete_if might be an array method rather than a file method. In the code below, “content2” is a file, not an array.)

if FileTest.exist?("input_installs/#{field[5]}" )
	install_file = ''
	File.open("./input_installs/#{field[5]}") { |f| install_file = f.read }
	content2.gsub!(/<install_template.txt>/, install_file)
	content2.delete_if { |l| l =~ "uncompress" }
else
(blah blah...)

For the present, I’d prefer to avoid using arrays, so I need a way to search a file and delete any line in which a certain string (eg “uncompress”) occurs.

Thanks!

-Kurt

Is something like this what you mean? (I didn’t debug this btw)
fh = File.open(“file”,“r”);
out = File.open(“file_result”,“w”);
fh.each { |line|
out.puts line unless line =~ /unwantedstring/
}
fh.close; out.close;

···

On Mon, Jul 15, 2002 at 07:07:55AM +0900, Kurt Euler wrote:

All-

Please advise as to how I can delete a line in a file in which a given string appears.

I tried using the delete_if method in the following code, but it failed. (I suspect that delete_if might be an array method rather than a file method. In the code below, “content2” is a file, not an array.)

if FileTest.exist?(“input_installs/#{field[5]}” )
install_file = ''
File.open("./input_installs/#{field[5]}") { |f| install_file = f.read }
content2.gsub!(/<install_template.txt>/, install_file)
content2.delete_if { |l| l =~ “uncompress” }
else
(blah blah…)

For the present, I’d prefer to avoid using arrays, so I need a way to search a file and delete any line in which a certain string (eg “uncompress”) occurs.

Thanks!

-Kurt


P.S. Only personal email to this email please.

Tussman’s Law:
Nothing is as inevitable as a mistake whose time has come.