【问题标题】:Batch to read partial xml file批量读取部分xml文件
【发布时间】:2012-10-19 14:41:37
【问题描述】:

我正在尝试制作一个批处理文件以从文件中提取数据并将其设置为变量。

棘手的部分是我需要读取一个 XML 文件,而我只需要下一行引号之间的数据...

narrative="I only need this text here"

该行中的文本还可以包含空格、方括号、斜线、破折号和冒号。

示例 XML 文件

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
    <cadcall>
        <call callnumber="123456" jurisdiction="abcd" department="dept 1" complaint="cost" priority="M" calltencode="" callername="Persons Name" phonenumber="Cell Number HERE" narrative="[10/02/2012 14:56:27 : pos9 : PERSON] Fairly long narrative here describing issue of dispatch, but sometimes these can be short." alarmtype="" ocanumber="0000000000" disposition1="TRAN" />
    </cadcall>

【问题讨论】:

    标签: shell scripting batch-file command


    【解决方案1】:

    执行此操作的正确工具是来自libxmlxmllint,请提供更完整的XML 示例,我将告诉您如何在您的XML 上使用Xpath 请求。

    编辑

    这里有一个使用 Xpath 的解决方案(有一点技巧:contains):

    xmllint --xpath '/cadcall/call/@narrative[contains(.,'.')]' file.xml
    

    【讨论】:

    • 这里是一个完整的例子。 -
    • 当我运行这个时,我得到一个 XPath 错误:无效的表达式。它显示了一个指向时期的carret; (x,'.')]'
    【解决方案2】:

    没有看到完整的输入,只是基于您的示例行。 grep 适合你。

    kent$  echo 'narrative="I only need this text here"'|grep -Po '(?<=narrative=")[^"]*'
    I only need this text here
    

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 2016-05-25
      • 2023-04-07
      • 2014-03-10
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多