Avg calculation for top 5 records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avg calculation for top 5 records
# 1  
Old 06-24-2015
Avg calculation for top 5 records

Hi, we have a file which contains Area, Country & Rank fields.

HTML Code:
AREA,COUNTRY,RANK
A,MX,1
A,MX,2
A,MX,4
A,MX,6
A,MX,3
A,MX,8
A,IN,7
A,IN,5
A,IN,2
B,CN,6
B,CN,2
B,CN,7
B,CN,0
--------
--------
Have to calculate average of RANK based on area, country. Below are the conditions to calculate avg.

1. If a (Area & Country combination) count >= 5 then, take avg of top 5 ranks. In above ex: for A, MX combination, top 5 ranks are (8,6,4,3,2) and its avg is 4.6

2. If (Area & Country combination) count < 5 then, avg of rank is calculated as usual. In above file, for A, IN combination, avg is (7+5+2)/3. For B,CN combination, avg=(6+2+7+0)/4

HTML Code:
A,MX,4.6
A,IN,4.6
B,CN,3.75
im able to sort the data based on Area, country and Rank using
Code:
sort -t"," -k1,2 -k3n,3 file

, but stuck to get logic to calculate avg. plz help me out

Last edited by JSKOBS; 06-24-2015 at 05:13 AM..
# 2  
Old 06-24-2015
You might be interested in reading this. The article describes your problem (and its solution) almost to the letter, you just need to adapt for the minimum number of records.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Sybase ASE - AVG Function Error

Hi Team - I am using Sybase ASE 15.7 version. Below query is throwing an error stating Error : incorrect syntax near the keyword 'OVER' SELECT EMPLOYEE_ID , EMPLOYEE , Department, CAST( Salary as DECIMAL( 10, 2 ) ) AS Salary CAST(AVG( Salary) OVER ( PARTITION... (3 Replies)
Discussion started by: Perlbaby
3 Replies

2. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

3. Shell Programming and Scripting

Extract top 20 records from sybase db in shell script

Hi, I am new to advanced scripting. I need to get top 20 records from database (Sybase) with a condition that all Char/varchar columns should have more than 4 characters. I need to do this via shell scripting, I have got half logic (on how to get varchar/char columns and stored it in a... (2 Replies)
Discussion started by: Selva_2507
2 Replies

4. AIX

Nmon max and avg for cpu and memory

Hi All, Anyone know how to capture the nmon avg and max cpu and memory for one of the AIX server for Monthly Utilization Report purposes ? Thanks. ---------- Post updated at 05:18 AM ---------- Previous update was at 05:07 AM ---------- if possible use shell script to count or sum... (6 Replies)
Discussion started by: ckwan
6 Replies

5. Shell Programming and Scripting

Avg using awk

Coins: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (3 Replies)
Discussion started by: Ramesh M
3 Replies

6. Shell Programming and Scripting

Extracting avg latency value from ping output

Hello Everyone, Below is the output of the ping from a router. Please help with a script which extract the Avg value from the o/p (Avg here = 4, as depicted below) and put the value into a new file. Will appreciate your help dearly Router#ping 36.36.36.36 Type escape sequence to abort.... (2 Replies)
Discussion started by: sanjugab
2 Replies

7. Shell Programming and Scripting

Select only top "N" records based on column value

Hi Gurus, I know this'll be simple task for all the geeks out here but me being a newbie is finding it hard to crack this shell. Ok coming to the task I've a delimited file as below ================================================== ==================================================== ... (8 Replies)
Discussion started by: asandy1234
8 Replies

8. Shell Programming and Scripting

Find avg using awk

Hi, i need some help plz... The file data.txt contains: code of student,surname and name,code of lesson,grade of lesson.The number of lessons of each student is not the same. 25,Jackson Steve,12,4,34,2,65,2 29,Jordan Mary,13,6,23,8,56,4,34,2 04,Leven Kate,14,6,15,6,26,4 34,Owen... (10 Replies)
Discussion started by: Steve_09
10 Replies

9. UNIX for Advanced & Expert Users

ps avg | grep ? filter the desired out put.

Hi Folk, Following is the command I used to get data related to the DataFlowEngine. I wanted to know the % usage of cpu and memory. ps avg | grep Data This command will show the processes with its PID as : PID TTY STAT TIME PGIN SIZE RSS LIM TSIZ TRS %CPU %MEM COMMAND ... (1 Reply)
Discussion started by: varungupta
1 Replies

10. SuSE

sles 9 - sudden high load avg

Hi Running SLES 9(4) on PE 1950. I saw yesterday that the load average on the machine was 54 and keeping around that number. Later I found there were 54 /USR/SBIN/CRON processes running in the system. I tried to kill using killall, kill -9 pid but they did not get killed. I also tried stopping... (1 Reply)
Discussion started by: upengan78
1 Replies
Login or Register to Ask a Question