Sponsored Content
Top Forums Shell Programming and Scripting awk to select lines with maximum value of each record based on column value Post 302988192 by yifangt on Wednesday 21st of December 2016 03:48:58 PM
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..
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. 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
COLRM(1)						    BSD General Commands Manual 						  COLRM(1)

NAME
colrm -- remove columns from a file SYNOPSIS
colrm [start [stop]] DESCRIPTION
The colrm utility removes selected columns from the lines of a file. A column is defined as a single character in a line. Input is read from the standard input. Output is written to the standard output. If only the start column is specified, columns numbered less than the start column will be written. If both start and stop columns are spec- ified, columns numbered less than the start column or greater than the stop column will be written. Column numbering starts with one, not zero. Tab characters increment the column count to the next multiple of eight. Backspace characters decrement the column count by one. ENVIRONMENT
The LANG, LC_ALL and LC_CTYPE environment variables affect the execution of colrm as described in environ(7). EXIT STATUS
The colrm utility exits 0 on success, and >0 if an error occurs. SEE ALSO
awk(1), column(1), cut(1), paste(1) HISTORY
The colrm command appeared in 3.0BSD. BSD
August 4, 2004 BSD
All times are GMT -4. The time now is 04:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy