?
Solved

Search and replace between patterns.

Posted on 2006-05-18
2
Medium Priority
?
220 Views
Last Modified: 2010-04-17
I have the following xml on which I'm trying to replace the ip address of the employee Mark. I have written the script as
====================================================
#!/usr/bin/perl -w
use warnings;
use strict;

undef $/;
open FILEHD, 'test.xml' or die "Cannot open the file\n";
my $fileVar = <FILEHD>;
$/="\n";

my $emp="Mark";
my $prop="Emp.IP";
my $value="1.1.1.1";

$fileVar =~  s/(\b$emp.*?<$prop>)\b\S*.*\b(<.*Employee>\b)/$1$value$2/s;

print $fileVar;
=========================================================
But the ip address does not get replaced. I have tried different combinations but none works. Can you please suggest how it can be achieved.


==========================================================
        <Employees>
                <Employee id="17">
                        <Employee.Name>Mark</Employee.Name>
                        <Emp>
                                <Emp.IP>10.32.4.27</Emp.IP>
                                <Emp.Sal>5890</Emp.Sal>
                        </Emp>
                        <Last>
                                <Last.Community>public</Last.Community>
                                <Last.Sal>5896</Last.Sal>
                        </Last>
                </Employee>
                <Employee id="16">
                        <Employee.Name>Smith</Employee.Name>
                        <Emp>
                                <Emp.IP>10.32.4.25</Emp.IP>
                                <Emp.Sal>5890</Emp.Sal>
                        </Emp>
                        <Last>
                                <Last.Community>public</Last.Community>
                                <Last.Sal>5896</Last.Sal>
                </Employee>
           </Employees>
0
Comment
Question by:rohit_kapil
  • 2
2 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 16713431
#!/usr/bin/perl -w
use warnings;
use strict;

undef $/;
open FILEHD, 'test.xml' or die "Cannot open the file\n";
my $fileVar = <FILEHD>;
$/="\n";

my $emp="Mark";
my $prop="Emp.IP";
my $value="1.1.1.1";

$fileVar =~ s/\b($emp.*?<$prop>)\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(.*$prop\b)/$1$value$3/s;

print $fileVar;
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 150 total points
ID: 16713442
$fileVar =~ s/\b($emp.*?<$prop>)\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(.*?$prop\b)/$1$value$3/s;

would probably be better
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question