![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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)
Code:
# ./test.py Entry2 ||| 50 Entry2 ||| 40 Entry2 ||| 30 Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 80 |
| Bits Awarded / Charged to ghostdog74 for this Post | |||
| Date | User | Comment | Amount |
| 05-29-2009 | FrancoisCN | N/A | 2,000 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|