zoukankan      html  css  js  c++  java
  • 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` 

  • 相关阅读:
    机房收费系统重构(三)—工厂+反射+DAL
    机房收费系统重构(二)—菜鸟入门
    机房收费系统重构(—)—小试牛刀
    vb.net机房收费登录功能
    设计模式总结之结构型模式
    设计模式总结之创建型模式
    大话设计之桥接模式
    大话设计之单例模式
    大话设计之适配器模式
    大话设计之抽象工厂模式
  • 原文地址:https://www.cnblogs.com/wxdlut/p/3347521.html
Copyright © 2011-2022 走看看