如何grep我的xml文件并保存输出?

如何grep我的xml文件并保存输出?,xml,shell,xml-parsing,grep,Xml,Shell,Xml Parsing,Grep,我只是给出了巨大xml文件的一部分 <caldata chopper="on" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)"> <c0 unit="V">0.00000000e+00</c0> <c1 unit="Hz">4.00000000e+04</c1> <c2 unit="V/(nT*Hz)">8.35950000e-0

我只是给出了巨大xml文件的一部分

   <caldata chopper="on" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
      <c0 unit="V">0.00000000e+00</c0>
      <c1 unit="Hz">4.00000000e+04</c1>
      <c2 unit="V/(nT*Hz)">8.35950000e-06</c2>
      <c3 unit="deg">-1.17930000e+02</c3>
    </caldata>
    <caldata chopper="on" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
      <c0 unit="V">0.00000000e+00</c0>
      <c1 unit="Hz">5.55810000e+04</c1>
      <c2 unit="V/(nT*Hz)">4.43400000e-06</c2>
      <c3 unit="deg">-1.58280000e+02</c3>
    </caldata>
    <caldata chopper="on" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
      <c0 unit="V">0.00000000e+00</c0>
      <c1 unit="Hz">6.00000000e+04</c1>
      <c2 unit="V/(nT*Hz)">3.63180000e-06</c2>
      <c3 unit="deg">-1.67340000e+02</c3>
    </caldata>
    <caldata chopper="off" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
      <c0 unit="V">0.00000000e+00</c0>
      <c1 unit="Hz">4.00000000e-01</c1>
      <c2 unit="V/(nT*Hz)">1.07140000e-02</c2>
      <c3 unit="deg">1.48080000e+02</c3>
    </caldata>
    <caldata chopper="off" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
      <c0 unit="V">0.00000000e+00</c0>
      <c1 unit="Hz">5.55800000e-01</c1>
      <c2 unit="V/(nT*Hz)">1.33250000e-02</c2>
      <c3 unit="deg">1.39110000e+02</c3>
    </caldata>
    <caldata chopper="off" gain_1="0" gain_2="0" gain_3="0" impedance="(0,0)">
      <c0 unit="V">0.00000000e+00</c0>
      <c1 unit="Hz">7.72300000e-01</c1>
      <c2 unit="V/(nT*Hz)">1.57750000e-02</c2>
      <c3 unit="deg">1.29560000e+02</c3>

0.00000000 E+00
4.00000000 E+04
8.35950000e-06
-1.17930000e+02
0.00000000 E+00
5.55810000e+04
4.43400000e-06
-1.58280000e+02
0.00000000 E+00
6.00000000 E+04
3.63180000e-06
-1.673400000E+02
0.00000000 E+00
4.00000000e-01
1.07140000e-02
1.480800000+02
0.00000000 E+00
5.55800000e-01
1.33250000e-02
1.39110000e+02
0.00000000 E+00
7.72300000e-01
1.57750000e-02
1.29560000e+02
我试过这样做

grep '<c1 unit="Hz"' *.xml | cut -f2 -d">"|cut -f1 -d"<"
grep'这样做可以:

cat file.xml | awk '/chopper="off"/,/calcdata/{print}' | grep 'unit="Hz"' | sed 's/^.*">//;s/<.*$//'

cat file.xml | awk'/chopper=“off”//calcdata/{print}| grep'unit=“Hz”| sed's/^.*>/;s/解决方案是使用xml grep,例如。我自己在我的机器上尝试过,得到了以下结果:

$ xgrep -t -x '//caldata[@chopper="off"]/c1[@unit="Hz"]/text()' test.xml 
4.00000000e-01
5.55800000e-01
7.72300000e-01
秘密在于XPath表达式:

  • //caldata[@chopper=“off”]
    -使用
    chopper
    属性等于
    off
    的所有
    caldata
    元素
  • c1[@unit=“Hz”]
    -从该
    caldata
    元素中,获取
    c1
    元素,其
    unit
    属性等于
    Hz
  • text()
    -从那些
    c1
    元素中,仅获取文本内容
要将其保存到输出文件,只需使用shell中的
重定向程序。我们只需在命令后添加它,然后添加文件名即可获得输出:

$ xgrep -t -x '//caldata[@chopper="off"]/c1[@unit="Hz"]/text()' test.xml  > output.xml
$ cat output.xml 
4.00000000e-01
5.55800000e-01
7.72300000e-01

我不知道您是否可以使用像这样的自定义工具,当然,但如果您可以,它可能是您最好的解决方案。

因为您使用的是grep,我将假设一些*nix风格和命令行类型的解决方案

在这种情况下,您可能希望看到类似zorba的东西,它将使用xquery解析您的输入文档并输出您想要的部分

如果数据中的容器元素是foo,那么xquery将包含:

for $c in /foo/caldata
return if ($c/@chopper="on")
then $c else ""

使用XML/HTML解析器(xmllint,xmlstarlet…)。你的文件有多大?100s mb或gbs?@vtd XML author 96,5kB,一点也不大!Obrigado,nordestino!如何将输出保存到文本文件?De nada;)我编辑了答案,解释了如何将输出保存到文件中。