bash 用于更改 xml 元素的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18212285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
shell script for the changing the element of xml
提问by Zeeshan
I want to change the value of my xml "abc.xml" element to the value that is stored in the variable $value i.e
我想将我的 xml "abc.xml" 元素的值更改为存储在变量 $value 中的值,即
$value = 'abc';
$value = 'abc';
<annotation>
<filename>img_000001016592.png</filename>
<folder>Rec_20121219_171905</folder>
<source>
<sourceImage>The MIT-CSAIL database of objects and scenes</sourceImage>
<sourceAnnotation>LabelMe Webtool</sourceAnnotation>
</source>
<imagesize>
<nrows>481</nrows>
<ncols>640</ncols>
</imagesize>
</annotation>
The shell script is required which has one variable and it contains the value in the variable and then change the value of the element filename of the abc.xml to the value in variable.
需要shell脚本,它有一个变量,它包含变量中的值,然后将abc.xml的元素文件名的值更改为变量中的值。
回答by konsolebox
Perhaps you mean to use sed.
也许您的意思是使用 sed。
value='abc'
sed -i "s|abc.txt|$value|g" abc.xml
You would have to run that in a shell, or as a shell script with the header #!/bin/sh
.
您必须在 shell 中运行它,或者作为带有 header 的 shell 脚本运行#!/bin/sh
。
---- Update ----
- - 更新 - -
#!/bin/sh
value="something"
sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|${value}|" abc.xml
Add g
to the sed command if you need to replace multiple instances in one line.
g
如果您需要在一行中替换多个实例,请添加到 sed 命令。
sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|${value}|g" abc.xml