awk question in relation to finding "top 3" by group.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk question in relation to finding "top 3" by group.
# 1  
Old 08-01-2019
awk question in relation to finding "top 3" by group.

Hi,
I have a file that contains 3 key element fields with the fourth being a total value, something along the lines of this "Site,Region,Town,Total" . What I need to be able to do is find out the top 3 totals by Site using an AWK program. In my efforts so far I have managed to create either a max value by site (using max) or a top three irrespective of site (using a for loop). I realise this needs to be done with arrays but I am having difficulty getting that correct for this particular problem. Is there anybody who could help shed some light on to this please? I should add that I need to be able to see all the key element fields in the output.

Many thanks
# 2  
Old 08-01-2019
# input file is csv?

# just awk?, try:
Code:
awk -F, '
{
   sites[$1]=$1;
   for (i=3; i>=1; i--) {
      if ($4 >= max3[i,$1]) {
         for (j=1; j<i; j++) {
            max3[j,$1]=max3[j+1,$1]; top3[j,$1]=top3[j+1,$1];
         }
         max3[i,$1]=$4; top3[i,$1]=$0;
         break;
      }
   }
}
END {
   for (i in sites) {
      for (j=3; j>=1; j--) print top3[j,sites[i]];
   }
}
' input_file

# using other tools
Code:
sort -t, -nrk1,4 input_file | awk -F, 'site[$1]++ < 3'

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-01-2019
Thank you so much for that AWK program that does exactly what I needed. My first efforts resulted in top three but not specifically for each of the sites and my second attempt only produced a result for each site but only a single maximum value. I realised I needed to somehow marry the two together but I couldn't get my head around the syntax for the arrays - I really appreciate your help - many thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

2. Shell Programming and Scripting

awk top not getting the "k" result

Hi all, I have the script : top -n 1 | awk '{ if (NR==4) print $5 }' It will return me with one memory usage : for instance 3134720k However, I need to be returned only the number : 3134720 Does anyone knows how to fix the script that return the number (without k) thank you (3 Replies)
Discussion started by: peuceul
3 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Solaris

Relation btw commands, "man" and "more" ???

Hi guys, Hope u r doing find. I have this query. When we check the manual pages for a certain command, say man cat we see the manual page with more What is UNIX really doing here, I mean why not less command instead of more command. And can we have UNIX display the manual pages with less command... (2 Replies)
Discussion started by: gabam
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. AIX

AIX 5.3 - Discrepancies between "top" and "vmstat"

Can someone explain the differences I'm seeing below in TOP and VMSTAT commands on my AIX 5.3 server? Thanks! CPUs: 4; load averages: 0.86, 0.97, 0.97 18:09:26 926 processes: 4 stopped, 922 running CPU states: 78.4% idle, 8.5% user, 12.6% kernel, 0.3% wait Memory: 23680M Total.... (1 Reply)
Discussion started by: troym72
1 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies

10. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies
Login or Register to Ask a Question