Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] calculating average segment after segment using AWK
 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

 03-20-2012, 06:55 AM #1 vjramana Member   Registered: Sep 2009 Posts: 89 Rep: 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
 03-20-2012, 07:23 AM #2 druuna LQ Veteran   Registered: Sep 2003 Posts: 10,532 Blog Entries: 7 Rep: 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.
 03-21-2012, 01:08 AM #3 vjramana Member   Registered: Sep 2009 Posts: 89 Original Poster Rep: HI, Thank you so much for your kind help. Regards Vijay

 Tags awk

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post xiewanchen Linux - Newbie 2 01-13-2012 07:12 PM vjramana Programming 6 04-21-2011 09:36 AM newbieMhke Programming 0 06-06-2006 01:13 AM melodious Fedora - Installation 1 10-25-2004 10:35 PM Dumpsterm0uth Linux - General 9 08-03-2003 03:19 PM

LinuxQuestions.org

All times are GMT -5. The time now is 11:28 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -