huangwason
Programmer
Hello, guys, I am thinking about a script that can change element contents of a specified tag in xml file. For example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_GB">
<context>
<name>ezono::ccapp::bup::BiosUpgrade</name>
<source>Failed to update BIOS.</source>
<translation type="unfinished">huang </translation>
</message>
</context>
<context>
<name>ezono::ccapp::bup::BiosUpgrade</name>
<source>Failed to update BIOS.</source>
<translation> here is a test
</translation>
</context>
</TS>
I will change the element contents between the tag <translation> from lower case to upper case.
I intend to use Sed
sed -e '/<translation*>/ s/\(.*\)/\U\1/' -e '/<\/translation>/,$ s/\(.*\)/\U\1/' input.xml
some problems:
1) it changes the tag to upper case as well(<TRANSLATION>), how to figure it out only for those texts between tags
2) I use range by pattern of sed here
sed '/start/,/stop/ s/regular express/replaced string/'
the "start" partern I used is "<translation*>" which can handle tag begin with <translation>, can not handle tag such as <translation type="unfinished">
3) the above script is line oriented, that means it work if <translation> and </translation> in one line, but sometime, the begin and end of a tag are not always in one line.
How to solve the problem? Is Sed really good to solve this problem?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_GB">
<context>
<name>ezono::ccapp::bup::BiosUpgrade</name>
<source>Failed to update BIOS.</source>
<translation type="unfinished">huang </translation>
</message>
</context>
<context>
<name>ezono::ccapp::bup::BiosUpgrade</name>
<source>Failed to update BIOS.</source>
<translation> here is a test
</translation>
</context>
</TS>
I will change the element contents between the tag <translation> from lower case to upper case.
I intend to use Sed
sed -e '/<translation*>/ s/\(.*\)/\U\1/' -e '/<\/translation>/,$ s/\(.*\)/\U\1/' input.xml
some problems:
1) it changes the tag to upper case as well(<TRANSLATION>), how to figure it out only for those texts between tags
2) I use range by pattern of sed here
sed '/start/,/stop/ s/regular express/replaced string/'
the "start" partern I used is "<translation*>" which can handle tag begin with <translation>, can not handle tag such as <translation type="unfinished">
3) the above script is line oriented, that means it work if <translation> and </translation> in one line, but sometime, the begin and end of a tag are not always in one line.
How to solve the problem? Is Sed really good to solve this problem?