SLUG Mailing List Archives
Re: [SLUG] Max in $2, $3
- To: Jobst Schmalenbach <jobst@xxxxxxxxxxxxxx>, Slug List <slug@xxxxxxxxxxx>
- Subject: Re: [SLUG] Max in $2, $3
- From: Andrew Bennetts <andrew@xxxxxxxxxxxx>
- Date: Tue Jul 3 00:21:01 2001
- User-agent: Mutt/1.3.18i
On Mon, Jul 02, 2001 at 02:39:40PM +1000, Jobst Schmalenbach wrote:
> On Mon, Jul 02, 2001 at 11:15:05PM +1000, Terry Collins (terryc@xxxxxxxxxx) wrote:
> > As I can not find anything in the archives, I thought I would ask the
> > list.
> > Just need to run though a file and find the maximum number in field 2
> > and field 3. (Two runs okay).
> Cant you just pipe it through sort and awk/sed the first line?
(assuming comma delimited)
sort -n -t, -r -k 2 | head -n 1 | cut -d "," -f 2
See the sort/head/cut man pages to decipher. In short, it numerically
sorts in reverse order, seperating the fields with a ",", and using
field 2 as the sort key. It then takes the first line, and then removes
all but the second field.
I may have the field number off by one.. a quick moments fiddling will
And you'd do the almost the same for field 3, of course.
Or in python (1.5.2):
import string, sys
if len(sys.argv) < 1:
print "Usage: <input file> <field_list>"
print "e.g. find_max.py datafile.txt 2,3"
# Turn the "2,3" into a python list [2,3]
max_list = map(int,string.split(sys.argv),",")
# Initialise the result list to the appropriate length.
# I'm assuming you aren't worried about negative values.
max_values = [0.0] * len(max_list)
# No error checking is done... if something unexpected happens, tough.
for line in open(sys.argv,'r').readlines():
# Assume comma is the seperator
fields = string.split(string.strip(line),",")
for pos in max_list:
max_values[pos] = max(max_values[pos],float(fields[pos]))
# Iterate over two lists simultaneously
for pos, value in map(None, max_list, max_values):
print "Field %d has a maximum value of %f" % (pos, value)
Note that this script will find as many maximum values as you like at
once. It's more verbose, but it ought to be faster than the
shell commands on larger files, as it doesn't need to sort the
data first. It's also completely untested, but it looks ok...
It's not a very interesting script, but I thought the list could do with
some variety from the usual perl gunk ;)