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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Print a line using a max and a min values of different columns
# 1  
Old 04-19-2012
Question [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:

Quote:
cluster 1 ligands 1
cluster 2 ligands 1
cluster 3 ligands 1
cluster 4 ligands 1
cluster 5 ligands 1
cluster 6 ligands 1
cluster 7 ligands 2
cluster 8 ligands 2
cluster 9 ligands 2
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 column 2.

So the output should be:
Quote:
cluster 7 ligands 2
I already can get the line based in the maximum value of last column with this awk:
Code:
awk '{if ($3 > max) max=$3} END {print line=$0}'

That give me this:
Quote:
cluster 9 ligands 2
But now i don't know how to get the "cluster 7 ligands 2" and not the "cluster 9 ligands 2". I was thinking in get the minimum value in 2 column in case of the maximum value repeats but i can't find how to do that Smilie

Thanks for any help Smilie
# 2  
Old 04-19-2012
That script always prints the last line (and in some awks it prints an empty line). Try:
Code:
awk '{if ($4 > max) {max=$4; maxline=$0}} END {print maxline}' infile

This works is the clusters are in sorted order, otherwise you will need to test for $2 too.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-20-2012
Quote:
Originally Posted by Scrutinizer
That script always prints the last line (and in some awks it prints an empty line). Try:
Code:
awk '{if ($4 > max) {max=$4; maxline=$0}} END {print maxline}' infile

This works is the clusters are in sorted order, otherwise you will need to test for $2 too.
Yes this solve the problem, and in all files the clusters are in order.

If you can you confirm this to me.

For what i can tell my mistake was the
Code:
{print line=$0}

in my awk that give always last line and not the one with max value right?Smilie

Sorry if is a dumb question but im new in script
# 4  
Old 04-20-2012
Hi, yes this line is in the END section, which gets executed after the input file(s) will have been read. The statement means set variable "line" to the last record ($0) and prints the result.. On some awks $0 will still contain the last record, but this is not guaranteed, so it may print an empty line. Anyway, if written like that it would be equivalent to:
Code:
awk 'END {print}'


Last edited by Scrutinizer; 04-20-2012 at 06:24 AM..
# 5  
Old 04-20-2012
Hum ok thanks again

---------- Post updated at 04:18 AM ---------- Previous update was at 04:17 AM ----------

Hum ok thanks again.


This is solved you can close
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue using awk to print min values for unique ids

I am using the following script to search for and print minimum values for each individual Fields (3-14) for each unique id (Field 1). But when the field contains a "-99.99" ( I am ignoring "-99.99") and when the minimum value is the first line of a new id (Field 1), the output does not print Field... (13 Replies)
Discussion started by: ncwxpanther
13 Replies

2. Shell Programming and Scripting

Print root number between min and max ranges

Hi to all, Please help on the following problem, I'm not where to begin, if awk or shell script. I have pairs of ranges of numbers and I need to find the root or roots of ranges based on min Range and Max ranges Example #1: If min range is 120000 and max ranges 124999, it means that are... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

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

4. Shell Programming and Scripting

Need to pick max values of the columns

Hi, I have sar disk reports like below sample: 01:01:00 hdisk24 0 0.0 0 0 0.0 0.0 hdisk15 0 0.0 0 3 0.0 5.5 hdisk20 0 0.0 2 1 0.0 1.9 hdisk19 1 ... (3 Replies)
Discussion started by: reddyr
3 Replies

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

6. Shell Programming and Scripting

print max number of 2 columns - awk

Is it possible to print max number of 2 columns - awk note: print max if the integer is positive and print min if the integer is negative input a 1 2 b 3 4 c 5 1 d -3 -5 d -5 -3 output a 2 b 4 c 5 d -5 d -5 (4 Replies)
Discussion started by: quincyjones
4 Replies

7. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

8. Shell Programming and Scripting

Find min.max value if matching columns found using AWK

Input_ File : 2 3 4 5 1 1 0 1 2 1 -1 1 2 1 3 1 3 1 4 1 6 5 6 6 6 6 6 7 6 7 6 8 5 8 6 7 Desired output : 2 3 4 5 -1 1 4 1 6 5 6 8 5 8 6 7 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

9. UNIX for Dummies Questions & Answers

Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value. BEGIN {FS=","; max=0} NF == 7 {if (max < $6) max = $6;} END { print man, min} where $6 is the column of a field separated by a comma (3 Replies)
Discussion started by: Kirichiko
3 Replies

10. Shell Programming and Scripting

max values amd min values

Hello every one, I have following data ***CAMPAIGN 1998 CONTRIBUTIONS*** --------------------------------------------------------------------------- NAME PHONE Jan | Feb | Mar | Total Donated ... (12 Replies)
Discussion started by: devmiral
12 Replies
Login or Register to Ask a Question