Sponsored Content
Top Forums Shell Programming and Scripting Find top N values for field X based on field Y's value Post 302320919 by ghostdog74 on Friday 29th of May 2009 09:57:33 AM
Old 05-29-2009
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.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk - Summing a field based on another field

So, I need to do some summing. I have an Apache log file with the following as a typical line: 127.0.0.1 - frank "GET /apache_pb.gif HTTP/1.0" 200 2326 Now, what I'd like to do is a per-minute sum. So, I can have awk tell me the individual minutes, preserving the dates(since this is a... (7 Replies)
Discussion started by: treesloth
7 Replies

2. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

3. UNIX for Dummies Questions & Answers

Please help me to find out maximum value of a field based on grouping of other fields.

Please help me to find out maximum value of a field based on grouping of other fields, as we do in SQL. Like in SQL if we are having below records : Client_Name Associate_Name Date1 Value C1111 A1111 2012-01-17 10 C1111 A1111 ... (1 Reply)
Discussion started by: KamalKumarKalra
1 Replies

4. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

5. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

6. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

7. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

8. Shell Programming and Scripting

Compute average based on field values

Im looking for a way to average the values in field 14 (when field 2 is equal to 2016) and fields 3 and 4 (when field 2 is equal to 2017). Any help is appreciated. 001001 2016 33.22 38.19 48.07 51.75 59.77 67.68 70.86 72.21 66.92 53.67 42.31 40.15 001001 2017 ... (10 Replies)
Discussion started by: ncwxpanther
10 Replies

9. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies
FORMS(3)						   BSD Library Functions Manual 						  FORMS(3)

NAME
dup_field, free_field, link_field, new_field -- form library LIBRARY
Curses Form Library (libform, -lform) SYNOPSIS
#include <form.h> FIELD * dup_field(FIELD *field, int frow, int fcol); int free_field(FIELD *field); FIELD * link_field(FIELD *field, int frow, int fcol); FIELD * new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf); DESCRIPTION
The dup_field() function duplicates the given field, including any buffers associated with the field and returns the pointer to the newly created field. free_field() destroys the field and frees any allocated resources associated with the field. The function link_field() copies the given field to a new field at the location frow and fcol but shares the buffers with the original field. new_field() creates a new field of size rows by cols at location frow, fcol on the page, the argument nrows specified the number of off screen rows the field has and the nbuf parameter specifies the number of extra buffers attached to the field. There will always be one buffer associated with a field. RETURN VALUES
On error dup_field() and new_field() will return NULL. The functions will one of the following error values: E_OK The function was successful. E_BAD_ARGUMENT A bad argument was passed to the function. E_CONNECTED The field is connected to a form. SEE ALSO
curses(3), forms(3) NOTES
The header <form.h> automatically includes both <curses.h> and <eti.h>. BSD
January 1, 2001 BSD
All times are GMT -4. The time now is 08:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy