【问题标题】:A one line command to get the average of specific column in big file [duplicate]获取大文件中特定列平均值的单行命令[重复]
【发布时间】:2016-11-28 23:18:20
【问题描述】:

我需要设置一个单行命令来获取文件中一列中大量数字的平均值包含多列,我们需要添加文件名的参数和大文件所需的列数

Example: # avgerage_column 3 test

average_column should be the script
3 is the number of the column in the big file
test is the big file


Sample:

210  56687  1792 18 55 26  1  
188  21949   961  9 57 34  0  
201   1257   521  1 46 54  0  
260  11455   641  4 47 49  0  
244  16466   756  7 38 55  0  
 54   1570   651  1 14 85  0  
161    619   583  0 28 72  0  
 88    479   600  0 26 74  0  
 30    394   670  0 37 63  0  
  6   3713   369  3 79 18  0  
 24  16109   553 16 60 24  0  
  7    877   482  0 68 32  0  
 84    845   525  0 63 36  0  
 31    844   492  0 79 21  0  

谢谢

【问题讨论】:

  • 你能用awk这个吗?有很多问题需要寻找。

标签: bash moving-average


【解决方案1】:

在 AWK 中:

awk  'BEGIN { total = 0; count = 0 } { total += $3; count += 1; } END { avg = total / count; print avg} ' input.txt
  • $3 是第三个字段
  • input.txt 是你的大文件。

输出:

685.429

【讨论】:

  • 然后我可以在用 $$1 编辑 $3 之后在脚本中添加该行,因为 $1 是我的第一个参数.. 也可以用 $2 来检查 input.txt .... #the required command is # your_code 3 输入.txt
  • @mshafey 确定您可以制作一个 bash 脚本,将参数分配给一些变量并将它们用作此 AWK 脚本的一部分,这将由您来决定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
相关资源
最近更新 更多