1825886 Members
3165 Online
109689 Solutions
New Discussion

Perl search and replace

 
SOLVED
Go to solution
HPP
Regular Advisor

Perl search and replace

Hi,
I have written perl script, which prompts for network parameters and modifies copule of file, one of them is tnanames.ora. I am trying to search and replace in tnsnames.ora:

Current values:

(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.32.100)(PORT = 1521))

Search and replace with perl script:
$IN =~ s/\(^ .*\)\(ADDRESS.*\)\(HOST.*\)\(PORT.*\)/\1\2HOST \= $IP_ADDRESS\)\(\4/g;

Perl script does nothing. But if I use the same search and replace within vi it works.

:1,$s/\(^ .*\)\(ADDRESS.*\)\(HOST.*\)\(PORT.*\)/\1\2HOST \= $IP_ADDRESS\)\(\4/g;

Am i missing anything with perl script?

thanks in advance.


Be Teachable
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl search and replace

1. use $1, $2, $3 in the replacement pattern
2. perl's re's capture with (), not with \(\)
3. the start of string anchor cannot match after a literal (

so you regex does not capture anything

Enjoy, H.Merijn
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Perl search and replace

Like Procura stated, change your line to:

$IN =~ s/(^.*)(ADDRESS.*)(HOST.*)(PORT.*)/$1$2HOST = $IP_ADDRESS)($4))/g;

You also had a SPACE after the carat (^), which will only match if there is a SPACE in front of "(ADDRESS".

live free or die
harry
Live Free or Die
HPP
Regular Advisor

Re: Perl search and replace

Merijin,
Thanks for the quick response. I made changes to script and its working fine.

Thanks again.
PAd
Be Teachable
HPP
Regular Advisor

Re: Perl search and replace

Thanks harry.
Be Teachable