Aanya
Aanya

Reputation: 3

How to replace a string having special characters in xml file by unix commands

I need to replace the below string with null . Its basically an xml file and i want to omit the xmnls part using unix commands.

xmlns:stl="http://www.xxx.yy/1234/stl-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

I have tried using the below commands :

perl -pi -e "s+xmlns:stl="http://www.xxx.yy/1234/stl-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"++" filename.xml

sed 's+xmlns:stl="http://www.xxx.yy/1234/stl-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"++' filename.xml

Didnt work out well. :( Does the experts have any idea on how this should be handled.

PS: Its a continuous line having tags after the string value xmlns:stl="http://www.xxx.yy/1234/stl-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance". So cannot delete the line

Upvotes: 0

Views: 589

Answers (1)

Phil Krylov
Phil Krylov

Reputation: 530

Your sed line works reasonably well, but:

  1. To edit the file in place instead of writing the result to stdout, use sed -i.
  2. To make it predictable, you need to escape regex special characters with \, like this:

    sed -i 's+xmlns:stl="http://www\.xxx\.yy/1234/stl-1\.0" xmlns:xsi="http://www\.w3\.org/2001/XMLSchema-instance"++' filename.xml
    

Upvotes: 1

Related Questions