LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2012, 06:55 AM   #1
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Rep: Reputation: 0
calculating average segment after segment using AWK


Dear Experts,

I would like to calculate average over few hundred thousands lines of data. I would like to use awk to that but I just could not figure out how the syntax to this. The sample data as below:

Code:
           5      31.879999    
           5      27.227272    
           5      32.049999    
           5      33.000000    
           5      37.000000    
           5      35.500000    
           5      43.900002    
           5      39.285713    
           5      43.400002    
           5      21.000000    
          15      39.333332    
          15      33.818180    
          15      27.400000    
          15      28.125000    
          15      47.857143    
          15      41.333332    
          15      33.200001    
          15      34.666668    
          15      60.000000    
          15      10.000000    
          25      49.250000    
          25      15.000000    
          25      13.666667    
          25      31.000000    
          25      49.000000    
          25      55.000000    
          25      60.333332    
          25      31.000000    
          25      48.000000    
          35      29.166666    
          35      43.599998    
          35      32.200001    
          35      27.750000    
          35      40.666668    
          35      37.000000    
          35      39.000000    
          35      59.000000    
          35      61.000000    
          45      36.400002    
          45      27.500000    
          45      37.000000    
          45      29.666666    
          45      47.000000    
          45      25.000000    
          45      38.500000    
          45      40.000000    
          45      44.000000    
          55      56.500000    
          55      18.333334    
          55      10.666667    
          55      19.000000    
          55      70.500000    
          55      39.500000
I would like to calculate the average for numbers in second column corresponding to number 5 in first column. Then average for numbers in second column corresponding to number 15 in first column. And so on.

at the end I want final data to be

Code:
5    average
15   average
25   average
35   average
.
.
.

Appreciate any help in advance.
Thank you
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-20-2012, 07:23 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403
Hi,

Try this:
Code:
awk '{ a[$1] += $2 ; b[$1]++ } END { for ( cnt in a) { print cnt , "\t",  a[cnt]/b[cnt] } }' input | sort -n
Example run with data given in post #1:
Code:
$ awk '{ a[$1] += $2 ; b[$1]++ } END { for ( cnt in a) { print cnt , "\t",  a[cnt]/b[cnt] } }' input | sort -n
5        34.4243
15       35.5734
25       39.1389
35       41.0426
45       36.1185
55       35.75
Hope this helps.
 
2 members found this post helpful.
Old 03-21-2012, 01:08 AM   #3
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Original Poster
Rep: Reputation: 0
HI,

Thank you so much for your kind help.

Regards

Vijay
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
segment selector xiewanchen Linux - Newbie 2 01-13-2012 07:12 PM
[SOLVED] awk syntax for calculating average and standard deviation vjramana Programming 6 04-21-2011 09:36 AM
one segment newbieMhke Programming 0 06-06-2006 01:13 AM
segment fault? melodious Fedora - Installation 1 10-25-2004 10:35 PM
What is a segment fault Dumpsterm0uth Linux - General 9 08-03-2003 03:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration