# Simple Reading, Deleting and Writing to Files

**URL:** <https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476>\
**Category:** Ruby\
**Created:** [August 5, 2006, 6:26am UTC](https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476 "2006-08-05T06:26:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [August 5, 2006, 6:26am UTC](https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476/1 "2006-08-05T06:26:43Z")

</div>

i have done some research, but can’t see anything that explains simple  
file writing. eventually, i may end using rexml, but for now i just  
want to use a simple text file.

ip.txt contains three lines of text. they are as follows:

#ip 127.0.0.1  
#email removed\_email\_address@domain.invalid  
#duration 600

thanks to help i received on this forum, i know how to open the file.  
i also figured out how to grep a line of text and then manipulate the  
line so i get only the data of interest.

what i don’t know how to do is to…

1. write this data to a newly created file (case: file doesn’t exist)
2. overwrite the data (case: file exists but data needs to be updated).

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.

can anyone point me to a tutorial or lend a hand?

tia…

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [August 5, 2006, 7:02am UTC](https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476/2 "2006-08-05T07:02:46Z")

</div>

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)}

> 1. 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.

> can anyone point me to a tutorial or lend a hand?

Look at

[http://www.ruby-doc.org/core/classes/IO.html](http://www.ruby-doc.org/core/classes/IO.html)

and

[class File - RDoc Documentation](http://www.ruby-doc.org/core/classes/File.html)

Note that the File class inherits from IO, so behavior of interest may  
be defined in either place.

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [August 5, 2006, 8:20am UTC](https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476/3 "2006-08-05T08:20:49Z")

</div>

James B. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [August 5, 2006, 9:05am UTC](https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476/4 "2006-08-05T09:05:52Z")

</div>

Skeets wrote:

> > > 1. overwrite the data (case: file exists but data needs to be updated).
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [August 5, 2006, 10:50am UTC](https://www.ruby-forum.com/t/simple-reading-deleting-and-writing-to-files/101476/5 "2006-08-05T10:50:55Z")

</div>

William J. wrote:

> > > You can use the same code. It will overwrite the current file with the  
> > > #ip line.  
> > > 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 removed\_email\_address@domain.invalid  
#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 removed\_email\_address@domain.invalid  
#duration 600

robert@fussel /cygdrive/c/Temp  
$ cat ip.txt.bak  
#ip 127.0.0.1  
#email removed\_email\_address@domain.invalid  
#duration 600

robert@fussel /cygdrive/c/Temp  
$

Kind regards

```
robert
```
