Sam B
Sam B

Reputation: 27618

Perl replace string contents within quotes

I have a large xml file that I am reading line by line.

<Role elemId="id35" island_id="0" role_name="Design" value="True"/>
<Role elemId="id34" island_id="0" role_name="Lead" value="False"/>

What I need to do is whenever I see role_name in the string I need to make it blank.

For example this is my desired output. Notice role_name is blanked out.

<Role elemId="id35" island_id="0" role_name="" value="True"/>
<Role elemId="id34" island_id="0" role_name="" value="False"/>

role_name could have any number or combination of values. so I cannot hardcode for it

Here's my working code:

#!/usr/bin/perl

use strict;
use warnings;

print "\nPerl Starting ... \n\n"; 

while (my $recordLine =<DATA>) 
{
    chomp($recordLine);

    print "$recordLine ...\n";

    if (index($recordLine, "role_name") != -1) 
    {
        #Found role_name tag ... now blank it ... how?
        $recordLine =~ s/role_name="Design"/role_name=""/g; #I need a generic reg experssion here
        print "recordLine: $recordLine ...\n";

    }
}

print "\nPerl End ... \n\n"; 

__DATA__
<Role elemId="id35" island_id="0" role_name="Design" value="True"/>
<Role elemId="id34" island_id="0" role_name="Lead" value="False"/>

Upvotes: 0

Views: 76

Answers (2)

Andrey
Andrey

Reputation: 1818

This should do what you need:

$recordLine =~ s/role_name=".*?"/role_name=""/g;

Upvotes: 0

choroba
choroba

Reputation: 242423

Use an XML-aware tool to process XML. Processing it line by line can lead to broken non-well formed XML.

In Perl, you can use XML::LibXML.

#!/usr/bin/perl
use warnings;
use strict;

use XML::LibXML;

my $dom = 'XML::LibXML'->load_xml(IO => *DATA);

for my $role ($dom->findnodes('//Role[@role_name]')) {
    $role->{role_name} = "";
}

print $dom;

__DATA__
<Root>
<Role elemId="id35" island_id="0" role_name="Design" value="True"/>
<Role elemId="id34" island_id="0" role_name="Lead" value="False"/>
<!--
<Role elemId="id34" island_id="0" role_name="Lead" value="False"/>
-->
</Root>

There's also xsh, a wrapper around XML::LibXML which I happen to maintain. It's interactive and less verbose:

open file.xml ;
for //@role_name set . "" ;
save :b ;

Upvotes: 1

Related Questions