equick has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to write code to replace the folling text in an xml file

<!-- <control_link_address>push1.mycompany.com</control_link_address> -->
with
<!-- <control_link_address>push1.mycompany.com</control_link_address> --> <control_link_address>mdp.travel.co.uk</control_link_address>
using perl -npe 's/find/replace/g' -i $file

Here's my code but this doesn't work. :

$find=q[[<control_link_address>push1\.mycompany\.com<\/control +_link_address>\s*\n\s*-->]]; $replace=q[$find\n <control_link_address>mdp.travel.co.uk< +\/control_link_address>\n]; $cmd=qq[perl -npe "s/$find/$replace/g" -i /tmp/ls.conf];

Could someone help me out please?

Thanks, Ed.

Replies are listed 'Best First'.
Re: perl regex for xml
by Corion (Patriarch) on Jun 18, 2010 at 13:37 UTC

    This code:

    q[[...]]

    likely does not do what you think it does. Print $find to see what it actually contains.

    Also, note that shell quoting will get in the way of proper quoting your replacement strings. I recommend doing the actual replacement also within your main Perl process, using for example Tie::File for in-place substitution.

Re: perl regex for xml
by graff (Chancellor) on Jun 19, 2010 at 02:00 UTC
    This is a case where there really is no good reason to run a shell command from within your perl script -- especially when the shell command is only going to run another perl script, as a one-liner. That's just way too complicated.

    The easy way is to slurp the whole file into a scalar variable, do your regex substitution, and write the string back out to a file. (I'd recommend keeping the original as-is and saving the altered data to a different file, so you can be sure it worked before obliterating the original data.)

    Here's one way to do your task:

    #!/usr/bin/perl use strict; my $find = '(<!--\s*<control_link_address>push1\.mycompany\.com</contr +ol_link_address>\s*-->)'; my $rplc = '$1\n <control_link_address>mdp.travel.co.uk</control_link_ +address>\n'; open( IN, "<", "/tmp/ls.conf" ) or die "/tmp/ls.conf: $!"; $/ = undef; $_ = <IN>; eval "s{$find}{$rplc}"; open( OUT, ">", "/tmp/ls.conf.new" ) or die "/tmp/ls.conf.new: $!"; print OUT;
    I'm using a string eval so that the "$1" will behave as intended. I'm sure there are better ways... (Parsing, maybe?)

      Hi graff,

      Thanks for the reply, that's a really clever bit of coding! I tried it out and it worked perfectly.

      Ed.