我想找到列B的最大值,并保留列B值等于最大值的20%或更多的所有行。
输入数据
A B C D E
2 79 56 SD L
1 09 67 JK S
9 60 37 KD G
0 10 47 SO E
期望的输出
A B C D E
2 79 56 SD L
9 60 37 KD G
我尝试使用,awk 'BEGIN {max = 0} {if ($2>max) max=$2} END {if ($2 >= (0.1*max)) print}' file_in > file_out
但这只会打印出似乎是文件最后一行的内容。
您需要将所有行保存在数组中,以便能够在处再次通过它们END{ }
。或者,也可以对文件进行两次扫描。因此,保存所有值和行:
awk 'NR == 1 {header=$0; next} # save the header
{ lines[NR] = $0; values[NR] = $2; # save the line and 2nd field
if ($2 > max) max = $2; } # update max
END { print header; # in the end, print the header
for (i = 1 ; i <= NR ; i++) { # (we skipped line 0)
if (values[i] >= max * 0.2) # print lines where $2 was high enough
print lines[i]; } } ' file_in
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句