clear.choi
clear.choi

Reputation: 855

Linux sorting and cut rows except maximum values

I have a question control txt file.

One file contain 4 column and if I want to cut rows except maximum values in the columns

1.Check First Column Name
2.Check Last Column Values.
3.Remove depends on First Column and Last Column.

Test1    500     400    200
Test1    499     400    200
Test1    499     399    200 
Test1    498     100    100
Test2    600     200    150
Test2    600     199    150
Test2    599     199    100

I want to delete line without top line of Name For example as below Name Score Length

Test1    500   400  200
Test1    499   400  200
Test1    499   399  200 
Test2    600   200  150
Test2    600   199  150

Anyone have a good Idea to figure it out? Awk or Sed ..

Thank you for your any information!

ubuntu awk sed

Upvotes: 1

Views: 105

Answers (1)

Barmar
Barmar

Reputation: 780843

This is based on glenn jackman's answer, but tests the columns correctly according to my reading of the question. The array max contains the maximum value of column 4 for each name in column 1. During the second pass, we test whether the column matches the maximum value.

awk '
    NR == FNR {
        if ($4 > max[$1]) max[$1] = $4
        next
    }
    $4 == max[$1]
' control.txt control.txt

Upvotes: 1

Related Questions