Visit Our UNIX and Linux User Community


AWK way of calculating growth


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK way of calculating growth
# 1  
Old 02-20-2012
AWK way of calculating growth

Hi All,
IS there any 'awk' way to manipulate following data?

Fruit Date Count
Apple 20/08/2011 5
Apple 27/08/2011 7
Apple 05/09/2011 11
Apple 12/09/2011 3
Apple 19/09/2011 25
.
.
.
.
Orange 20/08/2011 9
Orange 27/08/2011 20
Orange 27/08/2011 7
Orange 05/09/2011 15
Orange 12/09/2011 24
.
.
.
And so on.

I need to manipulation on above data-
Fruit Date Count Difference
Apple 20/08/2011 5 0
Apple 27/08/2011 7 2
Apple 05/09/2011 11 4
Apple 12/09/2011 3 -8
Apple 19/09/2011 25 22
.
.
.
.
Orange 20/08/2011 9 0
Orange 27/08/2011 20 11
Orange 27/08/2011 7 -13
Orange 05/09/2011 15 8
Orange 12/09/2011 21 6
.
.
.
.
And also to count growth in count of fruits excluding negative values of differences-
Fruit Growth_In_Count
Apple 7
Orange 6.25
.
.
.

Thanks in advance.
# 2  
Old 02-20-2012
aniketdixit, is this some sort of homework?
# 3  
Old 02-20-2012
No sir. I am able to do that with floowing -
Code:
nawk '{c[$1]++;s[$1]+=$2}END{ for(i in c) printf("%s %.5f\n", i, s[i]/c[i])}'

but thing is that it calculates differnce from top to bootom for particular thing. I need to do it in reverse. And also not able to exclude negative values.

Last edited by Franklin52; 02-20-2012 at 04:47 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 02-20-2012
awk

Hi,

Anyway !!!
Try this below code.
Code:
awk '{if ( NR == 1 ){print $0,"0";}else{result=$3-prev;print $0,result;}if( result > 0 ){a[$1] = a[$1] + result;b[$1]=$1;c[$1]=c[$1]+1;}prev=$3;}END{for(i in a){v=a[i];d=c[i];res=v/d;print i,res;}}' Input_File

Cheers,
RangaSmilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #752
Difficulty: Medium
BIOS is an acronym for Bootstrap Input/Output System.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculating correlations across columns in awk

Hello, I came across a previous thread "awk-calculating-simple-correlation-rows" which calculated correlations across rows in awk. Code: awk '{ a = 0; for (i = 2; i <= NF; ++i) a += $i; a /= NF-1 b = 0; for (i = 2; i <= NF; ++i) b += ($i - a) ^ 2; b = sqrt(b) if... (7 Replies)
Discussion started by: Ross
7 Replies

2. UNIX for Dummies Questions & Answers

Calculating cumulative frequency using awk

Hi, I wanted to calculate cumulative frequency distribution of my data that involves several arithmetic calls. I did things in excel but its taking me forever. this is what I want to do: var1.txt contains n observations which I have to compute for frequency which is given by 1/n and subsequently... (7 Replies)
Discussion started by: ida1215
7 Replies

3. Shell Programming and Scripting

Calculating Running Variance Using Awk

Hi all, I am attempting to calculate a running variance for a file containing a column of numbers. I am using the formula variance=sum((x-mean(x))^2)/(n-1), where x is the value on the current row, and mean(x) is the average of all of the values up until that row. n represents the total number... (1 Reply)
Discussion started by: Jahn
1 Replies

4. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

Calculating an integer with awk

I would like to extract a number from $0 and calculate if it can be devided by 25. Though the number can also be less then 25 or bigger than 100. How do i extract the number and how can the integer be calculated? String: "all_results">39</span>I am looking for the number between "all_results"> ... (5 Replies)
Discussion started by: sdf
5 Replies

7. Shell Programming and Scripting

[Solved] awk calculating between lines

Hey guys, maybe you can help me with this... I want to read input.dat line by line, while doing a simple calculation between the second column value of the current line and the second column value of the next line (like a difference). input is something like this: 0 3.945757 1 ... (1 Reply)
Discussion started by: origamisven
1 Replies

8. Shell Programming and Scripting

reading and calculating no. of occurrence in awk

:confused:Hi, I have got results from my simulation and I did extract the time field from the results file into new file. Then I was trying to calculate no. of occurrence for each set of time. However, I couldn't. For example : 0.2 -- 0.2 -- 0.21016 -- 0.21016 -- 0.21016 -- . . .... (12 Replies)
Discussion started by: ENG_MOHD
12 Replies

9. Shell Programming and Scripting

Calculating totals in AWK

Hello, With the following small script I list the size of documents belonging to a certain user by each time selecting the bytes-field of that file ($7). Now it fills the array with every file it finds so in the end the output of some users contains up to 200.000 numbers. So how can I calculate... (7 Replies)
Discussion started by: Hille
7 Replies

10. Filesystems, Disks and Memory

calculating disk space for growth

what's the best way to calculate the raw disk space in format. the system is a Solaris system using EMC disks- df -k will give me what's used plus available.Now I need the total disk space that the system is using/assigned. Which means I must inventory the raw disks in format. the system uses... (6 Replies)
Discussion started by: sholiver
6 Replies

Featured Tech Videos