[英]use awk to confront first number in every line of a file and print the whole line that contains the largest one
The file I have looks like this: (thanks to @fredtantini) 我的文件如下所示:(感谢@fredtantini)
37.36 44.22 -221.12 0.85 2.02 4.00 0.49 2345.147 '/home/~/gold_soln_x_m1_70.sdf' 'X'
38.46 45.89 -229.45 1.49 1.94 4.00 0.61 2370.912 '/home/~/gold_soln_y_m1_69.sdf' 'Y'
39.90 46.86 -234.28 0.29 2.66 4.00 1.00 2368.052 '/home/~/gold_soln_w.sdf' 'W'
37.75 48.10 -240.50 2.58 3.77 4.00 0.60 2220.947 '/home/~/gold_soln_z.sdf' 'Z'
I need to have as output: 我需要作为输出:
39.90 46.86 -234.28 0.29 2.66 4.00 1.00 2368.052 '/home/~/gold_soln_w.sdf' 'W'
printed in another file but I've never used awk so I have no idea on how to organize my script. 打印在另一个文件中,但我从未使用过awk,所以我不知道如何组织脚本。 looking for answers I found this: 寻找答案,我发现了这一点:
awk'
function max(x){i=0;for(val in x){if(i<=x[val]){i=x[val];}}return i;}
{a[$2]=$2;next}`
END{minimum=min(a);maximum=max(a);print "Maximum = "maximum "}'
but it's not working and i have no idea why. 但它不起作用,我也不知道为什么。
The complete script looks like this: 完整的脚本如下所示:
#!/bin/bash
cd /home/~/
for k in $( cat lista_sottocartelle ); do
cd /home/~/$k
awk 'BEGIN{max=0}{if($1>max){max=$1;maxline=$0}}END{print maxline}' bestranking.lst >> indirizzi_ASP.lst
The output is: syntax error: unexpected end of file 输出为: 语法错误:文件意外结束
I just forgot to add done at the end of the script. 我只是忘了在脚本末尾添加完成。 thanks to all of you. 感谢大家。
Simply use: 只需使用:
~$ awk 'BEGIN{max=0}{if($1>max){max=$1;maxline=$0}}END{print maxline}' f
39.90 46.86 -234.28 0.29 2.66 4.00 1.00 2368.052 '/home/~/gold_soln_w.sdf' 'W'
For each line, you check if first number is greater than current max. 对于每一行,请检查第一个数字是否大于当前最大值。 If it is, you save the line in maxline
. 如果是这样,则将行保存在maxline
。 At the end, you print this line. 最后,您打印此行。
A shorter version doing the same thing (thanks to @Jotne): 做同样事情的简短版本(感谢@Jotne):
awk '$1>max {max=$1;maxline=$0}END{print maxline}'
There are several problems in your script ( $2
is the second column, min function is not defined…) 您的脚本中有几个问题( $2
是第二列,未定义min函数…)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.