如何提取两个标签之间的所有文本,包括标签,但不是整行

时间:2018-03-18 14:55:12

标签: bash unix

我是unix的新手。我想提取两个关键字之间的所有文本,包括那些关键字,但不提取输出中的任何其他文本,即使文本属于关键字的同一行。

以下是示例文件:test.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <spml:batchResponse executionTime="6066" result="success" xmlns:spml="urn:si:names:prov:gw:SPML:2:0" xmlns:customer="urn:si:names:prov:gw:HOME:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><version>v10</version><response executionTime="45" result="success" xsi:type="spml:ModifyResponse" requestID="41f01d9f:1616237351f:-761e" timestamp="2018/02/05 12:40:57,767 +0600">
  <version>v10</version>
  <objectclass>customer</objectclass>
  <identifier alias="method">1234</identifier>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="source/testInject" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:METHODNAME">
  <methodInfo>FIRE</methodInfo>
  </valueObject>
  </modification>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="node" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:NODE">
  <iserv>200</iserv>
  </valueObject>
  </modification>
  </response><response executionTime="95" result="success" xsi:type="spml:ModifyResponse" requestID="41f01d9f:1616237351f:-7696" timestamp="2018/02/05 12:42:57,767 +0600">
  <version>v10</version>
  <objectclass>customer</objectclass>
  <identifier alias="method">4321</identifier>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="source/testInject" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:METHODNAME">
  <methodInfo>FIRE</methodInfo>
  </valueObject>
  </modification>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="node" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:NODE">
  <iserv>200</iserv>
  </valueObject>
  </modification>
  </response><response executionTime="65" result="success" xsi:type="spml:ModifyResponse" requestID="41f01d9f:16162373512:-9621" timestamp="2018/02/05 12:39:20,767 +0600">
  <version>v10</version>
  <objectclass>customer</objectclass>
  <identifier alias="method">3214</identifier>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="source/testInject" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:METHODNAME">
  <methodInfo>FIRE</methodInfo>
  </valueObject>
  </modification>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="node" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:NODE">
  <iserv>200</iserv>
  </valueObject>
  </modification>
  </response></spml:batchResponse>

以下是我的预期输出,我希望在其中提取所有文本

  <response executionTime="45" result="success" xsi:type="spml:ModifyResponse" requestID="41f01d9f:1616237351f:-761e" timestamp="2018/02/05 12:40:57,767 +0600">
  <version>v10</version>
  <objectclass>customer</objectclass>
  <identifier alias="method">1234</identifier>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="source/testInject" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:METHODNAME">
  <methodInfo>FIRE</methodInfo>
  </valueObject>
  </modification>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="node" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:NODE">
  <iserv>200</iserv>
  </valueObject>
  </modification>
  </response>
  <response executionTime="95" result="success" xsi:type="spml:ModifyResponse" requestID="41f01d9f:1616237351f:-7696" timestamp="2018/02/05 12:42:57,767 +0600">
  <version>v10</version>
  <objectclass>customer</objectclass>
  <identifier alias="method">4321</identifier>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="source/testInject" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:METHODNAME">
  <methodInfo>FIRE</methodInfo>
  </valueObject>
  </modification>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="node" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:NODE">
  <iserv>200</iserv>
  </valueObject>
  </modification>
  </response>
  <response executionTime="65" result="success" xsi:type="spml:ModifyResponse" requestID="41f01d9f:16162373512:-9621" timestamp="2018/02/05 12:39:20,767 +0600">
  <version>v10</version>
  <objectclass>customer</objectclass>
  <identifier alias="method">3214</identifier>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="source/testInject" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:METHODNAME">
  <methodInfo>FIRE</methodInfo>
  </valueObject>
  </modification>
  <modification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scope="uniqueTypeMapping" name="node" operation="modify">
  <valueObject xmlns:ns2="urn:si:names:prov:gw:customer:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:NODE">
  <iserv>200</iserv>
  </valueObject>
  </modification>
  </response>

1 个答案:

答案 0 :(得分:0)

安装xmlstarlet工具(如果未安装)

运行命令:

xmlstarlet sel -t -c '//response' -n input.xml

或与xmllint工具相同:

xmllint --xpath '//response' input.xml