Search code examples
bashsortingmax

Find maximum value on only specific lines bash script


I have the following text :

title
P1 : I = -20.32;
P2 : I = 24.07;
P3 : I = -16.68;
T_B1 : I = 24.93;
T_H1 : I = -7.49;
T_B2 : I = 25.48;
T_H2 : I = -0.20;
T_B3 : I = 25.81;
T_H3 : I = 5.32;
T_B4 : I = 26.00;
T_H4 : I = 9.27;
T_B5 : I = 26.09;
T_H5 : I = 11.84;
T_B5 : I = 26.11;
T_H5 : I = 11.04;

And I just would like the maximum value of T_H*

T_H5 : I = 11.84;

I tried something like this :

sort -t= -nr -k3 text | head -1

I don't understand if I have to use sort or awk because I only want to sort on specifis lines. I tried to google it many times and read the sort command manual but I don't get what I want. If somebody could help me this this it would be cool :) Thank you


Solution

  • Another awk script, to get the maximum value of T_H*:

    awk '/^T_H/{max=($5+0>max?$5+0:max)}END{print max}' file
    11.84
    

    The $5+0 allows to strip out the ; to only get the value.
    This value is compared to the max variable in the ternary operator.
    When end of the file is reached, the max value is printed.