Personal popular awk command

Disclaimer: This article is a blogger original article, reproduced, please indicate the original source. https://blog.csdn.net/woay2008/article/details/86515913

1. Some formatting / proc file output

Output content in some Linux proc file really can not see, such as /proc/net/snmpthe output is as follows:

[root@localhost ~]# cat /proc/net/snmp 
Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagrams InUnknownProtos InDiscards InDelivers OutRequests OutDiscards OutNoRoutes ReasmTimeout ReasmReqds ReasmOKs ReasmFails FragOKs FragFails FragCreates
Ip: 2 64 1112736 0 0 0 0 0 1112733 1112545 0 36 0 0 0 0 0 0 0
Icmp: InMsgs InErrors InCsumErrors InDestUnreachs InTimeExcds InParmProbs InSrcQuenchs InRedirects InEchos InEchoReps InTimestamps InTimestampReps InAddrMasks InAddrMaskReps OutMsgs OutErrors OutDestUnreachs OutTimeExcds OutParmProbs OutSrcQuenchs OutRedirects OutEchos OutEchoReps OutTimestamps OutTimestampReps OutAddrMasks OutAddrMaskReps
Icmp: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts InCsumErrors
Tcp: 1 200 120000 -1 2207 2204 4 598 3803 1112646 1112462 0 0 303 0
Udp: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors InCsumErrors IgnoredMulti
Udp: 176 0 0 91 0 0 0 0
UdpLite: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors InCsumErrors IgnoredMulti
UdpLite: 0 0 0 0 0 0 0 0

The first field is the row, the second row is the value of the field. These outputs are too many fields contained in each row, aligned not any fields and values, looking too hard, then a awkcommand to format the output such later for easy viewing.
awkCommand is as follows:

awk '{\
    if (NR % 2 == 0) print $1;\
    for(i = 2; i <= NF; i++) {\
        if (NR % 2 ) \
            count[i] = $i;\
        else if ($i > 0) \
            printf("    %-28s:%d\n",count[i], $i);\
    }\
}' \
/proc/net/snmp

Effect formatted as follows:

Ip:
    Forwarding                  2
    DefaultTTL                  64
    InReceives                  2252593
    InDelivers                  2252590
    OutRequests                 2252398
    OutNoRoutes                 36
Icmp:
Tcp:
    RtoAlgorithm                1
    RtoMin                      200
    RtoMax                      120000
    ActiveOpens                 3807
    PassiveOpens                3804
    AttemptFails                4
    EstabResets                 598
    CurrEstab                   7003
    InSegs                      2252503
    OutSegs                     2252315
    OutRsts                     303
Udp:
    InDatagrams                 176
    OutDatagrams                91
UdpLite:

Print command field and a value above the non-zero value according to the format. The command is also suitable /proc/net/netstatfile.
Binding watchorder to obtain the timing can view the changes in these values, the following command:

watch -n 1 "awk '{if (NR % 2 == 0) print \$1;for(i = 2; i <= NF; i++) {if (NR % 2 ) count[i]=\$i; else if (\$i > 0 ) printf(\"    %-28s%d\n\",count[i], \$i);}}' /proc/net/netstat"

Further, watchthe command can only see the value of a statistical point in time, if you want to see change in value per second, is not so easy, it took me some time to write the following command to achieve this effect:

echo '' > tmp.txt; \
for((i = 1; ; i++)); do \
    awk '{for(i = 2; i <= NF; i++) {if (NR % 2 ) count[i] = $i; else if ($i > 0 ) print count[i], $i;}}' \
        /proc/net/snmp /proc/net/netstat >> tmp.txt; \
    if [ `expr $i % 2` == 0 ]; then \
        printf "\033c"; \
        awk '{if (a[$1]) {if ($2 != a[$1]) printf("%-28s%d\n", $1, $2 - a[$1])} else a[$1] = $2;}' tmp.txt; \
        echo '' > tmp.txt;\
    else \
        sleep 1;\
    fi; \
done

This command per output /proc/net/snmpand /proc/net/netstatfile value changes, the output format is as follows:

InReceives                  76931
InDelivers                  76931
OutRequests                 76931
InMsgs                      4
InEchos                     2
InEchoReps                  2
OutMsgs                     4
OutEchos                    2
OutEchoReps                 2
InType0                     2
InType8                     2
OutType0                    2
OutType8                    2
ActiveOpens                 200
PassiveOpens                166
EstabResets                 200
CurrEstab                   132
InSegs                      76927
OutSegs                     76927
OutRsts                     100
DelayedACKs                 1114
DelayedACKLocked            2
ListenOverflows             34
ListenDrops                 34
TCPHPHits                   15774
TCPPureAcks                 339
TCPHPAcks                   35402
TCPAbortOnClose             100
TCPOrigDataSent             41348
InOctets                    46293624
OutOctets                   46300484
InNoECTPkts                 77482

Guess you like

Origin blog.csdn.net/woay2008/article/details/86515913