AWK - Organising/Sorting 多维数组 3.1.7

AWK - Organising/Sorting multidimentional arrays 3.1.7

我的问题

I need to look at all lines within a log file (see input below) to work out how to organize the hours (</code>) 00 - 23 (not always there all the time) without using a variable that increments and only print a single line for each hour.</p> </blockquote> <h2>输入文件样本</h2> <pre><code>... INFO 2016-06-15 00:00:30.065 TelegramDispatcher - --> Complete telegram dispatching took 8.9ms (canHandle(56:TelegramHandlerECHLane) took 0.0ms, handleTelegram took 2.2ms, commit took 5.6ms, doACK took 0.6ms, doNAK took -0.0ms performAfterCommit took 0.3ms, failedCanHandle took 0.1ms) INFO 2016-06-15 00:00:30.072 TelegramDispatcher - --> Complete telegram dispatching took 7.2ms (canHandle(56:TelegramHandlerECHLane) took 0.0ms, handleTelegram took 2.0ms, commit took 4.1ms, doACK took 0.7ms, doNAK took -0.0ms performAfterCommit took 0.3ms, failedCanHandle took 0.1ms) INFO 2016-06-15 00:00:30.114 TelegramDispatcher - --> Complete telegram dispatching took 12.4ms (canHandle(69:TelegramHandlerTUNotification) took 0.0ms, handleTelegram took 4.3ms, commit took 6.6ms, doACK took 1.0ms, doNAK took -0.0ms performAfterCommit took 0.3ms, failedCanHandle took 0.2ms) INFO 2016-06-15 00:00:30.165 TelegramDispatcher - --> Complete telegram dispatching took 19.6ms (canHandle(69:TelegramHandlerTUNotification) took 0.0ms, handleTelegram took 3.6ms, commit took 14.3ms, doACK took 0.9ms, doNAK took -0.0ms performAfterCommit took 0.5ms, failedCanHandle took 0.1ms) INFO 2016-06-15 00:00:30.271 TelegramDispatcher - --> Complete telegram dispatching took 10.0ms (canHandle(69:TelegramHandlerTUNotification) took 0.0ms, handleTelegram took 3.7ms, commit took 4.8ms, doACK took 0.9ms, doNAK took -0.0ms performAfterCommit took 0.3ms, failedCanHandle took 0.1ms) INFO 2016-06-15 00:00:30.300 TelegramDispatcher - --> Complete telegram dispatching took 18.7ms (canHandle(61:TelegramHandlerPackingOrderBufferHanging) took 0.0ms, handleTelegram took 7.3ms, commit took 10.2ms, doACK took 0.8ms, doNAK took -0.0ms performAfterCommit took 0.3ms, failedCanHandle took 0.1ms) ...

当前代码

#!/usr/bin/gawk -f

BEGIN {
        print "-------------------------------------------------------"
        print "----------Telegram Processing Time by Hour-------------"
        print "-------------------------------------------------------"
} #End of BEGIN
{ #Start of MID
        key = substr(,match(,":")+1,match(,")")-15); #Message Extracted 10 Total
        key2 = substr(,1,2) #Hour
        MSG_TYPE[key]++ #Distinct Message
        MSG_HR[key,key2] +=  #Tots up Time Took for each MSG by Hour
} #End of MID
END {
                for (msg in MSG_TYPE) {
                        print msg
                        print "-----------------------------------"
                        for(msghr in MSG_HR) {
                        split(msghr,indices,SUBSEP);
                        hr = indices[2];
                        print msg #Added this to try and Debug
                        print hr
                        print "AVG by Hour: "MSG_HR[msghr]"ms"
                        }
                print "\n"
                        }
} #End of END

为了稍微解释一下这段代码,MSG_HR 数组当前正在累加 </code>,供参考的是第一个 <code>took 之后的 ***mskey 返回 msgkey2 返回小时。

当前输出样本

...
TelegramHandlerECHLane
-----------------------------------
TelegramHandlerECHLane
14
AVG by Hour: 80950.6ms
TelegramHandlerECHLane
08
AVG by Hour: 25.2ms
TelegramHandlerECHLane
01
AVG by Hour: 75053.9ms
...
TelegramHandlerLaneStatusHangingMPA
-----------------------------------
TelegramHandlerLaneStatusHangingMPA
14
AVG by Hour: 80950.6ms
TelegramHandlerLaneStatusHangingMPA
08
AVG by Hour: 25.2ms
TelegramHandlerLaneStatusHangingMPA
01
AVG by Hour: 75053.9ms
...

所需的输出样本

...
TelegramHandlerECHLane
-----------------------------------
00
AVG by Hour: 
01
AVG by Hour:
02
AVG by Hour:
03
AVG by Hour:
04
AVG by Hour:
05
AVG by Hour:
06
AVG by Hour:
...

有 10 种不同的 MSG_TYPES 我试图以上述格式列出。我无法升级到 awk V 4.1.0,因为我知道这会让事情变得更容易。 (真正的多维数组等。)

我们将不胜感激任何帮助。

您的代码中似乎有错误。摘自您的代码(我修复了缩进):

        for (msg in MSG_TYPE) {
            print msg
            print "-----------------------------------"
            for(msghr in MSG_HR) {
                split(msghr,indices,SUBSEP);
                msg = indices[1];

此外,您的外循环是通过消息进行的。在您的内部循环中,您处理 MSG_HR 的所有成员,而不仅仅是那些具有特定消息的成员。这似乎很奇怪。 您的外循环将 msg 作为循环变量。但是,在您的内部循环中,您更改了 msg (msg = indices[1];)。在循环中更改循环变量可能是一个错误。