Get the MAX value out of a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the MAX value out of a column
# 1  
Old 04-24-2013
Get the MAX value out of a column

I've the following data set. I would like to look at the column 3 and only use the rows which has the max value for column 3

Can we use the awk or sed to achieve it.

Code:
10      2       10       100
11      2       20       100
12      2       30       100
13      2       30       100
14      2       4       100
15      2       4       100
16      2       2       100
17      2       2       100
18      2       1       100

so based on the above data, max value for column 3 is 30. The results I would want is

Code:
12      2       30       100
13      2       30       100

# 2  
Old 04-24-2013
Code:
awk 'NR==FNR { if((!MAX)||(MAX<$3)) MAX=$3; next }; $3==MAX' filename filename

# 3  
Old 04-24-2013
Code:
awk '{A[NR" "$3]=$0;max=max<$3?$3:max}END{for(k in A){split(k,B);if(B[2]==max) print A[k]}} ' file

# 4  
Old 04-25-2013
While the OP's sample data consists exclusively of positive integers, if negative values and zeroes must be handled, neither solution is correct.

Corona's code will unconditionally set MAX to the next line's $3, if MAX is ever set to 0.
Code:
$ cat file
-1 -1 -1 -1
0 0 0 0
-2 -2 -2 -2
$ awk 'NR==FNR { if((!MAX)||(MAX<$3)) MAX=$3; next }; $3==MAX' file file
-2 -2 -2 -2
$ awk 'NR==FNR { if((NR==1)||(MAX<$3)) MAX=$3; next }; $3==MAX' file file
0 0 0 0

Yoda's code implicitly uses a max of 0 when NR==1 (a numeric 0 is the default value of an uninitialized variable). If that implicit 0, which is not present in the file, is greater than all values, B[2]==max will never succeed and no output is generated. max must be initialized from the first value of $3.
Code:
$ cat file
-1 -1 -1 -1
-2 -2 -2 -2
-3 -3 -3 -3
$ awk '{A[NR" "$3]=$0;max=max<$3?$3:max}END{for(k in A){split(k,B);if(B[2]==max) print A[k]}}' f
$ awk '{A[NR" "$3]=$0;max=max<$3||NR==1?$3:max}END{for(k in A){split(k,B);if(B[2]==max) print A[k]}}' f
-1 -1 -1 -1

Regards,
Alister
# 5  
Old 04-25-2013
Code:
$ awk '$3 == hi {print}' hi=$(awk '{print $3}' file | sort -n | tail -1) file
12      2       30       100
13      2       30       100

# 6  
Old 04-25-2013
Can be shorten some bye removing the print, since it's a default action.
Code:
awk '$3==hi' hi=$(awk '{print $3}' file | sort -n | tail -1) file

Other variation
Code:
awk '$3==hi' hi=$(awk '$3>n {n=$3} END {print n}' file) file
12      2       30       100
13      2       30       100


Last edited by Jotne; 04-25-2013 at 02:58 AM..
# 7  
Old 04-25-2013
Code:
$ sort -nr -k3,3 file|awk '$3<p{exit} {p=$3}1' 
13      2       30       100
12      2       30       100

To avoid the trap that alister pointed out, if negative values are expected in the file, preset p by adding p=$(getconf INT_MIN) to the end of the command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get min and max value in column

Gents, I have a big file file like this. 5100010002 5100010004 5100010006 5100010008 5100010010 5100010012 5102010002 5102010004 5102010006 5102010008 5102010010 5102010012 The file is sorted and I would like to find the min and max value, taking in the consideration key1... (3 Replies)
Discussion started by: jiam912
3 Replies

2. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

3. UNIX for Dummies Questions & Answers

get max value every 4 rows between 2 column

Hi all I have a file that has two columns and I need the maximum value in column 2 of 4 positions o rows. for example at position {1..3} there are 4 characters (A, C, G and T) each of these characters with a value with a value in column 2. I need the maximum value in column 2 and the corresponding... (2 Replies)
Discussion started by: xinox
2 Replies

4. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

5. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

6. 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

7. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

8. Shell Programming and Scripting

Calc max of a column

In C that was easy with a for and if. Iam trying to learn a litle more in bash. Example Ronaldo:5800 Figo:4000 Rafael:2321 Kaka:1230 I want the max of the $2 and the output will be: The max value is 5800 from Ronaldo. How can i do this in shell? Thanks for all, folks. (11 Replies)
Discussion started by: rafazz
11 Replies

9. Shell Programming and Scripting

Help: Find line where column has max vlaue

Hi all, Ive searched the forum but with no luck... I have a file: ID Name Val 1 bob 45 2 joe 89 3 sue 11 4 steve 89 ... etc I want to find a way to print... (6 Replies)
Discussion started by: muay_tb
6 Replies

10. Shell Programming and Scripting

Max column count in a file

I have to send a file to mainframe and before sending it, I have to execute the quote command to set the record length. Since the file is dynamic, I do not know what the maximum size of a line could be. Currently, I use the following function to get the Max Column Count. Since I use "sed" it... (2 Replies)
Discussion started by: gemini
2 Replies
Login or Register to Ask a Question