如何从日志文件中提取 XML

如何从日志文件中提取 XML

我在下面有这个日志文件,我想通过使用字段作为输入 search 来提取其中的 XML <ns12:equipmentId>089471386301077634</ns12:equipmentId>,因此脚本应该在日志文件中进行搜索089471386301077634并返回整个 XML,如<ns1:queryDeviceInfoBtRequest...直到</ns1:queryDeviceInfoBtRequest>,任何知道如何做到吗?

...
..
.
2016/11/09 07:29:18
Request:
mqMessage:
MQMD:[queue=EKS01A2T/EKS.QUERYDEVICEINFO,applicationOriginData=    ,format=MQSTR   ,backoutCount=0,characterSet=1208,correlationId=000000000000000000000000000000000000000000000000,encoding=273,expiry=500,messageId=414D51204450303141325420202020205788160022E00ACA,persistence=0,putApplicationName=WebSphere Datapower MQClient,putDateTime=2016-10-9T12:29:18.490-GMT,replyToQueueManagerName=DP01A2T                                         ,replyToQueueName=76S.REPLY16                                     ,userId=null]
Data:[<?xml version="1.0" encoding="UTF-8"?>

<ns1:queryDeviceInfoBtRequest xmlns:ns1="http://integration.sprint.com/integration/interfaces/queryDeviceInfoBtV1/v1/queryDeviceInfoBtV1">
<ns3:mqMessageHeader xmlns:ns3="http://integration.sprint.com/public/interfaces/MQMessageHeaderV2.xsd">
<ns3:messageHeaderVersion>2</ns3:messageHeaderVersion>
<ns3:serviceName>queryDeviceInfoBt</ns3:serviceName>
<ns3:serviceQualifier>76S</ns3:serviceQualifier>
<ns3:serviceVersion>1</ns3:serviceVersion>
<ns3:dialogTypeCode>2</ns3:dialogTypeCode>
<ns3:dialogSubTypeCode>1</ns3:dialogSubTypeCode>
<ns3:parentDialogReference>7V2-CONVID-aab02e21</ns3:parentDialogReference>
<ns3:dialogReference>7V2-MSGID-SP-aab02e21</ns3:dialogReference>
<ns3:applicationReference>7V2</ns3:applicationReference>
<ns3:applicationGroup>7V2</ns3:applicationGroup>
<ns3:componentGroup>Equipment</ns3:componentGroup>
<ns3:componentName>queryDeviceInfoBt</ns3:componentName>
<ns3:reqSentDateTime>2016-11-09T07:29:18.003-05:00</ns3:reqSentDateTime>
<ns3:applicationUserId>7V2</ns3:applicationUserId></ns3:mqMessageHeader>
<data xmlns="http://integration.sprint.com/integration/interfaces/queryDeviceInfoBtV1/v1/queryDeviceInfoBtV1">
<ns12:queryDeviceInfo xmlns="http://integration.sprint.com/interfaces/validateDevice/v8/validateDeviceV8.xsd" xmlns:ns2="http://integration.sprint.com/common/header/WSMessageHeader/v2" xmlns:ns3="http://integration.sprint.com/common/ErrorDetailsV2.xsd" xmlns:ns4="http://integration.sprint.com/interfaces/queryDeviceResourceInfo/v1/queryDeviceResourceInfo.xsd" xmlns:ns5="http://integration.sprint.com/interfaces/queryCdmaDeviceInfo/v2/queryCdmaDeviceInfoV2.xsd" xmlns:ns6="http://integration.sprint.com/interfaces/queryAccountDeviceList/v1/queryAccountDeviceList.xsd" xmlns:ns7="http://integration.sprint.com/interfaces/queryDeviceCapabilities/v2/queryDeviceCapabilitiesV2.xsd" xmlns:ns8="http://integration.sprint.com/interfaces/validateGiveBackDevice/v1/validateGiveBackDevice.xsd" xmlns:ns9="http://integration.sprint.com/interfaces/queryDeviceResourceInfo/v2/queryDeviceResourceInfoV2.xsd" xmlns:ns10="http://integration.sprint.com/interfaces/validateDevice/v7/validateDeviceV7.xsd" xmlns:ns11="http://integration.sprint.com/interfaces/queryDeviceList/v1/queryDeviceList.xsd" xmlns:ns13="http://integration.sprint.com/interfaces/queryDeviceHistoryByResource/v1/queryDeviceHistoryByResource.xsd" xmlns:ns14="http://integration.sprint.com/interfaces/querySubscriberDeviceList/v1/querySubscriberDeviceList.xsd" xmlns:ns15="http://integration.sprint.com/interfaces/queryDeviceCatalogInfo/v1/queryDeviceCatalogInfo.xsd" xmlns:ns16="http://integration.sprint.com/interfaces/validateDevice/v6/validateDeviceV6.xsd" xmlns:ns17="http://integration.sprint.com/interfaces/queryDeviceOrderHistory/v1/queryDeviceOrderHistory.xsd" xmlns:ns18="http://integration.sprint.com/interfaces/validateBuybackDevice/v1/validateBuybackDevice.xsd" xmlns:ns19="http://integration.sprint.com/interfaces/validateDevice/v9/validateDeviceV9.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns12="http://integration.sprint.com/interfaces/queryDeviceInfo/v1/queryDeviceInfo.xsd">
<ns12:info>
<ns12:equipmentId>089471386301077634</ns12:equipmentId>
<ns12:checkPreviouslyActivated>true</ns12:checkPreviouslyActivated>
<ns12:brandCode>BST</ns12:brandCode></ns12:info></ns12:queryDeviceInfo></data></ns1:queryDeviceInfoBtRequest>]


2016/11/09 07:29:18
OutputQMgr/OutputQueue:[null/EKS.VALDVC]
MQPutMessage:
MQMD:[applicationOriginData=    ,backoutCount=0,characterSet=819,correlationId=000000000000000000000000000000000000000000000000,encoding=273,expiry=500,messageId=414D5120454B53303141325420202020C54617580FB08F20,persistence=2,putApplicationName=java                        ,putDateTime=2016-10-9T12:29:18.510-GMT,replyToQueueManagerName=                                                ,replyToQueueName=EKS.REPLY03                                     ,userId=eaiowner    ]
Data:[1VALDVCBT            3  10 00000_EKS                                                                                       EKS     eksSystem       00000000                             089471386301077634                                       BST]


2016/11/09 07:29:18
MQGetMessage:
MQMD:[applicationOriginData=    ,backoutCount=0,characterSet=819,correlationId=414D5120454B53303141325420202020C54617580FB08F20,encoding=273,expiry=500,messageId=414D5120454B53303141325420202020C54617580FB08F20,persistence=0,putApplicationName=java                        ,putDateTime=2016-10-9T12:29:18.520-GMT,replyToQueueManagerName=EKS01A2T                                        ,replyToQueueName=EKS.REPLY03                                     ,userId=eaiowner    ]
Data:[4VALDVCBT_R          3  10 00000                                                                                           EKS     eksSystem      00000000010 Device is valid and cleared for use                                             APPLE, INC                    IPH 6 GRAY 16 BST BOX SGL     CG885909950249        35544007107182U0    SUCCESS                                                                                                                                                CO                                                      I          ]


2016/11/09 07:29:18
 Select  rtrim(id.item_id), trim(id.serial_type), trim(id.item_ldesc),  trim(id.item_sdesc), trim(id.item_ldesc), trim(id.manf_cd),  trim(id.network_ind), trim(id.used_eqp), trim(sii.pool),  trim(sii.location_id), trim(sii.activity_code), to_char(sii.activity_date, 'mmddyyyy'),  trim(sii.item_ownership),rtrim(sii.curr_possession), trim(sii.missing_ind),  trim(sii.in_repair_ind), trim(sii.in_transit_ind), trim(sii.comited_to_pos_ind),  trim(sii.software_ver),rtrim(sii.SERIAL_NUMBER),trim(item_id_type)  FROM serial_item_inv sii, item_definition id  WHERE sii.serial_number= ? AND  sii.item_id = id.item_id
{[0]089471386301077634}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getEquipmentDetails ]
Elapsed time (ms) =1

2016/11/09 07:29:18
 select TRIM(manf_cd), TRIM(item_ldesc), TRIM(item_id_type) from item_definition where item_id = rpad(:mdlNbr,15) AND ROWNUM < 2
{[0]885909950249}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getManufacturerInfo ]
Elapsed time (ms) =1

2016/11/09 07:29:18
 SELECT ea.ATTR_CODE  FROM EQUIPMENT_ATTR ea, ITEM_DEFINITION id  WHERE id.ITEM_ID = ea.ITEM_ID  AND id.ITEM_ID = rpad(?,15)
{[0]885909950249}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getDeviceDetailInfo ]
Elapsed time (ms) =1

2016/11/09 07:29:18
 select case when  count(*) > 0 THEN 'true'  else 'false' end  from EQP_RULE_LINK where item_id = RPAD(:item_id, 15) and group_id in('MF','MT','MC','MB','M4')
{[0]885909950249}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getDeviceDetailInfo ]
Elapsed time (ms) =0

2016/11/09 07:29:18
 SELECT DISC_AMT from EQUIP_AGR_GEN_ATTR WHERE ATTR_CODE= 'DISCELG'
{}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getDeviceDetailInfo ]
Elapsed time (ms) =1

2016/11/09 07:29:18
 select TRIM(MODEL_NME) from device_info where wrls_dev_sku_nbr = TRIM(:item_id)
{[0]885909950249}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getMarketingName ]
Elapsed time (ms) =1

2016/11/09 07:29:18
 SELECT  decode(sa.cross_fleet_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.dc_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.cell_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.pds_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.mms_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.sdgc_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.gprs_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.ext800_ind, 'Y', 'true', 'N','false', 'false')  , decode(sa.ptt_ind, 'Y', 'true', 'N','false', 'false')  FROM software_attr sa  WHERE sa.item_id=rpad(:itemId, 15)  AND (:phoneSoftware is null or sa.software_ver = :phoneSoftware)
{[0]885909950249,[1]null,[2]null}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getPhoneSoftwareInfo ]
Elapsed time (ms) =0

2016/11/09 07:29:18
 SELECT ea.ATTR_CODE  FROM EQUIPMENT_ATTR ea, ITEM_DEFINITION id  WHERE id.ITEM_ID = ea.ITEM_ID  AND id.ITEM_ID = rpad(?,15)
{[0]885909950249}
jdbc:eaidb:dbpool
[ com.sprint.eai.queryDeviceinfo.dao.QueryDeviceInfoDao : getEquipAttrCode ]
Elapsed time (ms) =1

2016/11/09 07:29:18
OutputQMgr/OutputQueue:[null/EKS.REQDVCHIST]
MQPutMessage:
MQMD:[applicationOriginData=    ,backoutCount=0,characterSet=819,correlationId=000000000000000000000000000000000000000000000000,encoding=273,expiry=500,messageId=414D5120454B53303141325420202020C546175810B08F20,persistence=2,putApplicationName=java                        ,putDateTime=2016-10-9T12:29:18.540-GMT,replyToQueueManagerName=                                                ,replyToQueueName=EKS.REPLY03                                     ,userId=eaiowner    ]
Data:[1REQDVCHISTBT        3  2  00000_EKS                                                                                       EKS     eksSystem       00000000                             089471386301077634E1         ]


2016/11/09 07:29:18
MQGetMessage:
MQMD:[applicationOriginData=    ,backoutCount=0,characterSet=819,correlationId=414D5120454B53303141325420202020C546175810B08F20,encoding=273,expiry=500,messageId=414D5120454B53303141325420202020C546175855AB8F20,persistence=0,putApplicationName=java                        ,putDateTime=2016-10-9T12:29:18.560-GMT,replyToQueueManagerName=EKS01A2T                                        ,replyToQueueName=EKS.REPLY03                                     ,userId=eaiowner    ]
Data:[4REQDVCHISTBT_R      3  2  00000                                                                                           EKS     eksSystem      200000001NMS                 REQDVCHISTBT        REQDVCHISTBT                                           nms_exception                                     00000115Server.704:NMS returned status_code=38, status_text=DOES_NOT_EXIST_IN_DB: No records found in SubscriberLinkHistory]


2016/11/09 07:29:18
Response:
mqMessage:
MQMD:[applicationOriginData=    ,format=MQSTR   ,backoutCount=0,characterSet=1208,correlationId=414D51204450303141325420202020205788160022E00ACA,encoding=273,expiry=500,messageId=,persistence=0,putApplicationName=WebSphere Datapower MQClient,putDateTime=null,replyToQueueManagerName=DP01A2T                                         ,replyToQueueName=76S.REPLY16                                     ,userId=null]
Data:[<?xml version="1.0" encoding="UTF-8"?>
<quer:queryDeviceInfoBtResponse xmlns:quer="http://integration.sprint.com/integration/interfaces/queryDeviceInfoBtV1/v1/queryDeviceInfoBtV1" xmlns:quer1="http://integration.sprint.com/interfaces/queryDeviceInfo/v1/queryDeviceInfo.xsd">
  <ns3:mqMessageHeader xmlns:ns3="http://integration.sprint.com/public/interfaces/MQMessageHeaderV2.xsd">
    <ns3:messageHeaderVersion>2</ns3:messageHeaderVersion>
    <ns3:serviceName>queryDeviceInfoBt</ns3:serviceName>
    <ns3:serviceQualifier>76S</ns3:serviceQualifier>
    <ns3:serviceVersion>1</ns3:serviceVersion>
    <ns3:dialogTypeCode>2</ns3:dialogTypeCode>
    <ns3:dialogSubTypeCode>4</ns3:dialogSubTypeCode>
    <ns3:parentDialogReference>7V2-CONVID-aab02e21</ns3:parentDialogReference>
    <ns3:dialogReference>7V2-MSGID-SP-aab02e21</ns3:dialogReference>
    <ns3:applicationReference>7V2</ns3:applicationReference>
    <ns3:applicationGroup>7V2</ns3:applicationGroup>
    <ns3:componentGroup>Equipment</ns3:componentGroup>
    <ns3:componentName>queryDeviceInfoBt</ns3:componentName>
    <ns3:reqSentDateTime>2016-11-09T07:29:18.003-05:00</ns3:reqSentDateTime>
    <ns3:applicationUserId>7V2</ns3:applicationUserId>
    <ns3:replyCompletionCode>0</ns3:replyCompletionCode>
  </ns3:mqMessageHeader>
  <quer:data>
    <quer1:queryDeviceInfoResponse>
      <quer1:equipmentInfo>
        <quer1:equipmentId>089471386301077634</quer1:equipmentId>
        <quer1:itemId>885909950249</quer1:itemId>
        <quer1:serialType>E</quer1:serialType>
        <quer1:esn>089471386301077634</quer1:esn>
        <quer1:esnHex>35544007107182</quer1:esnHex>
        <quer1:description>IPH 6 GRAY 16 BST BOX SGL</quer1:description>
        <quer1:manufacturer>436</quer1:manufacturer>
        <quer1:modelNumber>885909950249</quer1:modelNumber>
        <quer1:phoneModel>IPH 6 GRAY 16 BST BOX SGL</quer1:phoneModel>
        <quer1:possession>A</quer1:possession>
        <quer1:negativeInd>false</quer1:negativeInd>
        <quer1:cellInd>true</quer1:cellInd>
        <quer1:pdsInd>true</quer1:pdsInd>
        <quer1:pttInd>false</quer1:pttInd>
        <quer1:networkInd>C</quer1:networkInd>
        <quer1:pamInd>true</quer1:pamInd>
        <quer1:hppttInd>false</quer1:hppttInd>
        <quer1:evdoInd>true</quer1:evdoInd>
        <quer1:abGlmsInd>false</quer1:abGlmsInd>
        <quer1:previouslyActivated>N</quer1:previouslyActivated>
        <quer1:equipmentStatus>D</quer1:equipmentStatus>
        <quer1:inputSerialType>E</quer1:inputSerialType>
        <quer1:wiMaxInd>false</quer1:wiMaxInd>
        <quer1:lteInd>true</quer1:lteInd>
        <quer1:lteUiccType>U</quer1:lteUiccType>
        <quer1:iccId/>
        <quer1:uiccStatus/>
        <quer1:isBroadband2GoInd>false</quer1:isBroadband2GoInd>
        <quer1:isBlackberryDeviceInd>false</quer1:isBlackberryDeviceInd>
        <quer1:isDeviceInsuranceEligibleInd>Y</quer1:isDeviceInsuranceEligibleInd>
        <quer1:isAppleInsuranceApplicableInd>N</quer1:isAppleInsuranceApplicableInd>
        <quer1:isDeviceMRCDiscountEligibleInd>true</quer1:isDeviceMRCDiscountEligibleInd>
        <quer1:mrcDiscountAmount>5</quer1:mrcDiscountAmount>
        <quer1:marketingName>Boost iPhone 6 16GB</quer1:marketingName>
      </quer1:equipmentInfo>
    </quer1:queryDeviceInfoResponse>
  </quer:data>
</quer:queryDeviceInfoBtResponse>]
.
..
...

