平均文件列M的最后N行 - 终端

时间:2021-04-23 16:21:03

I wanted to compute an average of the last N lines of column M of a file. I found that the best solution is to use tail to get last N lines and pipe it to awk. For my case, N=200 and M=2. The resulting command would be:

我想计算文件列M的最后N行的平均值。我发现最好的解决方案是使用tail来获取最后N行并将其传递给awk。对于我的情况,N = 200且M = 2。结果命令是:

tail -n 200 -f filename.asc | awk '{ total += $2 } END { print total/NR }'

However, I am not getting any result. In fact, the terminal does not return. I tried tail separately and it outputs the last 200 rows fine, but does not work combined with awk.

但是,我没有得到任何结果。实际上,终端不会返回。我分别尝试尾部,它输出最后200行罚款,但不能与awk结合使用。

I also tried using unbuffer (before the tail) as one of the solutions suggested this, but still not solved yet. Kindly suggest a solution.

我也尝试使用unbuffer(在尾部之前)作为解决方案之一,但仍未解决。请提出解决方案。

1 个解决方案

#1


2  

When running tail with -f (follow mode), it never terminates; and awk runs the END block at the end-of-input, thus it will never terminate. Remove the -f and it should work.

当使用-f(跟随模式)运行tail时,它永远不会终止;并且awk在输入结束时运行END块,因此它永远不会终止。删除-f它应该工作。

#1


2  

When running tail with -f (follow mode), it never terminates; and awk runs the END block at the end-of-input, thus it will never terminate. Remove the -f and it should work.

当使用-f(跟随模式)运行tail时,它永远不会终止;并且awk在输入结束时运行END块,因此它永远不会终止。删除-f它应该工作。