How to replace value of password tag in xml with blanks when special characters are there?

Hi All,

I am trying to replace the values inside <password> tag in an xml file but it doesn't replace certain passwords:

For eg:

Server/home/sperinc>cat TextXML.txt
<appIds>
        <entry name="AccountXref">
                <type id="ldap">
                        <realm>nam</realm>
                        <id>XXXXXX</id>
                        <password>![CDATAp6t1a7$p4u6f4*k9]></password>
                </type>
        </entry>
        <entry name="WsRmsAcctAdd">
                <type id="XXXXX">
                        <realm>xdsdev</realm>
                        <id>OLSEXTERNALCUSTOMER</id>
                        <password>57aSdfJ8</password>
                </type>
        </entry>

when i run the below script - O/P is below

awk '
BEGIN { FS = "[<|>]" }
{
        if ($2 == "password") {
                sub($3,"")
        }
        print
}
' TextXML.txt > Newfile.txt

The below password is not replaced

<appIds>
        <entry name="AccountXref">
                <type id="ldap">
                        <realm>nam</realm>
                        <id>XXXXXX</id>
                        <password>![CDATAp6t1a7$p4u6f4*k9]></password>

If that's really what the XML looks like:

sed 's/[<]password[>].*[<]\/password[>]/<password><\/password>/' inputfile > outputfile
1 Like

I get the below error

/home/sperinc>sed 's/[<]password[>].*[<]\/password[>]/' TextXML.txt_new > outputfile
sed: 0602-404 Function s/[<]password[>].*[<]\/password[>]/ cannot be parsed.

You're very quick, I was still making sneaky edits. :o Try it now?

1 Like

Awesome! I have been spending last one day trying to fix it via awk....You are the best! Thanks Buddy!

You could do almost the same thing in gsub:

gsub(/<password>.*<\/password>/, "<password><\/password>");

gsub with the awk?

sub in an awk statement, yes. gsub isn't necessary, I realized, just plain sub. So if you're doing 37 operations in awk already, you could just add one more for less performance hit than adding another pipe chain.

1 Like