# Open a remote file and edit it

**URL:** <https://www.ruby-forum.com/t/open-a-remote-file-and-edit-it/142262>\
**Category:** Ruby\
**Created:** [July 5, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/open-a-remote-file-and-edit-it/142262 "2008-07-05T12:48:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [July 5, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/open-a-remote-file-and-edit-it/142262/1 "2008-07-05T12:48:22Z")

</div>

Guys i want to open a file on another machine lets say test.txt, read it  
find (grep) for example the text “localhost” and replace it with a  
machine name and then close the file. I can connect to the machine using  
ssh or sftp but as far editing a file I’m not sure how to.

Any help much appreciated.

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [July 5, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/open-a-remote-file-and-edit-it/142262/2 "2008-07-05T13:00:54Z")

</div>

Paul Donaghy wrote:

> Guys i want to open a file on another machine lets say test.txt, read it  
> find (grep) for example the text “localhost” and replace it with a  
> machine name and then close the file. I can connect to the machine using  
> ssh or sftp but as far editing a file I’m not sure how to.
> 
> Any help much appreciated.

require ‘net/ssh’  
require ‘net/sftp’

Net::SSH.start( ‘server’, ‘user’, ‘password’ ) do |session|  
session.sftp.connect do |sftp|  
# use sftp to pull the file to a local copy  
# perform string surgery on the local copy  
# and use sftp.put\_file to copy it back  
end  
end
