# Single Line replacement in file

**URL:** <https://www.ruby-forum.com/t/single-line-replacement-in-file/144287>\
**Category:** Ruby\
**Created:** [July 28, 2008, 7:41am UTC](https://www.ruby-forum.com/t/single-line-replacement-in-file/144287 "2008-07-28T07:41:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [July 28, 2008, 7:41am UTC](https://www.ruby-forum.com/t/single-line-replacement-in-file/144287/1 "2008-07-28T07:41:02Z")

</div>

Hi All,

My code:

code.rb

lines=File.open(“hi.txt”,“r”)  
output=[]  
lines.each do |single|  
out1=single.split(’ ‘)  
out2=out1[0].gsub(’**‘, ‘’)  
out3= out2.gsub(’**‘, ‘’)  
out4=out3.gsub(’‘,’')  
output \<\< out4 # I need to overwrite the appropriate line.  
end

hi.txt:

[WordPress.com](http://wordpress.com/tag/) **jazzez** / - 11k -  
[swik.net/](http://swik.net/) **jazzez** - 40k -  
[raveendran.wordpress.com/category/](http://raveendran.wordpress.com/category/) **jazzez** / - 15k -  
[www.linkedin.com/pub/2/377/3A2](http://www.linkedin.com/pub/2/377/3A2) - 10k -  
[www.bebo.com/Profile.jsp?MemberId=4902204778](http://www.bebo.com/Profile.jsp?MemberId=4902204778) -

My need:

code.rb → out contains my proper output. But i didn’t able to  
overwrite the same line in file.It means

1. single → "[WordPress.com](http://wordpress.com/tag/) **jazzez** / - 11k - "
2. out4 → “[wordpress.com/tag/jazzez](http://wordpress.com/tag/jazzez)”

Here i want to replace the first line in hi.txt file 1 to 2.

Awaiting your reply

Regards,  
P.Raveendran  
RF, chennai

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [July 30, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/single-line-replacement-in-file/144287/2 "2008-07-30T13:15:45Z")

</div>

“Raveendran J.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) schrieb im Newsbeitrag  
news:removed\_email\_address@domain.invalid…

> My need:
> 
> code.rb → out contains my proper output. But i didn’t able to  
> overwrite the same line in file.It means
> 
> 1. single → "[WordPress.com](http://wordpress.com/tag/) **jazzez** / - 11k - "
> 2. out4 → “[WordPress.com](http://wordpress.com/tag/jazzez)”
> 
> Here i want to replace the first line in hi.txt file 1 to 2.

You can’t replace “lines” in a file because file on disk doesn’t know  
about  
lines.

You can read one file, filter its content, write into another file,  
close  
first file, close second file, delete first file or rename to backup,  
rename  
second file to first file.  
This would be the easiest way.

or

You read complete file into memory, filter there, seek to file start,  
write  
contents from memory, resize.  
More dangerous on errors.

or

You replace characters of these tags by spaces directly in the file, may  
be  
possible but needs more repositioning in file.

I also would recommend to analyze the XML-tree instead of simply  
removing  
the tags wherever found.  
Use, for example, “rexml” as contained in Ruby 1.8.

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [July 31, 2008, 6:09am UTC](https://www.ruby-forum.com/t/single-line-replacement-in-file/144287/3 "2008-07-31T06:09:46Z")

</div>

Hi Michael Bruschkewitz ,

Thanks for your detailed explanation.

You can read one file, filter its content, write into another file,  
close  
first file, close second file, delete first file or rename to backup,  
rename  
second file to first file.  
This would be the easiest way.

I think this one is very simple and safety way…

Thanks.  
P.Raveendran  
RF, chennai

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [July 31, 2008, 10:58am UTC](https://www.ruby-forum.com/t/single-line-replacement-in-file/144287/4 "2008-07-31T10:58:04Z")

</div>

On Thu, Jul 31, 2008 at 6:09 AM, jazzez ravi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This would be the easiest way.
> 
> I think this one is very simple and safety way…

You might want to check out the -i option of Ruby.

## Robert

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

There’s no one thing that’s true. It’s all true.
