创建bash脚本来检索XML文件中的数据 [英] Creating Bash Script to retrieve data from XML file
本文介绍了创建bash脚本来检索XML文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的XML文件:
I have an XML File like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
<Job id="1" name="John/>
<Job id="2" name="Zack"/>
<Job id="3" name="Bob"/>
</List>
我想创建一个bash脚本在那里它从一个特定的名字的ID号。例如,请求约翰将出来把1.有没有什么办法可以做到这一点?
I want to create a bash script where it pulls the id number from a specific name. For example, requesting John will out put 1. Is there any way I can do this?
推荐答案
在解析XML文件时,使用一种工具,它的理解的XML。你可以使用 xmlstarlet
:
When parsing XML files, use a tool that understands xml. You could use xmlstarlet
:
例如,他说:
xmlstarlet sel -t -v "/List/Job[@name=\"John\"]/@id" file.xml
将产生
1
BTW,没有很好地形成的输入。你有一个失踪报价
BTW, your input isn't well-formed. You have a missing quote in
<Job id="1" name="John/>
这应该是
<Job id="1" name="John"/>
这篇关于创建bash脚本来检索XML文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文