How to calculate difference:?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate difference:?
# 1  
Old 07-23-2013
How to calculate difference:?

Experts,

file1 : Want to find the difference of $3 field from next line's 3rd field,


The difference to be calculated from next lines 3rd field, to current lines lines 3rd field.


file1 :
Code:
Jun24_2013.06242013     3301244928 3133059904 167370640   95%
Jun25_1124.06252013     3301244928 3145497448 154988472   95%
Jun26_1016.06262013     3301244928 3157360376 143177152   96%
Jun27_1112.06272013     3301244928 3144037776 156356096   95%
Jun27_1714.06272013     3301244928 3144066576 156327520   95%
Jun28_1036.06282013     3301244928 3155272808 145171224   96%
Jun28_1114.06282013     3301244928 3155276936 145167128   96%
Jul01_1105.07012013     3301244928 3100088440 200823992   94%
Jul02_1033.07022013     3301244928 3112573560 188395312   94%
Jul03_1015.07032013     3301244928 3124571632 176449880   95%
Jul04_1404.07042013     3301244928 3136850640 164224552   95%
Jul05_1036.07052013     3301244928 3136862344 164212944   95%
Jul08_1031.07082013     3301244928 3149128144 152001000   95%
Jul09_1055.07092013     3301244928 3161187472 139993752   96%
Jul10_1006.07102013     3301244928 3172878672 128354128   96%


The desired output should be :
<diff-value>/1024 "MB"

Jun24_2013.06242013 xxMB 95% # Where xx is the calculated value


please advise,
Thanks.
# 2  
Old 07-23-2013
Like this?
Code:
awk '{printf "%s %10.0f MB %s\n", $1, ($3-p)/1024, $NF}{p=$3}' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-23-2013
try:
Code:
awk '{a[NR]=$1; b[NR]=$3; c[NR]=$NF} END {for (i=1; i<NR; i++) printf("%s %8d MB %s\n", a[i], (b[i+1]-b[i])/1024, c[i])} {p=$3}' infile

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 07-23-2013
Thanks both,
I got different output with Scrutinizer's code , and first entry showing way too much difference in the output ,

Code:
#  =>awk '{printf "%s %10.0f MB %s\n", $1, ($3-p)/1024, $NF}{p=$3}' file
Jun24_2013.06242013    3059629 MB 95%
Jun25_1124.06252013      12146 MB 95%
Jun26_1016.06262013      11585 MB 96%
Jun27_1112.06272013     -13010 MB 95%
Jun27_1714.06272013         28 MB 95%
Jun28_1036.06282013      10944 MB 96%
Jun28_1114.06282013          4 MB 96%
Jul01_1105.07012013     -53895 MB 94%
Jul02_1033.07022013      12192 MB 94%
Jul03_1015.07032013      11717 MB 95%
Jul04_1404.07042013      11991 MB 95%
Jul05_1036.07052013         11 MB 95%
Jul08_1031.07082013      11978 MB 95%
Jul09_1055.07092013      11777 MB 96%
Jul10_1006.07102013      11417 MB 96%
#


- rdrtx1
Code:
#  =>awk '{a[NR]=$1; b[NR]=$3; c[NR]=$NF} END {for (i=1; i<NR; i++) printf("%s %8d MB %s\n", a[i], (b[i+1]-b[i])/1024, c[i])} {p=$3}' file
Jun24_2013.06242013    12146 MB 95%
Jun25_1124.06252013    11584 MB 95%
Jun26_1016.06262013   -13010 MB 96%
Jun27_1112.06272013       28 MB 95%
Jun27_1714.06272013    10943 MB 95%
Jun28_1036.06282013        4 MB 96%
Jun28_1114.06282013   -53895 MB 96%
Jul01_1105.07012013    12192 MB 94%
Jul02_1033.07022013    11716 MB 94%
Jul03_1015.07032013    11991 MB 95%
Jul04_1404.07042013       11 MB 95%
Jul05_1036.07052013    11978 MB 95%
Jul08_1031.07082013    11776 MB 95%
Jul09_1055.07092013    11417 MB 96%
#  =>

# 5  
Old 07-23-2013
Yes that is the original value. I left the first line in so it would be clear in relation to what value the differences in next lines would be... It can be easily left out:
Code:
awk 'NR>1{printf "%s %10.0f MB %s\n", $1, ($3-p)/1024, $NF}{p=$3}' file

or did you mean the other way around?
Code:
awk 'NR>1{printf "%s %10.0f MB %s\n", f1, ($3-f2)/1024, f3}{f1=$1; f2=$3; f3=$NF}' file

This User Gave Thanks to Scrutinizer For This Post:
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 percentage difference between two columns

I have a input text file in this format: ITEM1 10.9 20.1 ITEM2 11.6 12 ITEM3 14 15.7 ITEM5 20 50.6 ITEM6 25 23.6 I want to print those lines which have more than 5% difference between second and third columns. (8 Replies)
Discussion started by: ctrld
8 Replies

3. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

4. Shell Programming and Scripting

Calculate date difference

Hi All Can you please help me with UNIX script code that will work with ksh shell on UNIX server My Requirement Time1: 09/17/13101536 Time2: 09/16/13101536 I want to calculate the difference in minutes for the dates with format given above. I have a requirement to wait for... (5 Replies)
Discussion started by: jimmyb
5 Replies

5. Shell Programming and Scripting

How to Calculate the difference between two dates?

I want the difference between two following date using scripts in terms of no.of days. How I can accomplish this. lastdate=Tue Nov 13 10:30:56 2012 currdate=Wed Dec 15 15:58:21 PAKST 2012 Ouput should be like this: Your Password will expire after = 32 Days on Wed Dec 15 15:58:21 PAKST... (1 Reply)
Discussion started by: m_raheelahmed
1 Replies

6. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

7. Shell Programming and Scripting

How to calculate the difference between two adjacent columns?

Dear All, I need to find the difference between two adjacent columns. The file is having 'i' columns and i need to find the difference between two adjacent columns (like $1 difference $2; $2 difference $3; .... and $(i-1) difference $i). I have used the following coding awk '{ for (i=1; i<NF;... (7 Replies)
Discussion started by: Fredrick
7 Replies

8. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies

10. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies
Login or Register to Ask a Question