substitution on a XML file shell script snippet

One more Shell script snippet.

Scenario: I have several directories, each one with a file called strings.xml. The problem with those files is that they are like:

<string desc="whatever">"text"</string>

I’d like to remove those double quotation marks inside the string element.

I’m already in the directory which contains all others directories with the xml files.

Solution: A script that for each directory, cat it xml file and substitute the occurrences of >” for > and “< for <.

for file in `ls`; do cat $file/strings.xml| sed 's/>"/>/g'| sed 's/"</</g' > $file/strings.xml; done

Warning: Shell script and regular expressions are not the correct way to parse XML because there are special cases where you can not handle properly. But in simple cases like this one it can be useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>