Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建Bash脚本以从XML文件检索数据_Xml_Linux_Bash - Fatal编程技术网

创建Bash脚本以从XML文件检索数据

创建Bash脚本以从XML文件检索数据,xml,linux,bash,Xml,Linux,Bash,我有这样一个XML文件: <?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> 试试像这样的东西 #!/bin/bash name="$1" while re

我有这样一个XML文件:

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

试试像这样的东西

#!/bin/bash

name="$1"
while read -r line; do
  [[ $line =~ "name=\"$name\"" ]] && [[ $line =~ "Job id=\""([^\"]+) ]] &&  echo "${BASH_REMATCH[1]}"
done < file 

解析XML文件时,请使用理解XML的工具。您可以使用:

比如说:

xmlstarlet sel -t -v "/List/Job[@name=\"John\"]/@id" file.xml
会产生

1
顺便说一句,您的输入格式不正确。您在中缺少报价

<Job id="1" name="John/>
<Job id="1" name="John"/>

<Job id="1" name="John"/>