[Solved] Using awk to calculate max value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Using awk to calculate max value
# 1  
Old 02-07-2014
[Solved] Using awk to calculate max value

I have a file of sites and each site has a variable number of flow values with a date for each value. I want to determine the max value of flow for each site and output the site number, max value, and date of max value.The format structure (simplified) is the following:

Code:
Record  Site Number  Date Flow Value
 V      050100
 3      050100   6/17/1928    145
 3      050100  5/28/1929     500
 .
 .
 3     050100    6/5/2012     450
 V     06200
 3     06200      7/3/1945     1256
 3     06200      6/8/1950      835
 .
 .
 3    06200      5/28/1999    287

Thanks for your help!!

Last edited by bartus11; 02-07-2014 at 02:35 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 02-07-2014
What would be a desired output for this sample data?
# 3  
Old 02-07-2014
The output would look the following:

Code:
050100  5/28/1929     500
062000  7/1/2001     1500
.
.

where the values shown for sites 050100 and 06200 are the max values for the entire set of flows for each site. the number of flow values for each site vary from 10 to over 100.

Last edited by Scrutinizer; 02-07-2014 at 06:24 PM.. Reason: code tags
# 4  
Old 02-07-2014
Something like this
Code:
awk '!/^Rec/ && NF==4 && a[$2]<$NF{ a[$2]=$NF; d[$2]=$(NF-1) } END{ for(i in a)print i,a[i],d[i] }' input_file

--ahamed
# 5  
Old 02-08-2014
Quote:
Originally Posted by cparr
The output would look the following:

Code:
050100  5/28/1929     500
062000  7/1/2001     1500
.
.

where the values shown for sites 050100 and 06200 are the max values for the entire set of flows for each site. the number of flow values for each site vary from 10 to over 100.
Given that your sample input does not have any entries for Site Number 062000, does not have any entries with Date 7/1/2001, and does not have any entries with Flow Value 1500, I don't understand how you would expect to get the 2nd line of output shown above.

If the output you wanted from your sample input had been:
Code:
050100  5/28/1929     500
06200   7/3/1945     1256

you could use something like the following awk script:
Code:
awk '
function prmax() {
	if(m != "") printf("%-8s%-10s%7d\n", s, d, m)
	m = ""
}
$1 == "V" {
	prmax()
	s = $2
	next
}
NF == 4 && m < $4 {
	d = $3
	m = $4
}
END {	prmax()
}' file

Unlike the script provided by ahamed101, the output will be in the same order as the input file. (The for loop used by ahamed101 prints entries from the array in an unspecified order.) The above code prints the max flow value for the previous site number as soon as it finds the first line of the next site number (so it also uses less memory).

If you want to try this script on a Solaris/SunOS operating system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
# 6  
Old 02-08-2014
Problem Solved!

Thank you ahamed101 and Don! As you correctly surmised Don, the output I was after was the max value for each site with the output lines displayed as you indicated. My sample output was confusing because I was trying to show with the dots that actual number of flow values was greater than the 3 or 4 lines I showed for each site. In any case, your awk script worked perfectly, Don--many thanks!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get min and max values using awk?

Hi, I need your kind help to get min and max values from file based on value in $5 . File1 SP12.3 stc 2240806 2240808 + ID1_N003 ID2_N003T0 SP12.3 sto 2241682 2241684 + ID1_N003 ID2_N003T0 SP12.3 XE 2239943 2240011 + ID1_N003 ID2_N003T0 SP12.3 XE 2240077 2241254 + ID1_N003 ... (12 Replies)
Discussion started by: redse171
12 Replies

2. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

3. Shell Programming and Scripting

Shell script to calculate the max cpu usage from the main script

Hi All, I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide which cpu number (column 2) has maximum value (column 6). Since the output is displayed/updated every seconds, it's very... (1 Reply)
Discussion started by: Optimus81
1 Replies

4. Shell Programming and Scripting

awk to find the max

Experts, Here is my question. I have got file like below # cat file XYZb,24,26,6 XYZc,24,26,6 XYZe,24,25,5 XYZf,23,29,5 XYZi,16,25,5 XYZj,24,26,7 XYZn,17,23,4 XYZz,23,29,5 Now, I want to print the line's Column1 whose Column 3 is the max of all. My code is awk -F',' 'BEGIN {max... (9 Replies)
Discussion started by: sathyaonnuix
9 Replies

5. UNIX for Dummies Questions & Answers

[Solved] How to calculate in sun bash

I have two problems, and it would be great if someone could help me: The first line does not calculate. I have checked the origin term to calculate the variables and the result is OK. Normal substactions with $xx -100 work, but not in this constallation. I tried it with "| bc" and no result... (2 Replies)
Discussion started by: Pieter0815
2 Replies

6. Shell Programming and Scripting

awk, max value, array, row

Hello: I want to print out the entire row with max value in column 3 based on column 2. Input file is millions rows. test.dat: Contig1 lcl|1DL 111 155 265 27 Contig2 lcl|1DS 100 73 172 100 Contig3 lcl|1DL 140 698 837 140 Contig3 lcl|6DS 107 1488 1594... (1 Reply)
Discussion started by: yifangt
1 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

8. Shell Programming and Scripting

Join and awk max column

Hi Friends, I have a file1 with 3400 records that are tab separated and I have a file2 with 6220 records. I want to merge both these files. I tried using join file1 and file2 after sorting. But, the records should be (3400*6220 = 21148000). Instead, I get only around 11133567. Is there anything... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

9. Shell Programming and Scripting

count the max by awk

hey everybody .. I have a problem when I use this command to count the Max number by awk it give to me 99 while there are numbers bigger than 99 awk 'NR==0 { temp = $0 ;next} { if ( $0 >= temp ) { temp=$0 } } END{ print "Maximum Number:"temp""}' file thank you for your help... (9 Replies)
Discussion started by: halola85
9 Replies
Login or Register to Ask a Question