def ChangeOnFile(file, regex_to_find, text_to_put_in_place)
text= File.read file
File.open(file, 'w+'){|f| f << text.gsub(regex_to_find,
text_to_put_in_place)}
end
Then:
ChangeOnFile('/etc/myfile.conf', /30890/, "32737")
Phil Swenson wrote:
> I need to search a specific xml file in a directory, look for:
>
> <port system-property="tangosol.coherence.clusterport">30890</port>
>
> and if the port 30890 is still there replace it with a different
> integer.
>
> I know of several ways to do this (like open the file, iterate the
> lines until I find the integer, do a gsub! on on the integer). But it
> seems like there might be a better way to do it. Is there a search
> and replace API I don't know about?
>
> Any thoughts?
--
Posted via http://www.ruby-forum.com/.