Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

HHow to print the group with a highest value within a set

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-12-2017
quincyjones quincyjones is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 10 July 2017, 5:54 AM EDT
Posts: 184
Thanks: 53
Thanked 0 Times in 0 Posts
HHow to print the group with a highest value within a set

How to print the names with a highest value within a set and filter if it is the only unique group within the same set

input


Code:
sets    names    value    groups
j007    shot1    0.6    a
j007    shot2    0.5    b
j007    shot3    0.4    bb
j007    shot4    0.3    bc
j007    shot5    0.2    cd
j008    shot1    0.4    a
j008    shot2    0.3    ab
j009    shot1    0.14    a
j009    shot2    0.13    b
j009    shot3    0.12    bc
j010    shot1    22    a
j010    shot2    19    b
j010    shot3    5    bcd
j011    shot1    5    a
j011    shot2    2    b
j011    shot3    3    c

output


Code:
j007    shot1    0.6    a
j009   shot1   0.14 a
j010    shot1    22    a

Tried

Code:
sort -k 3,3 input | awk '$3*$3>A[$1]*A[$1]{A[$1]=$0} END{for(i in A) print i,A[i]}'  | sort -k 1,1


Last edited by quincyjones; 06-12-2017 at 04:00 PM..
Sponsored Links
    #2  
Old Unix and Linux 06-12-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 19 October 2017, 2:34 PM EDT
Location: Houston, Texas, USA
Posts: 971
Thanks: 0
Thanked 332 Times in 314 Posts
Clarification added.

Last edited by rdrtx1; 06-13-2017 at 05:31 PM..
Sponsored Links
    #3  
Old Unix and Linux 06-12-2017
quincyjones quincyjones is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 10 July 2017, 5:54 AM EDT
Posts: 184
Thanks: 53
Thanked 0 Times in 0 Posts
Thanks. But the group has to be always unique. Sorry maybe I didn't explain well. For example, first, the group with highest score 'a' should be unique. Means no ab or abc etc. Therefore j008 is not in the output. Second, there should be no other group that should be unique with in the same set. For example, j011 has 3 unique groups. Therefore it should not be in output. Hope that's clear? With your script, j009 is missing and j010 with wrong group is being selected.

Last edited by quincyjones; 06-12-2017 at 04:35 PM..
    #4  
Old Unix and Linux 06-12-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 19 October 2017, 2:34 PM EDT
Location: Houston, Texas, USA
Posts: 971
Thanks: 0
Thanked 332 Times in 314 Posts
The output was changed.

Last edited by rdrtx1; 06-12-2017 at 07:21 PM..
Sponsored Links
    #5  
Old Unix and Linux 06-12-2017
quincyjones quincyjones is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 10 July 2017, 5:54 AM EDT
Posts: 184
Thanks: 53
Thanked 0 Times in 0 Posts
Yes that's correct. I forgot to include j009 in the output earlier. My apologies.
Sponsored Links
    #6  
Old Unix and Linux 06-12-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 October 2017, 1:23 PM EDT
Location: Aachen, Germany
Posts: 11,480
Thanks: 310
Thanked 3,561 Times in 3,276 Posts
For my understanding, let me paraphrase your request:
In any set, look for the maximum value Col 3). These are listed below:


Code:
sets    names   value   groups  other_groups
j007    shot1   0.6     a       b bb bc cd
j008    shot1   0.4     a       ab
j009    shot1   0.14    a       b bc
j010    shot1   22      a       b bcd
j011    shot1   5       a       b c

Now, if the group(s) of this entry show up in any of the other entries of the same set, suppress this record. If so, ALL entries EXCEPT j008 should be printed, no?

Sorry, you crossposted while I was pondering. So - to be eliminated, the group has to be unique, i.e.one single letter, and this letter may not occur in any of the other, possibly multiletter, groups it the same set, nor may any other single letter group occur in that set?

Last edited by RudiC; 06-12-2017 at 05:13 PM..
Sponsored Links
    #7  
Old Unix and Linux 06-13-2017
quincyjones quincyjones is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 10 July 2017, 5:54 AM EDT
Posts: 184
Thanks: 53
Thanked 0 Times in 0 Posts
@RudiC: First part is correct. Second part noy exactly. If you do Venn diagram with the letters in the groups of a specific set, you should always see 'a' as a separate group. For example, j007 has this type but not j008. Next, though j009, j010, j011 have 'a' as a separate group, j011 has also 'b' and 'c' as separate groups. Therefore only j007, j009 and j010 are in the output.

---------- Post updated at 04:18 PM ---------- Previous update was at 04:17 PM ----------

@RudiC: Update: yes your update is correct. Sorry for the confusion.

---------- Post updated 06-13-17 at 02:34 AM ---------- Previous update was 06-12-17 at 04:18 PM ----------

@rdrtx1: It is still not working I think. For ex, when I ran the modified script on this input, it not suppose to print 'j007' but instead it prints with group 'a'. This should not be printed because there is another unique group ('e') in the data.


Code:
sets    names    value    groups
j007    shot1    0.6    a
j007    shot2    0.5    b
j007    shot3    0.4    bc
j007    shot4    0.3    c
j007    shot5    0.2    cd
j007    shot6    0.1    e


Last edited by quincyjones; 06-13-2017 at 05:00 AM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print the key with highest value quincyjones Shell Programming and Scripting 9 03-29-2012 09:09 AM
Only print the entries with the highest number? chirish Shell Programming and Scripting 4 02-22-2012 03:23 AM
Print line with highest value from one column dliving3 UNIX for Dummies Questions & Answers 3 06-29-2011 07:35 AM
Print line with highest value from one column dliving3 UNIX for Dummies Questions & Answers 1 06-28-2011 01:57 AM
Sort the file contents in each group....print the group title as well prash184u Shell Programming and Scripting 11 08-26-2010 01:54 AM



All times are GMT -4. The time now is 12:34 AM.