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 :

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.

Like this?

awk '{printf "%s %10.0f MB %s\n", $1, ($3-p)/1024, $NF}{p=$3}' file
1 Like

try:

awk '{a[NR]=$1; b[NR]=$3; c[NR]=$NF} END {for (i=1; i<NR; i++) printf("%s %8d MB %s\n", a, (b[i+1]-b)/1024, c)} {p=$3}' infile
1 Like

Thanks both,
I got different output with Scrutinizer's code , and first entry showing way too much difference in the output ,

#  =>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
#  =>awk '{a[NR]=$1; b[NR]=$3; c[NR]=$NF} END {for (i=1; i<NR; i++) printf("%s %8d MB %s\n", a, (b[i+1]-b)/1024, c)} {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%
#  =>

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:

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?

awk 'NR>1{printf "%s %10.0f MB %s\n", f1, ($3-f2)/1024, f3}{f1=$1; f2=$3; f3=$NF}' file
1 Like