bash - shell脚本将标签插入到xml中?

标签 bash shell

我有一个 xml

<Data>  

<employee>
  <name>emp1</name>
  <id>1</id>
</employee>

</Data>

假设我有一个模板,可以在其中更改值并添加到以前的 xml

<employee>
 <name>##name##</name> 
 <id>##empid##</id>
<employee>

我用##name##和##empid##替换原始值并放入原始xml 名称=emp2 开斋节=2 所以在那之后它一定是

<Data>  
<employee>
  <name>emp1</name>
  <id>1</id>
</employee>
<employee>
  <name>emp2</name>
  <id>2</id>
</employee> 

</Data>

我尝试让行号插入标签

line=$(cat  data.xml | grep -n '</Data>' | grep -o '^[0-9]*')

尝试使用将文本放入原始 xml

sed $line\ i\<text to be insert in original xml>

但它给出了 xml 内容的一些错误,将给予帮助,因为我是 shell 脚本的新手

最佳答案

您可以使用 xmlstarlet 更新您的 template.xml(格式错误)

$ cat template.xml 
<employee>
 <name>##name##</name> 
 <id>##empid##</id>
</employee>

$ xmlstarlet ed -u //name -v emp2 -u //id -v 2 template.xml 
<?xml version="1.0"?>
<employee>
  <name>emp2</name>
  <id>2</id>
</employee>

关于bash - shell脚本将标签插入到xml中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11328536/

相关文章:

bash - 以 bash 友好的方式格式化 Go 日期

linux - 单个 bash 行中的嵌套命令

linux - 如何将 wget 输出重命名为文件中的名称(bash)

linux - 如何在 shell 脚本中正确地进行字符串比较?

linux - "telnet"命令在 shell 脚本中执行时失败并显示 "bad port",但在命令提示符下有效

string - 在bash中检查文件是否包含多行字符串

arrays - 如何移植使用 "${@:2}"?

linux - 如何用shell脚本读取变量中的多个值

linux - 使用不同文件夹中的文件将某些文件信息输出到文本文件

php - Rancher 操作系统 : generate data in one container and set as environment variable in another container