From: Emmanuel Oga Date: 2007-04-05T04:53:38+09:00 Subject: Re: search a file and replace text 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: > > 30890 > > 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/.