• shell解析xml文档


    需要解析的源文件:

    /tmp/cameralist :

    <?xml version="1.0" encoding="UTF-8"?>
    <cameralist>
      <camera brand="virtual camera" ip="192.168.1.17" mac="000000000000" hasAudio="no" hasVideo="yes">
        <url codec="m4v">rtsp://192.168.1.17:5554/ipcam1.m4v</url>
      </camera>
      <camera brand="virtual camera" ip="192.168.1.17" mac="000000000000" hasAudio="no" hasVideo="yes">
        <url codec="m4v">rtsp://192.168.1.17:5554/ipcam2.m4v</url>
      </camera>
      <camera brand="virtual camera" ip="192.168.1.17" mac="000000000000" hasAudio="no" hasVideo="yes">
        <url codec="m4v">rtsp://192.168.1.17:5554/ipcam3.m4v</url>
      </camera>
      <camera brand="virtual camera" ip="192.168.1.17" mac="000000000000" hasAudio="no" hasVideo="yes">
        <url codec="m4v">rtsp://192.168.1.17:5554/ipcam4.m4v</url>
      </camera>
      <camera brand="virtual camera" ip="192.168.1.17" mac="000000000000" hasAudio="no" hasVideo="yes">
        <url codec="m4v">rtsp://192.168.1.17:5554/ipcam5.m4v</url>
      </camera>
      <camera brand="virtual camera" ip="192.168.1.17" mac="000000000000" hasAudio="no" hasVideo="yes">
        <url codec="m4v">rtsp://192.168.1.17:5554/ipcam6.m4v</url>
      </camera>
    </cameralist>

    1.取得brand字段的内容:(value中可以有空格)

    grep -Eo "brand=/".+/"" /tmp/cameralist | awk -F/" '{print $2}'

    结果为

    virtual camera
    virtual camera
    virtual camera
    virtual camera
    virtual camera
    virtual camera
    AXIS M1011-W

    2..取得brand字段的内容:(value不可以有空格,有空格则返回值为空,如果没有空格返回值正常)

    grep -Eo "brand=/"[^ ]+/"" /tmp/cameralist | awk -F/" '{print $2}'

    结果为空

    如果grep -Eo "ip=/"[^ ]+/"" /tmp/cameralist | awk -F/" '{print $2}'

    192.168.1.17
    192.168.1.17
    192.168.1.17
    192.168.1.17
    192.168.1.17
    192.168.1.17
    192.168.1.103

    3.取得xml文档中含有virtual的行数

    v_num=`awk -v RS='virtual' 'END{print NR-1}' /tmp/cameralist` 

  • 相关阅读:
    HappyLeetcode45:Divide Two Integers
    HappyLeetcode44:Plus One
    HappyLeetcode43:Symmetric Tree
    HappyLeetcode42:Intersection of Two Linked Lists
    特征向量中心性(从文献中摘抄)
    二叉树(普通无规律)
    链式队列
    链式栈
    链表
    指针
  • 原文地址:https://www.cnblogs.com/wxdlut/p/3347521.html
Copyright © 2020-2023  润新知