Home > OS >  Find maximum value on only specific lines bash script
Find maximum value on only specific lines bash script

Time:10-15

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

CodePudding user response:

If you want to solve this using UNIX tools, then you should use a UNIX approach of one tool for each purpose.

grep '^T_H' < input | # Filter
    sort -t= -nr -k3 | # sort
    head -n1

Of course there are other ways to solve it with fewer pipes, but that doesn't seem to be what you're after.

CodePudding user response:

Both will work, but Awk can be quite a bit more efficient and succinct.

awk '/^T_H[0-9]/ { x = 0   substr($1, 4)
    if (maxx < x) maxx = x }
  END { print 0 maxx }' text

The main body extracts the integer after T_H and remerbers the current max. The 0 forces conversion to a number, which also discards any nonnumeric suffix. The 0 inthe END block similarly supplies a numeric context, so that the output will be a number rather than an empty strin, if maxx is empty for some reason.

If you mean the max of the last field rather than the number immediately after T_H, that's even easier; just use $NF instead of the substr(...)expression.

If you want to print the whole line, keep that in a second variable, and update it whenever you update the max.

awk '/^T_H[0-9]/ { x = 0   $NF
    if (maxx < x) { res = $0; maxx = x } }
  END { print res }' text

(This also illustrates the $NF variation.)

CodePudding user response:

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.

  • Related