AWK - !i++ 做什么

AWK - What does !i++ do

我有以下代码段:

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

    MSG_TYPE[substr(,match(,":")+1,match(,")")-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

这部分是在做我想做的事情。 Return 消息,substr(,match(,":")+1,match(,")")-15) 以及 min 处理时间,max 处理时间和 AVG 处理时间。尽管 minmax 值相同,所以我认为这些值正在被覆盖而不是单独计算,并将它们保存到变量 min01 - min10、max01 - max10、SUM01 - SUM10。

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

如果您正在看类似这样的内容:

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

(在顶层),那就是混淆了;它的含义与:

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

或等同于

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

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

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

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

记住:i 从零开始,所以 !i 是 1,i++ 是 post 增量,所以 !i++ 也是 1第一次遇到,但第二次 i 是 1,所以 !i++ 是零,第三次我是 2,!i++ 仍然是零,依此类推...

这就是他们要写的内容:

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

但它仍然没有真正意义。

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

作者认为 !i++ (min = ) 等同于

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

如果 !i++&&(min=)

会是这样