awk to capture memory difference

Hi,

I have a file which consists of the following information in repeating blocks.

************First iteration***************

xr_lab#show memory compare start
Thu Sep 19 14:38:06.400 WIB   <<<<<<<<<< START TIME
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_start.out
xr_lab#
xr_lab#
xr_lab#show memory compare end
Thu Sep 19 14:40:56.123 WIB   <<<<<<<<<< END TIME
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_end.out
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
-------------------------------------------------------------------------------

2550   sysdb_svr_local          7881443     7878256     -3187       87391
7582   mibd_interface           8670334     8484152     -186182     267657

**********Second iteration************

xr_lab#show memory compare start
Thu Sep 19 14:43:07.946 WIB   <<<<<<<<<< START TIME
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_start.out
xr_lab#
xr_lab#
xr_lab#
xr_lab#show memory compare end
Thu Sep 19 14:45:27.916 WIB   <<<<<<<<<< END TIME
Successfully stored memory snapshot in /var/log/malloc_dump_memcmp_end.out
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

I would like to capture the {start time, mem_before} in one column, and {end time, mem_after} in another column for mibd_interface.

The logic I need looks something like this. Can you please assist to develop it further?

awk '/show memory compare start/{
             getline; start_time=$0;
  }
     /show memory compare end/{
             getline; end_time=$0;
  }

if(start_time); /mibd_interface/print $3
if(end_time); /mibd_interface/print $4

Try posting runnable, syntax error free code only so people can concentrate on the logical problems. I won't comment on the obvious errors in your incomplete code snippet above. How about

awk '
/show memory compare start/     {getline
                                 start_time = $0;
                                }
/show memory compare end/       {getline
                                 end_time = $0;
                                }

start_time &&
end_time   &&
/mibd_interface/                {print start_time, $3, end_time, $4
                                 start_time = end_time = ""
                                }
' OFS="\t" file
Thu Sep 19 14:38:06.400 WIB    8670334    Thu Sep 19 14:40:56.123 WIB    8484152
Thu Sep 19 14:43:07.946 WIB    8369050    Thu Sep 19 14:45:27.916 WIB    8514825
1 Like

Hi RudiC,

Noted. Your code works well, thank you.

BTW, this is what I had before, which was not working.

awk '/show memory compare start/{
        getline; print

}

/mibd_interface/{print $3}

/show memory compare end/{
        getline; print
}

/mibd_interface/{print $4}' file.txt 

For some reason, the order of printing was incorrect. I was expecting start_date, $3, end_date, $4, but instead received start_date, end_date, $3, $4.

Thu Sep 19 14:38:06.400 WIB
Thu Sep 19 14:40:56.123 WIB
8670334
8484152
Thu Sep 19 14:43:07.946 WIB
Thu Sep 19 14:45:27.916 WIB
8369050
8514825
Thu Sep 19 14:46:28.464 WIB
Thu Sep 19 14:50:10.422 WIB
8446906
8264885
Thu Sep 19 14:50:44.374 WIB
Thu Sep 19 14:55:05.760 WIB
8264884
8264960

This is the output from your code which is what is needed here. (I changed OFS to "\n")

Thu Sep 19 14:38:06.400 WIB
8670334
Thu Sep 19 14:40:56.123 WIB
8484152
Thu Sep 19 14:43:07.946 WIB
8369050
Thu Sep 19 14:45:27.916 WIB
8514825
Thu Sep 19 14:46:28.464 WIB
8446906
Thu Sep 19 14:50:10.422 WIB
8264885
Thu Sep 19 14:50:44.374 WIB
8264884
Thu Sep 19 14:55:05.760 WIB
8264960

Thanks.

Didn't you want "the {start time, mem_before} in one column, and {end time, mem_after} in another column" ?

Try also this slightly condensed version (that assumes there is a start time when there is an end time):

awk '
/show memory compare/   {getline TIME[$4]
                        }

TIME["end"]      &&
/mibd_interface/                {print TIME["start"], $3
                                 print TIME["end"], $4
                                 split ("", TIME)
                                }
' OFS="\t" file