William James wrote: > Skeets wrote: >> James Britt wrote: >>> Skeets wrote: >>> >>>> what i don't know how to do is to... >>>> >>>> 1. write this data to a newly created file (case: file doesn't exist) >>> File.open( some_file_name, 'w'){|f| f.puts(the_data)} >>> >>>> 2. overwrite the data (case: file exists but data needs to be updated). >>> You can use the same code. It will overwrite the current file with the >>> new data. >>> >>>> eg, let's say my ip changed to 127.0.0.2. i want to delete "#ip >>>> 127.0.0.1" and replace it with "#ip 127.0.0.2". i i'd also consider if >>>> deleting just 127.0.0.1 and replacing it with 127.0.0.2 if it makes >>>> more sense. >>> Just overwrite the old file with the new, complete, data. >> thanks for the tip. at first glance, though, i'm thinking i want to >> delete only the line(s) that change. eg, if the i line changes, i >> don't want to read in the whole file, i'd prefer to just overwrite the >> #ip line. >> >> is this doable? if so, is it too much trouble for a short file like >> this one? >> >> i perused the linked references, but didn't find a way to delete a >> specific line and replace it. >> >> thanks again. > > Load the whole file that you want to change into an array. > Each element of the array is a line of the file. > Change a line. > Write the lines back to the file. Here's another alternative: use "ruby -i.bak" to change the file in place, like $ cat ip.txt #ip 127.0.0.1 #email address / url.com #duration 600 robert@fussel /cygdrive/c/Temp $ ruby -i.bak -p -e '$_.gsub!(/^#ip (\d+(?:\.\d+){3})/, "#ip 127.0.0.2")' ip.txt robert@fussel /cygdrive/c/Temp $ cat ip.txt #ip 127.0.0.2 #email address / url.com #duration 600 robert@fussel /cygdrive/c/Temp $ cat ip.txt.bak #ip 127.0.0.1 #email address / url.com #duration 600 robert@fussel /cygdrive/c/Temp $ Kind regards robert