Assistance with awk/bash to capture memory difference

落花浮王杯 提交于 2020-04-15 03:48:28

问题


I am trying to extract the following output from the following file:

xr_lab#   show clock
Thu Sep 19 14:38:02.812 WIB
14:38:02.893 WIB Thu Sep 19 2019
xr_lab#
xr_lab#
xr_lab#show memory compare report
Thu Sep 19 14:41:08.084 WIB

PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------

6777   ospf                     24292985    24293753    768         272634  
7582   mibd_interface           8670334     8484152     -186182     267657      


xr_lab#show clock
Thu Sep 19 14:42:42.425 WIB
14:42:42.497 WIB Thu Sep 19 2019
xr_lab#  
xr_lab#
xr_lab#show memory compare report
Thu Sep 19 14:45:42.091 WIB

PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------

6777   ospf                     24294569    24283592    -10977      227389              
7582   mibd_interface           8369050     8514825     145775      126259      

DESIRED OUTPUT

PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582   mibd_interface           8670334     8484152     -186182     267657
7582   mibd_interface           8369050     8514825     145775      126259
7582   mibd_interface           8446906     8264885     -182021     322280
7582   mibd_interface           8264884     8264960     76          284409
-------------------------------------------------------------------------------
                                                        -222352   <time difference>

Here is what I have so far.

$ awk '/PID/{print;getline;print;exit} /mibd_interface/' snmpoutput.txt
PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582   mibd_interface           8670334     8484152     -186182     267657
PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582   mibd_interface           8369050     8514825     145775      126259
PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582   mibd_interface           8446906     8264885     -182021     322280
PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582   mibd_interface           8264884     8264960     76          284409

$ awk '/show clock/{getline;print}' snmpoutput.txt
Thu Sep 19 14:38:02.812 WIB   <<<< capture this line
Thu Sep 19 14:42:42.425 WIB
Thu Sep 19 14:46:15.895 WIB
Thu Sep 19 14:50:44.213 WIB   <<<< capture this line

$  awk '/mibd_interface/{x+=$5} END {print x}' snmpoutput.txt
-222352

I would like some guidance on how to join these parts together. Not necessarily looking for a solution, but assistance around the logic.

Thanks.


回答1:


awk is a great language:

awk '
   /PID/{
       # store the line with PID NAME BLA BLA
       pidline=$0;
       # alse remember the line with ----------------------- minuses
       getline;
       minusline=$0
   }
   /mibd_interface/{
         # remember the line with mibd_interface
         mibdlines[mibdlen++]=$0;
         # calculate some-things
         diff += $5;
         timediff += $6
  }
  # finally output!
  END{ 
      # output the PID NAME ...
      print pidline; 
      # output ------------
      print minusline; 
      # output the mibd_interface lines
      for (i in mibdlines) print mibdlines[i]; 
      # another ------------ line
      print minusline; 
      # and output the difference, printf is there for a  simple formatting
      printf "%56s%-12d%-d\n"," ",diff,timediff
}'

or a oneliner:

awk '/PID/{pidline=$0;getline;minusline=$0} /mibd_interface/{mibdlines[mibdlen++]=$0; diff += $5; timediff += $6} END{ print pidline; print minusline; for (i in mibdlines) print mibdlines[i]; print minusline; printf "%56s%-12d%-d\n"," ",diff,timediff}'

when feed the input outputs:

PID    NAME                     MEM BEFORE    MEM AFTER  DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582   mibd_interface           8670334     8484152     -186182     267657      
7582   mibd_interface           8369050     8514825     145775      126259      
7582   mibd_interface           8446906     8264885     -182021     322280      
7582   mibd_interface           8264884     8264960     76          284409      
-------------------------------------------------------------------------------
                                                        -222352     1000605

On the second try there is no need to store the lines with mbd_interface, so we can just output them as they come:

awk '
    /PID/{
       if(once == 0) {
          #output the PID NAME line
          print;
          # output and remember the -------------- line
          getline;
          minusline=$0;
          print;
       }
       once=1
    }
    /mibd_interface/{
        # output the mibd line
        print;
        # calculate some-things
        diff += $5; timediff += $6
    }
    END{
        # print another minus line
       print minusline;
       # print the calculated some-things
       printf "%56s%-12d%-d\n"," ",diff,timediff
    }'

which outputs the same. Or a oneliner:

awk '/PID/{if(once == 0) { print; getline; minusline=$0; print; } once=1 } /mibd_interface/{print; diff += $5; timediff += $6} END{ print minusline; printf "%56s%-12d%-d\n"," ",diff,timediff}'



回答2:


Could you please try following(I am on mobile as of now do couldn't test it, should work but)

awk '/^PID/{found=1;if(++count==1){print};next} /^-/ && found{print;next} /^[0-9]/ && found && /mibd_interface/; /mibd_interface/{diff+=$5;mallocs+=$6} END{print "Total diff=" diff ORS "Total mallocs=" mallocs}' Input_file



来源:https://stackoverflow.com/questions/58200564/assistance-with-awk-bash-to-capture-memory-difference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!