The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting Field values for XML file junaid.nehvi High Level Programming 3 03-27-2009 12:38 PM
Awk search for max and min field values Kirichiko UNIX for Dummies Questions & Answers 3 07-29-2008 05:43 PM
How to sort a field in a file having date values risshanth Shell Programming and Scripting 4 06-04-2008 06:03 AM
read from a file and calculate values for a specified field lucho_1 Shell Programming and Scripting 3 03-11-2008 07:24 PM
Need help with switching field/column values sonyd8 Shell Programming and Scripting 7 02-13-2008 01:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-29-2009
FrancoisCN FrancoisCN is offline
Registered User
  
 

Join Date: May 2009
Posts: 16
Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field.

For example if N=3, we want the 3 best values for each entry:

Entry1 ||| 100
Entry1 ||| 95
Entry1 ||| 30
Entry1 ||| 80
Entry1 ||| 50
Entry2 ||| 40
Entry2 ||| 20
Entry2 ||| 10
Entry2 ||| 50
Entry2 ||| 30

would return

Entry1 ||| 100
Entry1 ||| 95
Entry1 ||| 80
Entry2 ||| 40
Entry2 ||| 50
Entry2 ||| 30
  #2 (permalink)  
Old 05-29-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,554
if you have Python
Code:
d={}
for line in open("file"):
    line=line.strip().split(" ||| ")
    d.setdefault(line[0],[])
    d[line[0]].append(line[-1])
for i,j in d.iteritems():
    for item in sorted(j,lambda x,y: int(y)-int(x))[:3]:
        print "%s ||| %s" %(i,item)
output
Code:
# ./test.py
Entry2 ||| 50
Entry2 ||| 40
Entry2 ||| 30
Entry1 ||| 100
Entry1 ||| 95
Entry1 ||| 80
show your code next time.
Bits Awarded / Charged to ghostdog74 for this Post
Date User Comment Amount
05-29-2009 FrancoisCN N/A 2,000
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0