CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Replacing an existing IP address with a new IP address

 



ranjan.kbr
New User

Nov 6, 2008, 2:10 AM

Post #1 of 4 (3606 views)
Replacing an existing IP address with a new IP address Can't Post

I want to substitute the existing IP address with a new IP address and recontruct the original string using perl .

My Source File is as follows

text.txt
-----------

RequestURI := "sip:192.1.1.1";
From := "sip:192.1.2.3";

i need to write a perl program which will ask for the RequestURI and From address and replace the value into the text.txt file

output
--------
Enter the Request URI:=192.10.2.3
Enter From :=185.60.2.1

test.txt
----------
RequestURI := "sip:192.10.2.3";
From := "sip:185.60.2.1";

I have written the code as follows:

#! /usr/bin/perl

my $data_file = '/home/remoteuser/ranjan/text.txt';
open DATA, "$data_file" or die "can't open $data_file $!";

print "Enter the RequestURI:\n";
$RequestURI=<STDIN>;

my @array_of_data = <DATA>;

foreach my $line (@array_of_data)
{
if ($line =~ m/RequestURI/)
{
$line =~ s/sip:.*;/sip:$RequestURI/;
open DATAOUT, ">$data_file" or die "can't open $data_file $!";
print DATAOUT "$line";

}
}
The code is not able to provide me the exact out put.
Need some help to sort it out.

Quote

Code

In Reply To


PGScooter
stranger

Nov 8, 2008, 11:04 PM

Post #2 of 4 (3518 views)
Re: [ranjan.kbr] Replacing an existing IP address with a new IP address [In reply to] Can't Post

Hi Ranjan,

what error do you get? What is the output that you do get?
The more you teach me, the more I learn. The more I learn, the more I teach.


ranjan.kbr
New User

Nov 9, 2008, 11:58 PM

Post #3 of 4 (3487 views)
Re: [PGScooter] Replacing an existing IP address with a new IP address [In reply to] Can't Post

Hi PGScooter,

I am not getting any error message. i am not getting the exact out put.

Suppose i will provide the input IP adress as : 123.45.6.7



Desired Output : RequestURI := "SIP:123.45.6.7";

but i am getting the output as



RequestURI :="SIP:123.45.6.7

("; ) Thease two characters are missing.

Here i need the proper replacement . i.e IP only to be replaced. it won't touch any other things.


FishMonger
Veteran / Moderator

Nov 10, 2008, 6:34 AM

Post #4 of 4 (3468 views)
Re: [ranjan.kbr] Replacing an existing IP address with a new IP address [In reply to] Can't Post


Code
if ( $line =~ s/(RequestURI\s*:=\s*"sip:).+/$1$RequestURI";/ ) 
{
open my $DATAOUT, '>', $data_file or die "can't open $data_file $!";
print $DATAOUT $line;
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives