【问题标题】:Write out specific data from log从日志中写出特定数据
【发布时间】:2013-08-21 07:16:14
【问题描述】:

我在压缩文件中有大量日志,我需要按特定数据写出行,如果相同的行包含具有相同 sessionID 的 XML 消息,则将写入文件。

日志结构:

 2013-08-16 16:31:06,810 ( 122:            rogate) [98839276727]  INFO  -      UId:10453, GId:5422: new CONX started, Application Context: disconnected
 2013-08-16 16:31:34,210 ( 122:            rogate) [98839276727]  INFO  -      UId:32453, GId:1213: new CONX started, Application Context: disconnected
 2013-08-16 16:31:45,110 ( 122:            rogate) [98839276727]  INFO  -      UId:11453, GId:2133: new CONX started, Application Context: disconnected
 2013-08-16 16:31:45,729 (1093:               jms_con.cpp) [140561430333184] DEBUG  - Received XML TextMessage:
 <?xml version="1.0" encoding="UTF-8"?><>
 <version>1</version>
 <sessionId>114532133</sessionId>
 <networkProtocolId>CAPv2</networkProtocolId>
 <trafficType>Forwarding</trafficType>
  <messages>
   <reportNotificationAck/>
 <superviseReq>
 <requestSequenceNr>0</requestSequenceNr>
 <time>60000</time>
 <releaseAfterTimeExpires>false</releaseAfterTimeExpires>
  <playWarningTone>false</playWarningTone>
 </superviseReq>
 <eventReportReq>
 <requestSequenceNr>1</requestSequenceNr>
 <events>
<routeSelectFailure monitorMode="Interrupt"/>
<busy monitorMode="Interrupt"/>
<noAnswer monitorMode="Interrupt">
  <noAnswerTimer>180000</noAnswerTimer>
</noAnswer>
<answer monitorMode="Notify"/>
<disconnectCalling monitorMode="Interrupt"/>
<disconnectCalled monitorMode="Interrupt"/>
<abandon monitorMode="Notify"/>
</events>
</eventReportReq>
<continueProcessing>
<requestSequenceNr>2</requestSequenceNr>
<moreEventsExpected>true</moreEventsExpected>
<interruptEventReceived>true</interruptEventReceived>
</continueProcessing>
2013-08-16 16:59:03,666 (1252:            capgw_main.cpp) [140561430333184]  INFO  - UId:57371, GId:7137: STAT_ISIG_PROCESSING: 0.001007.
2013-08-16 16:59:03,666 ( 888:  tcap_context_storage.cpp) [140561430333184] DEBUG  - UId:57371, GId:7137: updating the Last Appl. Access Time.
2013-08-16 16:59:03,666 ( 937:  tcap_context_storage.cpp) [140561430333184] DEBUG  - UId:57371, GId:7137: new Appl. message has different direction as previously stored one, calculating the response time.
2013-08-16 16:59:03,666 (1260:            capgw_main.cpp) [140561430333184] DEBUG  - UId:57371, GId:7137: TCAP Context Storage updated successfully (received iSig message).
2013-08-16 16:59:03,666 (1263:            capgw_main.cpp) [140561430333184]  INFO  - UId:57371, GId:7137: STAT_ISIG_RESP_TIME: 0.023346
2013-08-16 16:59:03,666 ( 767:  tcap_context_storage.cpp) [140561430333184] DEBUG  - UId:57371, GId:7137: updating the Last TCAP Access Time.

第三行之后的 XML 消息与 UiD+GiD 行具有相同的 sessionID。我需要将此行写入一个新文件,如下所示:

 2013-08-16 16:31:45,110 ( 122:            rogate) [98839276727]  INFO  -      UId:11453, GId:2133: new CONX started, Application Context: disconnected
 2013-08-16 16:31:45,729 (1093:               jms_con.cpp) [140561430333184] DEBUG  - Received XML TextMessage:
 <?xml version="1.0" encoding="UTF-8"?><>
 <version>1</version>
 <sessionId>114532133</sessionId>
 <networkProtocolId>CAPv2</networkProtocolId>
 <trafficType>Forwarding</trafficType>
  <messages>
   <reportNotificationAck/>
 <superviseReq>
 <requestSequenceNr>0</requestSequenceNr>
 <time>60000</time>
 <releaseAfterTimeExpires>false</releaseAfterTimeExpires>
  <playWarningTone>false</playWarningTone>
 </superviseReq>
 <eventReportReq>
 <requestSequenceNr>1</requestSequenceNr>
 <events>
<routeSelectFailure monitorMode="Interrupt"/>
<busy monitorMode="Interrupt"/>
<noAnswer monitorMode="Interrupt">
  <noAnswerTimer>180000</noAnswerTimer>
</noAnswer>
<answer monitorMode="Notify"/>
<disconnectCalling monitorMode="Interrupt"/>
<disconnectCalled monitorMode="Interrupt"/>
<abandon monitorMode="Notify"/>
</events>
</eventReportReq>
<continueProcessing>
<requestSequenceNr>2</requestSequenceNr>
<moreEventsExpected>true</moreEventsExpected>
<interruptEventReceived>true</interruptEventReceived>
</continueProcessing>

其中一个名为 XML 消息 sessionID 的文件,如 114532133_something.txt 并将每两条日志消息写入一个新文件。

感谢您的帮助!

编辑:

尝试在没有太多成功的脚本中执行。

#!/usr/bin/awk -f

BEGIN { FS=":|," }
FNR==NR && /INFO/ {
    a[$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10]++ ;
    next

}

END

{
    for (i in a) print i
}

【问题讨论】:

  • 您可以发布 2 个或更多数据块吗?它将帮助我了解如何分解它
  • 我添加了一些其他的行。只需要 XML 消息出现的那些行。其中sessionID与UId+GId相同。并且每条这一行+XML 消息都打印在一个新文件中,这个文本文件名为 sessionID_cpv.txt。谢谢
  • 您希望文件中有什么内容?只有日期信息的行,xml 块?您的文件中是否有多个要重复的 xml 块?
  • 是的,还有更多 XML 消息,但只需要那些 sessionID 与 UID+GId 相同的消息。文件包含整行+ XML消息会很棒。
  • “请为我编写程序”不是 StackOverflow 的恰当用法。

标签: shell unix awk


【解决方案1】:

我已将其分为多个任务。可能有人可以将这一切结合在一起。

1- 查找所有包含 INFO 的 Session ID 表单行并将其存储到文件 t1

awk -F":|," 'FNR==NR && /INFO/ {a[$6$8]++;next} END {for (i in a) print i }' xml_file >t1

2- 删除所有日志行(从 2013 年开始)并将其存储在 t2

awk  '!/^2013/' xml_file >t2

3- 如果每个 XML 批量包含在步骤 1 中找到的 ID 之一,则打印它

awk 'FNR==NR {a[$1]++;next} FNR==1 {RS="</continueProcessing>"} { for (i in a) {if ($0~i) print}}'  t1 t2

我假设所有 XML 部分都以 .如果不是这样,这将不起作用。

【讨论】:

  • 但我不想删除日志行,只需要那些存在具有相同会话 id 的 XML 消息的日志行,日志行包含 UId+GId。并为每个(日志行+XML 消息)创建一个名为 sessionID_something.txt 的新文件。
  • 所以你想要在日志行中找到所有日志行+所有带有 INFO 的 xml 批量?所以只需删除没有 INFO 日志行的 XML bluks
  • 所有包含 INFO 和 XML 消息的日志行。只需将这些行放入新文件中。每个日志行+XML 消息到一个新文件中,该文件将被称为 XML 消息 sessionID。例如:在日志中,我得到了一条带有 UId + GId 的行,对于其中一些日志,得到了一条带有数字 (UId+GId) 作为 sessionID 的 XML 消息。我需要将这两件事写到一个新文件中。存在 XML 消息的每个日志行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
相关资源
最近更新 更多