解析unix中的xml文件

时间:2013-05-23 10:31:17

标签: xml parsing shell unix

我有这个xml文件:

<?xml version="1.0"?>
<RunInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2">
  <Run Id="130514_M01481_0011_000000000-A3F7W" Number="10">
    <Flowcell>000000000-A3F7W</Flowcell>
    <Instrument>M01481</Instrument>
    <Date>130514</Date>
    <Reads>
      <Read NumCycles="151" Number="1" IsIndexedRead="N" />
      <Read NumCycles="8" Number="2" IsIndexedRead="Y" />
      <Read NumCycles="8" Number="3" IsIndexedRead="Y" />
      <Read NumCycles="151" Number="4" IsIndexedRead="N" />
    </Reads>
    <FlowcellLayout LaneCount="1" SurfaceCount="2" SwathCount="1" TileCount="14" />
  </Run>
</RunInfo>

我需要编写一个shell脚本,循环遍历'Reads',并为IsIndexedRead =“N”和IsIndexedRead =“Y”提取NumCycles。我在这个命令中使用了xmllint:

xmllint --xpath 'string(//Read/@NumCycles)' RunInfo.xml

这给了151,但我需要循环阅读。谁知道更好的方法? 感谢

1 个答案:

答案 0 :(得分:3)

xmllint --xpath '//Read[@IsIndexedRead = "Y"]/@NumCycles' RunInfo.xml | grep -o '[0-9]\+'

8
8

xmllint --xpath '//Read[@IsIndexedRead = "N"]/@NumCycles' RunInfo.xml | grep -o '[0-9]\+'

151
151