gpt4 book ai didi

bash - 将 xml 解析为并替换特定标签 shell 脚本

转载 作者:行者123 更新时间:2023-11-29 09:01:35 28 4
gpt4 key购买 nike

对于下面的xml,我需要替换<studentStatus>对于 <studentName>CLASSA</studentName><studentStatus>failed</studentStatus> .

<studentFile>
<student>
<studentName>CLASSA</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Juniour</studentType>
<studentStatus>Completed</studentStatus>
<studentMsg/>
</studentAction>
<studentAction>
<studentType>HighSchool</studentType>
<studentStatus>Completed</studentStatus>
<studentMsg/>
</studentAction>
</studentActions>
</student>
<student>
<studentName>CLASSB</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Senior</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
<studentAction>
<studentType>Middle</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
</studentActions>
</student>
</studentFile>

到目前为止我得到了什么,

xmllint -xpath "/studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType="Juniour"]/studentStatus" myxml.xml

现在我得到学生的状态为 Completed,现在这个值应该更改为 Failed。仅适用于 <studentType>Juniour</studentType> .我应该如何编辑 xml 才能将其作为 ,

<studentFile>
<student>
<studentName>CLASSA</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Juniour</studentType>
<studentStatus>Failed</studentStatus>
<studentMsg/>
</studentAction>
<studentAction>
<studentType>HighSchool</studentType>
<studentStatus>Completed</studentStatus>
<studentMsg/>
</studentAction>
</studentActions>
</student>
<student>
<studentName>CLASSB</studentName>
<studentStatus>Success</studentStatus>
<studentActions>
<studentAction>
<studentType>Senior</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
<studentAction>
<studentType>Middle</studentType>
<studentStatus>Completed</studentStatus>
</studentAction>
</studentActions>
</student>
</studentFile>

这可以使用 sed 来完成吗?我知道有像 xsltproc 这样的工具,但不确定它是否安装在我们集群的所有节点中。

任何帮助将不胜感激。提前致谢!

最佳答案

使用 file.xml 中的 xmllint 更新值:

xmllint --shell file.xml << EOF
cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus
set failed
save
EOF

或没有here document :

echo -e "cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus\nset failed\nsave" | xmllint --shell file.xml

更新:在变量中使用 bash 和 XML:

xml=$(xmllint --shell <(echo "$xml") << EOF
cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus
set failed
save -
EOF
)

或者没有这里文档:

xml=$(echo -e "cd /studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType='Juniour']/studentStatus\nset failed\nsave -" | xmllint --shell <(echo "$xml"))

关于bash - 将 xml 解析为并替换特定标签 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com