awk help in calulating difference for last two lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk help in calulating difference for last two lines
# 1  
Old 05-26-2009
awk help in calulating difference for last two lines

Hi all,

I have a requirement for a script to find out the increase in memory. We have a log native_stderr.log where this will log.

bash-2.05$ tail -40 native_stderr.log | grep ': freed'
<GC(4140): freed 168190456 bytes, 66% free (180990488/271776256), in 253 ms>
<GC(4141): freed 167436384 bytes, 66% free (180290224/271776256), in 414 ms>
<GC(4142): freed 168588096 bytes, 66% free (181440728/271776256), in 352 ms>

Here i have put some more filter and able to get the values.

tail -40 native_stderr.log | grep ': freed' | awk -F '(' '{print $3}' | awk -F '/|)' '{print $1, $2}'
178893640 271776256
178153688 271776256
178595056 271776256

but i need to find out the differnce between the last line $1 value and the second last line value $1

like 178595056 - 178153688 = 441368 bytes
then change to MB 441368/(1024*1024) = 0.42

if this value is more than 50 MB then i need to send mail. Could you please some one help on this.

Thanks
Senthilkumar AK


-----Post Update-----

i have found some way but still your expert views needed on this

tail -40 native_stderr.log | grep ': freed' | awk -F '(' '{print $3}' | awk -F '/|)' '{a=($2-$1)/(1024*1024);y=x "\n" a; x=a};END{printf "%-15s%3.2f%-2s\n", "Difference is", x-y, " MB"}'

Thanks
Senthyilkumar

Last edited by senthilkumar_ak; 05-26-2009 at 06:12 PM..
# 2  
Old 05-27-2009
Quote:
Originally Posted by senthilkumar_ak
Hi all,
...
but i need to find out the differnce between the last line $1 value and the second last line value $1
Assuming the lines of interest are of the same format ( as posted ) and they're always present in the last 40 records of the log file, you can do something like this:

Code:
tail -40 logfile | awk -F'[(/)]' '/: freed/{ y=x; x=$4 }END{ print (x-y)/(1024*1024) " MB"}'


Quote:
...if this value is more than 50 MB then i need to send mail. Could you please some one help on this.
Something along the lines of ...

Code:
[ $(tail -40 logfile | awk -F'[(/)]' '/: freed/{ y=x; x=$4 }END{ print int((x-y)/(1024*1024)) }') -gt 50 ] && \
                                       mail -s "Attention" user@mail.com || echo OK

#the test expects integers only, ( bash )!

or calling the mail command with the system function in the END statement:

Code:
tail ... | awk '{...} END{ if((d=(x-y)/(1024*1024))>n) system("mail -s "Attention" user@mail.com"); else print d " MB" }' n=50

Modify it to your needs.
# 3  
Old 05-27-2009
Bug

Thanks very much rubin

but one query i have

bash-2.05$ tail -40 native_stderr.log | grep ': freed' | awk -F '(' '{print $3}' | awk -F '/|)' '{a=($2-$1)/(1024*1024);y=x "\n" a; x=a};END{printf "%-15s%3.2f%-2s\n", "Difference is", x-y, " MB"}'
Difference is 0.40 MB
bash-2.05$ tail -40 native_stderr.log | awk -F'[(/)]' '/: freed/{ y=x; x=$4 }END{ print (x-y)/(1024*1024) " MB"}'
-0.398109 MB

Your query give the result in negative.

Do we missed anything.
Thanks
Senthil kumar ak.
# 4  
Old 05-27-2009
Code:
tail -3 file  | awk '{ p=last ; last=$1}END{  print $1-p}'

# 5  
Old 05-27-2009
Quote:
Originally Posted by senthilkumar_ak
...
Your query give the result in negative.

Do we missed anything.
Thanks
Senthil kumar ak.
As per my understanding of your requirement, the code works as expected.


Quote:
Originally Posted by senthilkumar_ak
...but i need to find out the differnce between the last line $1 value and the second last line value $1
I see that you need to substract the second to the last line value ( in blue ) from the last line value ( in red ), disregarding entirely all the previous records, ( as I see it ).
My conclusions stemmed from your posted code, which points to those colored values ( btw there's a missmatch between your first and second tail in your first post, I used the first ). In absence of a good sample, I provided my own:

( Displaying only the last records )

Code:
$ tail logfile
....
<GC(4140): freed 168190456 bytes, 66% free (180990488/271776256), in 253 ms>
data other data other data
data other data other data
<GC(4141): freed 167436384 bytes, 66% free (180290224/271776256), in 414 ms>
data other data other data
<GC(4142): freed 168588096 bytes, 66% free (181440728/271776256), in 352 ms>
data other data other data


$ tail -40 logfile | awk -F'[(/)]' '/: freed/{ y=x; x=$4 }END{ print  "x->" x " y->" y " |difference in MB-> " (x-y)/(1024*1024) }'

x->181440728 y->180290224 |difference in MB-> 1.09721

I believe the main issue of extracting the last adjacent values is resolved, so little effort is needed modifying the code.
 
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

Calculate time difference between two lines

i grepped the time stamp in a file as given below now i need to calculate time difference file data: 18:29:10 22:15:50 (5 Replies)
Discussion started by: vivekn
5 Replies

3. Shell Programming and Scripting

What is difference between this two lines in script?

Hi Guys, What is difference between this two lines in script logger -p daemon.info -t postback Starting /opt/local/bin/backup-report and /opt/local/bin/backup-report is the backu script running twice here? Thanks, (2 Replies)
Discussion started by: manalisharmabe
2 Replies

4. Shell Programming and Scripting

Find difference in content between two particular lines in two files

I have two files named Before.txt and After.txt: Now i want to find the difference in content between <Marker 1> and <Marker 2> in the two files. ---------- Post updated at 05:00 PM ---------- Previous update was at 04:50 PM ---------- Any help will be highly appreciated..:) (3 Replies)
Discussion started by: proactiveaditya
3 Replies

5. Shell Programming and Scripting

Find time difference between two consecutive lines in same file.

Hello I have a file in following format: IV 08:09:07 NM 08:12:01 IC 08:12:00 MN 08:14:20 NM 08:14:15 I need a script to compare time on each line with previous line and show the inconsecutive line. Ex.: 08:12:00 08:14:15 A better way... (6 Replies)
Discussion started by: vilibit
6 Replies

6. Shell Programming and Scripting

difference between 1 st fields from consicutive lines

13608:End of Tests. 13811:End of Tests. 14014:End of Tests. 14217:End of Tests. 14420:End of Tests. 14623:End of Tests. 14826:End of Tests. 15029:End of Tests. 15232:End of Tests. 15435:End of Tests. 15638:End of Tests. i have file like above. i want difference betwwn first field of... (2 Replies)
Discussion started by: pankajd
2 Replies

7. Shell Programming and Scripting

Help to find the time difference between the lines

Hi guru's, Am new to shell scripting. I am getting the below o/p from the oracle database, when I fire a query. ID JOB_ID ELAPSED_TIME FROM TO ----- ------ ------------------- -------- -------- 62663 11773 01/06/2009 09:49:13 SA CM 62664 11773 ... (4 Replies)
Discussion started by: sathik
4 Replies

8. Shell Programming and Scripting

Compare two files and print the two lines with difference

I have two files like this: #FILE 1 ABCD 4322 26485 JMTJ 5311 97248 XMPJ 4321 58978 #FILE 2 ABCD 4321 26485 JMTJ 5311 97248 XMPJ 4321 68978 What to do: Compare the two files and find those lines that doesn't match. And have a new file like this: #FILE 3 "from file 1" ABCD 4322 26485... (11 Replies)
Discussion started by: kingpeejay
11 Replies

9. UNIX for Dummies Questions & Answers

calculate the difference between numbers in lines of file

Hi everyone, i have files containing lines of number: 109 107 67 62 .. .. i want to calculate the difference between numbers in lines 1 and 2, 3 and 4, 5 and 6 and so on. would someone help me please?. Thanks (12 Replies)
Discussion started by: ahidayat
12 Replies

10. Shell Programming and Scripting

Help with showing the difference in two lines of input

I would like my script to be able to tell the difference between to lines of input, like: Input 1: 1 2 3 4 5 Input 2: 1 2 3 4 5 6 I want the script to tell me that the difference between the two lines is the 6. Is there anyway I can have it do this? Here's an example of what my script... (12 Replies)
Discussion started by: Kweekwom
12 Replies
Login or Register to Ask a Question