答案1

sed -rn '
/<ns1:queryDeviceInfoBtRequest/!b
h
:X
n
/<\/ns1:queryDeviceInfoBtRequest>/ {
    H;g
    />089471386301077634</p
    b
}
H
bX
' xmlfile

答案2

是的,这实际上很容易。您的 XML 似乎是开始从 到Data:[结束于]\n\n

唯一的问题是如果 XML 中有括号,就会变得有点尴尬。

反正:

#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;

#hardcoded. But could read this from the command line with 
#e.g. shift @ARGV
#this will 'take' the first argument, but leave any others (e.g. filenames to read)
my $search_id = '089471386301077634'; 

#set record separator
local $/ = ">]\n";

#iterate the 'magic' filehandle.
# <> is either STDIN or files specified as args, just like "grep"
while ( <> ) {

   #match Data followed by XML header. Multi-line 
   my ( $xml ) = m/Data:\[(\<\?xml[^]]+)/gms;
   #if no match (e.g. not XML ) then skip
   next unless $xml;
   #parse it - pick up any errors (XML parse errors should be fatal
   #hence the eval
   my $twig = eval { XML::Twig -> parse ( $xml ); };
   if ($@) {warn "XML blob couldn't be parsed $@\n"};

   #set output formatting. (optional)
   $twig -> set_pretty_print('indented_a');
   #print it to STDOUT. 
   #but test if the 'equipment ID' is as required first:
   if ( $twig -> findnodes("//ns12:equipmentId[string()=\"$search_id\"]") ) {
       print "Found: \n"; 
       $twig -> findnodes("//ns12:equipmentId[string()=\"$search_id\"]",0) -> print;
       print "\nFull XML:\n";
       $twig -> set_pretty_print('indented_a');
       $twig -> print;
   }
}

相关内容