免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2437 | 回复: 3

[文本处理] 关于log中xml内容的处理 [复制链接]

论坛徽章:
0
发表于 2019-06-27 10:55 |显示全部楼层
本帖最后由 apostleyc 于 2019-06-27 10:55 编辑

日志中有如下大量xml内容,现需要提取每个xml内容中的 RequestID的value 以及list列表中所有的target 的value,每个xml输出一行。
不知道大神们有没有什么好办法或者思路呀
比如下列输出为:
803dd00e016b2816b24b8a16d317,123456789
804b86ba1016b2d395816b2f00db,123456  1234581

#################################################################################
logfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilexxxx
logfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilelogfile
<request>
    <common>
        <property id="QueryType" value="1234"/>
        <property id="RequestID" value="803dd00e016b2816b24b8a16d317"/>
        <property id="JoinRegion" value="test1"/>
        <property id="CodeList" value="all"/>
        <property id="UserID" value="sl"/>
        <property id="UserIP" value="192.168.1.1"/>
        <property id="SrcAccountID" value=""/>
        <property id="DataSource" value="test2"/>
    </common>
    <incident>
        <property id="QueryTime" value="2018-07-05 00:00:00,2019-06-05 16:38:08,between"/>
        <property id="DataType" value="test3"/>
        <property id="EventType" value=""/>
        <list count="1">
            <value>
                <property id="Target" value="123456789"/>
                <property id="TargetType" value="test4"/>
            </value>
        </list>
    </incident>
</request>
logfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilelogfilelogfile
logfilelogfilelogfilelogfilelogfilexxxxxxxx
<request>
    <common>
        <property id="QueryType" value="1234"/>
        <property id="RequestID" value="804b86ba1016b2d395816b2f00db"/>
        <property id="JoinRegion" value="test1"/>
        <property id="CodeList" value="all"/>
        <property id="UserID" value="xx"/>
        <property id="UserIP" value="192.168.1.1"/>
        <property id="DataSource" value="test2"/>
    </common>
    <incident>
        <property id="QueryTime" value="2019-01-01 00:00:00,2019-06-05 16:14:02,between"/>
        <property id="DataType" value="test3"/>
        <list count="2">
            <value>
                <property id="Target" value="123456"/>
                <property id="TargetType" value="test4"/>
            </value>
            <value>
                <property id="Target" value="1234581"/>
                <property id="TargetType" value="test4"/>
            </value>
        </list>
    </incident>
</request>
logfilelogfilelogfilelogfilelogfile
logfilelogfilelogfilelogfilelogfile
logfilelogfilelogfilelogfilelogfile
#################################################################################

论坛徽章:
25
程序设计版块每日发帖之星
日期:2016-05-03 06:20:0015-16赛季CBA联赛之八一
日期:2018-07-05 10:34:09黑曼巴
日期:2018-07-06 15:19:5015-16赛季CBA联赛之佛山
日期:2018-08-03 13:19:3315-16赛季CBA联赛之山西
日期:2018-08-07 19:46:2315-16赛季CBA联赛之广夏
日期:2018-08-08 19:31:5015-16赛季CBA联赛之青岛
日期:2018-11-26 15:21:5015-16赛季CBA联赛之上海
日期:2018-12-11 09:45:3219周年集字徽章-年
日期:2020-04-18 23:54:5215-16赛季CBA联赛之深圳
日期:2020-04-19 21:40:19黑曼巴
日期:2022-04-03 17:55:1315-16赛季CBA联赛之八一
日期:2018-07-03 16:56:46
发表于 2019-06-27 11:26 |显示全部楼层
本帖最后由 wh7211 于 2019-06-27 11:28 编辑

回复 1# apostleyc


  1. awk 'match($0,"RequestID\" value=\"([^\"]+)",a){b=a[1]}match($0,"Target\" value=\"([^\"]+)",c){d=d?d" "c[1]:c[1]}/<\/request>/{printf("%s,%s\n",b,d);b=d=""}' file
复制代码

论坛徽章:
0
发表于 2019-06-27 14:53 |显示全部楼层
回复 2# wh7211

多谢多谢,太厉害了。我学习一下... - -||

论坛徽章:
0
发表于 2019-06-27 17:15 |显示全部楼层
$ awk '/"RequestID"/ {match($3, /value="(.+)"/, arr); request_id=arr[1];} /"Target"/ {match($3, /value="(.+)"/, arr); target[request_id]=target[request_id]?target[request_id]" "arr[1]:arr[1]} END {for (i in target) { print i",\t"target[i]}}' 1
804b86ba1016b2d395816b2f00db,   123456 1234581
803dd00e016b2816b24b8a16d317,   123456789
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP