Bash将子节点插入XML文件
发布时间:2020-12-15 18:58:37 所属栏目:安全 来源:网络整理
导读:我试图使用bash编辑配置文件.我的文件看起来像这样: configuration property name/name value/value /property property name/name value/value /property/configuration 我想添加另外两个 property阻止文件.由于所有属性标记都包含在配置标记内,因此文件将
我试图使用bash编辑配置文件.我的文件看起来像这样:
<configuration> <property> <name></name> <value></value> </property> <property> <name></name> <value></value> </property> </configuration> 我想添加另外两个< property>阻止文件.由于所有属性标记都包含在配置标记内,因此文件将如下所示: <configuration> <property> <name></name> <value></value> </property> <property> <name></name> <value></value> </property> <property> <name></name> <value></value> </property> </configuration> 我遇到了this post and followed the accepted answer,但是我的文件没有附加任何内容,我尝试追加的xml块是“echo-ed”作为单行字符串. file=/path/to/file/oozie-site.xml content="<property>n<name></name>n<value></value>n</property>n<property>n<name></name>n<value></value>n</property>" echo $content C=$(echo $content | sed 's/////g') sed "/</configuration>/ s/.*/${C}n&/" $file
使用xmlstarlet:
xmlstarlet ed --omit-decl -s '//configuration' -t elem -n "property" -s '//configuration/property[last()]' -t elem -n "name" -s '//configuration/property[last()]' -t elem -n "value" file.xml 输出: <configuration> <property> <name/> <value/> </property> <property> <name/> <value/> </property> <property> <name/> <value/> </property> </configuration>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |