Take greatest value from second column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Take greatest value from second column
# 1  
Old 06-11-2013
Take greatest value from second column

Dear All,

Please help me, I have file input like this,

Code:
  
1  2142
215  2162
217  2842
285  2862
287  4002
401  4022
403  4822
  1  2142
215  2162
217  2842
285  2862
287  4002
401  4022
403  4882
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1   231
 24   241
 25  4791
480  4801
  1   431
 44   441
 45  4801
  1   251
 26   261
 27  4801
  1   451
 46   461
 47  4801

Desired output:

Code:
  1  4822
  1  4882
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801
  1  4801

Thanks for advance.

Attila
# 2  
Old 06-11-2013
Its easy to get the largest value form second column, but I do not see the link to your desired output.
Why 1 in front?
# 3  
Old 06-11-2013
Can you please briefly explain what is your criteria to select the row.
And there is mismatch between your input file and desired output.
# 4  
Old 06-11-2013
Algoritma:

When first column has value 1, get greates value of second column until next first column has value 1 again.

Sorry for my english.

---------- Post updated at 06:58 AM ---------- Previous update was at 06:55 AM ----------
# 5  
Old 06-11-2013
Code:
awk ' 
$2 > max { max = $2 }
NR == 1 { next; }
$1 == 1 { print 1, max; max = $2; }
'  filename

my code return one row less than your output! can it help you find your way ?

Last edited by mstafreshi; 06-11-2013 at 09:41 AM..
This User Gave Thanks to mstafreshi For This Post:
# 6  
Old 06-11-2013
Like this?
Code:
awk 'NR>1 && $1==1{print $1,t} {t=$2}'
1 4822
1 4882
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801
1 4801

This User Gave Thanks to Jotne For This Post:
# 7  
Old 06-11-2013
If 2nd field is not sorted, using Jotne's idea:
Code:
awk 'NR > 1 && $1 == 1 { print $1, t; t = 0 } { t = t < $2 ? $2 : t }' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

3. UNIX for Dummies Questions & Answers

help! script to select line with greatest value 2 between columns

Hi, I’m trying to do something I haven’t done before and I’m struggling with how to even create the command or script. I have the following space delim file: gene accession chr chr_st begin end NN1 NC_024540 chr3 - 14000 14020 NN1 ... (10 Replies)
Discussion started by: wolf_blue
10 Replies

4. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

5. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

6. Shell Programming and Scripting

finding greatest value in a column using awk from iostat output in linux

Friends, Need some help. On linux i have to run iostat command and in each iteration have to print the greatest value in each column. e.g iostat -dt -kx 2 2 | awk ' !/sd/ &&!/%util/ && !/Time/ && !/Linux/ {print $12}' 4.38 0.00 0.00 0.00 WHhat i would like to... (15 Replies)
Discussion started by: achak01
15 Replies

7. Shell Programming and Scripting

finding greatest value in a column using awk from iostat output in linux

Friends, . On linux i have to run iostat command and in each iteration have to print the greatest value in each column. e.g iostat -dt -kx 2 2 | awk ' !/sd/ &&!/%util/ && !/Time/ && !/Linux/ {print $12}' 4.38 0.00 0.00 0.00 What i would like to print is only the... (3 Replies)
Discussion started by: achak01
3 Replies

8. Shell Programming and Scripting

Using perl code find greatest number.

How would one go about finding the greatest number in an array of numbers using loops and if statements? For example, if you had an array like this: (4, 8, 3, 19, 6, 11) (3 Replies)
Discussion started by: DemonixX
3 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies
Login or Register to Ask a Question