简体   繁体   English

仅在特定行 bash 脚本上查找最大值

[英]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_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.我不明白我是否必须使用 sort 或 awk 因为我只想在特定的行上进行排序。 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如果有人可以帮助我这会很酷:)谢谢

If you want to solve this using UNIX tools, then you should use a UNIX approach of one tool for each purpose.如果您想使用 UNIX 工具解决这个问题,那么您应该使用一种工具用于每个目的的 UNIX 方法。

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.当然还有其他方法可以用更少的管道来解决它,但这似乎不是你想要的。

Another awk script, to get the maximum value of T_H*:另一个 awk 脚本,获取 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 ; $5+0允许去掉; to only get the value.只获得价值。
This value is compared to the max variable in the ternary operator.该值与三元运算符中的max变量进行比较。
When end of the file is reached, the max value is printed.当到达文件末尾时,将打印最大值。

Both will work, but Awk can be quite a bit more efficient and succinct.两者都可以,但 Awk 可以更加高效和简洁。

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.主体提取T_H后的整数并重新计算当前最大值。 The 0 + forces conversion to a number, which also discards any nonnumeric suffix. 0 +强制转换为数字,这也会丢弃任何非数字后缀。 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. END块中的0 +类似地提供了一个数字上下文,因此如果由于某种原因maxx为空,则输出将是一个数字而不是一个空字符串。

If you mean the max of the last field rather than the number immediately after T_H , that's even easier;如果你的意思是最后一个字段的最大值而不是T_H之后的T_H ,那就更容易了; just use $NF instead of the substr(...) expression.只需使用$NF而不是substr(...)表达式。

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.) (这也说明了$NF变化。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM