awk to select lines with maximum value of each record based on column value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to select lines with maximum value of each record based on column value
# 1  
Old 12-21-2016
awk to select lines with maximum value of each record based on column value

Hello,
I want to get the maximum value of each record separated by empty line based on the 3rd column of each row within each record?
Input:
Code:
A1    chr5D    634    7    82    707
A2    chr5D    637    6    82    713
A3    chr5D    637    5    82    713
A4    chr5D    626    1    82    704
A5    chr5D    723    0    1    723
A6    chr5D    734    12    1    722
A7    chr5D    719    3    1    715
                    
B5    chr6B    344    2    64    403
B6    chr6B    738    1    1    735
B7    chr6B    738    5    1    732
B8    chr6B    738    1    1    735
B9    chr6D    732    0    1    732
B10    chr6D    735    1    1    735
B11    chr6D    735    1    1    735
                    
C1    chr6D    735    0    1    735
C2    chr6D    735    2    1    732
C3    chr6D    735    0    1    735
                    
C4    chr7A    678    0    1    678
C5    chr7A    681    10    1    660
C6    chr7A    678    0    1    678
C7    chr7A    674    11    42    687

Output:
Code:
A6    chr5D    734    12    1    722
B8    chr6B    738    1    1    735
C3    chr6D    735    0    1    735
C5    chr7A    681    10    1    660

So far, I have tried:
Code:
awk 'BEGIN{FS="\n"; RS=""} $3 > max[NR] {maxline[NR]=$0} END{ for (i in maxline) {print maxline[i]}}' input.tab

but it did not give what I expected.
There are several issues with my script.
1) $3 is for each line, but not correct within each record;
2) same issue with $0;
It seems to me the trick is the FS value and maxline[NR]=$0 in my case.

Thanks a lot!

Last edited by yifangt; 12-21-2016 at 05:13 PM..
# 2  
Old 12-21-2016
a bit of a long way...
awk -f yi.awk myFile where yi.awk is:
Code:
!NF {print maxline; max=0}
$3>=max{ max=$3;maxline=$0}
END { if (max) print maxline}

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 12-21-2016
How did you come with this trick "!NF" !

I am not sure if the END block get executed at each empty line. Could you please elaborate END block for me?
Thanks!

Last edited by yifangt; 12-21-2016 at 05:43 PM..
# 4  
Old 12-21-2016
Quote:
Originally Posted by yifangt
Thanks!
How did you come with this trick "!NF" ?
I guess by RTFMing? Smilie

---------- Post updated at 04:37 PM ---------- Previous update was at 04:23 PM ----------

Quote:
Originally Posted by yifangt
How did you come with this trick "!NF" !
One more question: Could you please elaborate END block for me?
Thanks!
there might be no trailing empty line after the LAST block (as in you example).
So the '!NF' will never catch the last record. So we're catching it in the END block - after ALL the records have been processed.
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 12-21-2016
Thanks! I get it now.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. UNIX for Dummies Questions & Answers

awk solution to duplicate lines based on column

Hi experts, I have a tab-delimited file with one column containing values separated by a comma. I wish to duplicate the entire line for every value in that comma-delimited field. For example: $cat file 4444 4444 4444 4444 9990 2222,7777 6666 2222 ... (3 Replies)
Discussion started by: torchij
3 Replies

4. Shell Programming and Scripting

Select record having different value in second column

I want records which have more than one and different value in the second column on the below sample file. Ex, I have the samle file below :- XYZ 1 XYZ 3 abc 1 abc 1 qwe 2 qwe 1 qwe 3 I want to select XYZ and QWE line only. (6 Replies)
Discussion started by: Sanjeev Yadav
6 Replies

5. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

6. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

7. Shell Programming and Scripting

Awk - how to select the column based on day of month

Problem, How can you pass today's date (eg) 18 into Awk and select the field representing the 18th column? I have an output that I want to interact with based on what day it is information=0:0 192:0 5436:0 22:99 0:0 0:0 1234:0 1359:09 DAY=date'+%d' numbrs=`$information | awk... (2 Replies)
Discussion started by: Gizmo1007
2 Replies

8. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies

9. Shell Programming and Scripting

Select Record based on First Column

Hi, I have a file with multiple records...and I have to select records based on first column....here is the sample file... I01,abc,125,1a2,LBVI02 I01,abc,126,2b5,LBVI02 I02,20070530,254,abc,LLBI01 I02,20070820,111,bvd,NGBI01 I need all records with I01 in first field in one file and... (8 Replies)
Discussion started by: mgirinath
8 Replies
Login or Register to Ask a Question