Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Identify IP address from a text file and replace it with new address.

Reply
Thread Tools

Identify IP address from a text file and replace it with new address.

 
 
Chandu80
Guest
Posts: n/a
 
      11-08-2010
Hello All,
I want to write a script by which I can open up a text file,scan
through it,find the line which contains the word 'test_site',get the
IP address corresponding to that word, replace that IP with another IP
address and then save and close the file.
Are there any commands in Ruby to identify IP address from a string?


Thanks in advance


Regards
Chandrika
 
Reply With Quote
 
 
 
 
Alexey Bovanenko
Guest
Posts: n/a
 
      11-08-2010
[Note: parts of this message were removed to make it a legal post.]

Hi!

I think you can use the following code:

text=IO.read(filename_of_file_with_ip)
text.gsub!(/test_site\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i,"0.0.0.0") #
0.0.0.0 - it's my new ip
f=File.new("outtxt","w")
f.write(text)
f.close

On Mon, Nov 8, 2010 at 3:20 PM, Chandu80 <> wrote:

> Hello All,
> I want to write a script by which I can open up a text file,scan
> through it,find the line which contains the word 'test_site',get the
> IP address corresponding to that word, replace that IP with another IP
> address and then save and close the file.
> Are there any commands in Ruby to identify IP address from a string?
>
>
> Thanks in advance
>
>
> Regards
> Chandrika
>
>



--
With regards,
Alexei Bovanenko

 
Reply With Quote
 
 
 
 
zuerrong
Guest
Posts: n/a
 
      11-08-2010
2010/11/8 Chandu80 <>:
> Hello All,
> I want to write a script by which I can open up a text file,scan
> through it,find the line which contains the word 'test_site',get the
> IP address corresponding to that word, replace that IP with another IP
> address and then save and close the file.
> Are there any commands in Ruby to identify IP address from a string?
>


maybe a regex is useful, though that's not rigorous enough.

irb(main):001:0> s=3D"ip addr:12.34.56.78"
=3D> "ip addr:12.34.56.78"

irb(main):002:0> s.scan /\d+\.\d+\.\d+\.\d+/
=3D> ["12.34.56.78"]


--=20
Kind regards,
=C2=A0 =C2=A0 =C2=A0Zuer (=E7=A5=96=E5=84=BF)

 
Reply With Quote
 
Chandu80
Guest
Posts: n/a
 
      11-09-2010
On Nov 8, 5:48*pm, Alexey Bovanenko <a.bovane...@gmail.com> wrote:
> [Note: *parts of this message were removed to make it a legal post.]
>
> Hi!
>
> I think you can use the following code:
>
> text=IO.read(filename_of_file_with_ip)
> text.gsub!(/test_site\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i,"0.0.0.0") #
> 0.0.0.0 - it's my new ip
> f=File.new("outtxt","w")
> f.write(text)
> f.close
>
>
>
> On Mon, Nov 8, 2010 at 3:20 PM, Chandu80 <chandu.she...@gmail.com> wrote:
> > Hello All,
> > I want to write a script by which I can open up a text file,scan
> > through it,find the line which contains the word 'test_site',get the
> > IP address corresponding to that word, replace that IP with another IP
> > address and then save and close the file.
> > Are there any commands in Ruby to identify IP address from a string?

>
> > Thanks in advance

>
> > Regards
> > Chandrika

>
> --
> With regards,
> Alexei Bovanenko


Hi,
Thanks for the response.It did work.However isn't there a provision to
edit the same file and save it?

Regards
Chandrika
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-09-2010
On 11/09/2010 08:07 AM, Chandu80 wrote:

> Thanks for the response.It did work.However isn't there a provision to
> edit the same file and save it?


ruby -p -i.bak -e 'gsub /\d{1,3}(?:\.\d{1,3}){3}/, "XX.XX.XX.XX"' a_file

If you omit ".bak" there is no backup. Please see "ruby -h".

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: OT: Text editors (was Re: Search and replace text in XML file?) Tim Chase Python 10 08-31-2012 03:56 AM
OT: Text editors (was Re: Search and replace text in XML file?) Chris Angelico Python 9 07-29-2012 05:55 PM
How to identify the ultimate source of a spam type message?... name, address, telephone dsaklad@gnu.org Computer Security 6 05-11-2006 06:47 PM
Identify a file type and open the file Shilpa ASP .Net 1 03-22-2006 11:07 PM
replace text in IFRAME using innerHTML.replace(...) possible? mscir Javascript 0 10-11-2005 11:21 PM



Advertisments