需要解析的源文件:
/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`