1
votes

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

1
Are the lines beginning with 1., 2. and 3. in the file? Please reformat the question if not. - Barmar
In awk, save every line into an array. Simultaneously, make an associative array that contains the maximum value for each name. Then in the END block, loop through the saved line array, printing it if it matches the maximum value for the name. - Barmar
you question is not clear to me, mamimum value in the columns?? plz explain?? - Hackaholic
I am sorry for making confused but Barmar get exactly my points thank you so much! - clear.choi

1 Answers

1
votes

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