awk to capture memory difference


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk to capture memory difference
# 1  
Old 10-19-2019
awk to capture memory difference

Hi,

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

Code:
************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?

Code:
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

# 2  
Old 10-19-2019
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
Code:
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

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-19-2019
Hi RudiC,

Noted. Your code works well, thank you.

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

Code:
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.

Code:
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")

Code:
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.
# 4  
Old 10-19-2019
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):

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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Difference in awk output and while

so, im going over one of my scripts and trying to optimize it. i have a code like this: cksum sjreas.py | awk '{prinnt $1$2}' This does what I need. However, i dont want to call the external command awk. so im doing this: cksum sjreas.py | while OFS=' ' read v1 v2 ; do printf... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Awk: What is the difference between: X[a,b,c] - X[a][b,c] - X[a][b][c]

I have awk appearing to behave inconsistently. With the same variable it will give the message: fatal: attempt to use array `X' in a scalar context and, if I try to correct that, then: fatal: attempt to use a scalar value as array I'm using a three dimensional array. There seems to be a... (2 Replies)
Discussion started by: Fustbariclation
2 Replies

4. Shell Programming and Scripting

awk to capture lines that meet either condition

I am trying to modify and understand an awk written by @Scrutinizer The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI=,0.95: portion in blue in file is <1.9. The... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. UNIX for Dummies Questions & Answers

What is the difference in this two awk command?

What is the difference in these two awk command? Both returns same output but I am not sure what is the use of +0 in command 1. awk -F "," '{print $1+0,$2+0,$3+0}' awk -F "," '{print $1, $2, $3}' (3 Replies)
Discussion started by: later_troy
3 Replies

6. Shell Programming and Scripting

Awk:String search more than one time and capture OP

Dear All During one of mine script developemnt i am stuch at one sub part. Requiremnt is as below kindly help me. IP file: 2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001X (unavailable) 2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001Y (unavailable) 2015-02-28 10:10:15 AL M... (6 Replies)
Discussion started by: jaydeep_sadaria
6 Replies

7. Shell Programming and Scripting

awk help string capture

Dear All My input file as under. From it I want op fine as mention below. Kindly help. I use below code but not help. code: awk -F" " '{print $2}' ip file: "BSCJNGR_IPA17_C" 030 131207 1305 RXOCF-353 PBD011_BGIL BOTH AC FAULTY "BSCJNGR_IPA17_C" 991 131207 1637 RXOCF-224 NAV001_BGIL ... (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

8. Shell Programming and Scripting

AWK Script to Capture Each Line of File As Variable

Hi All, I'm working on creating a parts database. I currently have access to a manufacturer database in HTML and am working on moving all of the data into a MySQL db. I have created a sed script that strips out the HTML and unnecessary info and separates the script into one line for each field.... (3 Replies)
Discussion started by: dkr
3 Replies

9. Shell Programming and Scripting

gsub in Awk to capture count of replaced characters

Hi , I am working on a script to replace special characters in ASCII file with '?'. We need to get count of replaced characters from file. I am new to Awk and i read, # The gsub function returns the number of substitutions made. I was trying to replace characters with below... (10 Replies)
Discussion started by: Akshay
10 Replies

10. Solaris

relationship or difference between entitled memory and locked memory

Hello solaris experts, Being new to solaris containers, from Linux, feeling difficulty in understanding certain concepts. Hope somebody can help me here. I understand that, & some questions .... Locked memory -- memory which will not be swapped out at any cause. is this for... (0 Replies)
Discussion started by: thegeek
0 Replies
Login or Register to Ask a Question