AWK - 我做了什么!

时间:2016-06-16 02:35:34

标签: awk gawk

我有以下代码部分:

 if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerECHLane") {
                $11 ~ /ms/ ( SUM10 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min10 = (min < $11 ? min : $11)
                max10 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                }

我想知道!i++做了什么,因为我知道i++在使用变量后递增变量,++i在变量使用之前递增变量。

我曾尝试使用Google,但它只显示了i++++i

之间的差异

提前致谢。

如同承诺,请参阅以下内容以获取完整的脚本:

#!/usr/bin/gawk -f

BEGIN {
min01 = 0
min02 = 0
min03 = 0
min04 = 0
min05 = 0
min06 = 0
min07 = 0 
min08 = 0 
min09 = 0
min10 = 0
max01 = 0 
max02 = 0 
max03 = 0
max04 = 0
max05 = 0
max06 = 0
max07 = 0
max08 = 0
max09 = 0 
max10 = 0
SUM01 = 0
SUM02 = 0
SUM03 = 0 
SUM04 = 0 
SUM05 = 0 
SUM06 = 0 
SUM07 = 0 
SUM08 = 0 
SUM09 = 0
SUM10 = 0
} #End of BEGIN
{ #Start of MID
    if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerPackingInfeedHanging") {
        $11 ~ /ms/ ( SUM01 += $11)
        if ($11 ~ /ms/) {
        !i++ (min = $11)
        !i++ (max = $11)
        {
        for (j= NR; j<= FNR; ++j) {
            min01 = (min < $11 ? min : $11)
            max01 = (max > $11 ? max : $11)
                      } #End of for
                }} #End of for wrapper
        } else
    if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerPackingAreaNOK") {
        $11 ~ /ms/ ( SUM02 += $11)
                !i++ (min = $11)
        !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min02 = (min < $11 ? min : $11)
                max02 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
        } else
     if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerPackingAreaMPANOK") {
                $11 ~ /ms/ ( SUM03 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min03 = (min < $11 ? min : $11)
                max03 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
     if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerPackingOrderBufferHanging") {
                $11 ~ /ms/ ( SUM04 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min04 = (min < $11 ? min : $11)
                max04 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
         if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerTUNotification") {
                $11 ~ /ms/ ( SUM05 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min05 = (min < $11 ? min : $11)
                max05 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
         if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerLaneStatusLaneLocker") {
                $11 ~ /ms/ ( SUM06 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min06 = (min < $11 ? min : $11)
                max06 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
         if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerPackingHangingBufferLane") {
                $11 ~ /ms/ ( SUM07 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min07 = (min < $11 ? min : $11)
                max07 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
         if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerLaneStatusHangingMPA") {
                $11 ~ /ms/ ( SUM08 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min08 = (min < $11 ? min : $11)
                max08 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
         if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerMPAHangingBufferLane") {
                $11 ~ /ms/ ( SUM09 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min09 = (min < $11 ? min : $11)
                max09 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                } else
         if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerECHLane") {
                $11 ~ /ms/ ( SUM10 += $11)
                !i++ (min = $11)
                !i++ (max = $11)
                {
                for (j= NR; j<= FNR; ++j) {
                min10 = (min < $11 ? min : $11)
                max10 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                }

    MSG_TYPE[substr($12,match($12,":")+1,match($12,")")-15)]++;
} #End of MID
END {
        for (b in MSG_TYPE) 
        if (b == "TelegramHandlerPackingInfeedHanging") {
            print b
                        print "MIN: "min01
                        print "MAX: "max01
                        print "AVG: "SUM01/FNR"ms"
                } else
        if (b == "TelegramHandlerPackingAreaNOK") {
            print b
                        print "MIN: "min02
                        print "MAX: "max02
                        print "AVG: "SUM02/FNR"ms"
                } else
                if (b == "TelegramHandlerPackingAreaMPANOK") {
            print b
                        print "MIN: "min03
                        print "MAX: "max03
                        print "AVG: "SUM03/FNR"ms"
                } else
                if (b == "TelegramHandlerPackingOrderBufferHanging") {
            print b
                        print "MIN: "min04
                        print "MAX: "max04
                        print "AVG: "SUM04/FNR"ms"
                } else
                if (b == "TelegramHandlerTUNotification") {
            print b
                        print "MIN: "min05
                        print "MAX: "max05
                        print "AVG: "SUM05/FNR"ms"
                } else
                if (b == "TelegramHandlerLaneStatusLaneLocker") {
            print b
                        print "MIN: "min06
                        print "MAX: "max06
                        print "AVG: "SUM06/FNR"ms"
                } else
                if (b == "TelegramHandlerPackingHangingBufferLane") {
            print b
                        print "MIN: "min07
                        print "MAX: "max07
                        print "AVG: "SUM07/FNR"ms"
                } else
                if (b == "TelegramHandlerLaneStatusHangingMPA") {
            print b
                        print "MIN: "min08
                        print "MAX: "max08
                        print "AVG: "SUM08/FNR"ms"
                } else
                if (b == "TelegramHandlerMPAHangingBufferLane") {
            print b
                        print "MIN: "min09
                        print "MAX: "max09
                        print "AVG: "SUM09/FNR"ms"
                } else
                if (b == "TelegramHandlerECHLane") {
            print b
                        print "MIN: "min10
                        print "MAX: "max10
                        print "AVG: "SUM10/FNR"ms"
}
} #End of END

返回的输出示例:

TelegramHandlerPackingInfeedHanging
MIN: 19.7ms
MAX: 19.7ms
AVG: 3.05067ms
TelegramHandlerPackingAreaNOK
MIN: 25.3ms
MAX: 25.3ms
AVG: 0.00706937ms
TelegramHandlerPackingAreaMPANOK
MIN: 22.3ms
MAX: 22.3ms
AVG: 4.26834e-05ms
TelegramHandlerPackingOrderBufferHanging
MIN: 12.0ms
MAX: 12.0ms
AVG: 1.60741ms
TelegramHandlerTUNotification
MIN: 8.7ms
MAX: 8.7ms
AVG: 4.00978ms
TelegramHandlerLaneStatusLaneLocker
MIN: 15.0ms
MAX: 15.0ms
AVG: 0.0527127ms
TelegramHandlerPackingHangingBufferLane
MIN: 23.9ms
MAX: 23.9ms
AVG: 13.3463ms
TelegramHandlerLaneStatusHangingMPA
MIN: 312.5ms
MAX: 312.5ms
AVG: 0.0126846ms
TelegramHandlerMPAHangingBufferLane
MIN: 57.0ms
MAX: 57.0ms
AVG: 0.135032ms
TelegramHandlerECHLane
MIN: 7.0ms
MAX: 7.0ms
AVG: 2.81495ms

这部分是我想要的。返回消息,substr($12,match($12,":")+1,match($12,")")-15)以及处理所需的min时间,处理时间max以及处理AVG时间。虽然minmax值相同,但我认为这些值会被覆盖而不是单独计算出来并保存到变量min01 - min10,max01 - max10,SUM01 - SUM10。

3 个答案:

答案 0 :(得分:2)

这是真正的awk代码吗?因为它确实看起来不像......

如果你正在寻找类似的东西:

!i++      { min = $11 }
$11 < min { min = $11 }

(在顶层),然后它只是混淆;它的意思是:

{ if (NR==1 || $11 < min) min = $11; }

或等效

NR == 1 || $11 < min  { min = $11; }

换句话说,如果您在第一行,或者第11个字段小于当前值min,请将min设置为第11个字段的值。在!i++中,i++递增i但返回原始未递增值作为传递给!的值,这意味着整个表达式:

  • 无条件增加i
  • 如果i在增量之前为0或未初始化,则
  • 为真

答案 1 :(得分:2)

无论是谁编写它都试图将其最小和最大变量初始化为所看到的第一个值,并且被awk的一般<condition> { <action> }语法混淆,因为它不适用于动作块中。

请记住:i从零开始,因此!i为1,i++为后增量,因此!i++在第一次遇到时也是1但是第二轮i为1,因此!i++为零,第三次为2且!i++仍然为零,等等......

这就是他们写的意思:

 if (substr($12,match($12,":")+1,match($12,")")-15) == "TelegramHandlerECHLane") {
                if ($11 ~ /ms/) {SUM10 += $11}
                if (!i++) {min = $11; max = $11}
                {
                for (j= NR; j<= FNR; ++j) {
                min10 = (min < $11 ? min : $11)
                max10 = (max > $11 ? max : $11)
                                          } #End of for
                                } #End of for wrapper
                }

但它仍然没有意义。

答案 2 :(得分:1)

此代码旨在将minmax值初始化为遇到的第一个值。

作者认为!i++ (min = $11)相当于

if (!i) {
    i++
    min=$11
}

!i++&&(min=$11)

会出现这种情况