xml - 用于更改 xml 元素的 shell 脚本

标签 xml bash shell

<分区>

我想将我的 xml“abc.xml”元素的值更改为存储在变量 $value 中的值,即

$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>

需要一个shell脚本,它有一个变量,它包含变量中的值,然后将abc.xml的元素文件名的值更改为变量中的值。

最佳答案

也许你的意思是使用 sed。

value='abc'
sed -i "s|abc.txt|$value|g" abc.xml

您必须在 shell 中运行它,或者作为带有 header #!/bin/sh 的 shell 脚本运行。

----更新----

#!/bin/sh
value="something"
sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|\1${value}\2|" abc.xml

如果需要在一行中替换多个实例,请将 g 添加到 sed 命令。

sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|\1${value}\2|g" abc.xml

关于xml - 用于更改 xml 元素的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18212285/

相关文章:

linux - 仅在特定目录中使用 find 命令查找文件,即使该文件存在于其他目录中

git - 为什么我的本地 git 标签从 "hello"变为 "hello-1-48281"

shell - 使用 shell 编写脚本 - Asterisk

linux - Shell 脚本支持——不同 URL 上的多个 nslookups

java - XStream 序列化集合

java - 在java中合并两个XML文件并生成单个xml

python - pyth 错误 [rtf 到 xml/html]

bash - 使用内容创建 shell 脚本

linux - 如何在使用单行 UNIX 命令创建文件时为其提供权限

android - 布局不会随 EditText 展开