linux - 使用移动窗口 bash 脚本的中值

标签 linux bash windows-mobile median

我需要创建一个 txt 文件,其中包含变化的中值列表 如果我有一个像这样的文件:

  1. 1
  2. 2
  3. 35
  4. 40
  5. 50

我想要一个像这样的输出

  1. 1
  2. 1.5
  3. 2
  4. 18.5
  5. 35

我当时尝试从输入文件中选取一个数字,然后使用排序,但我没有走多远.. 我希望我说清楚了,谢谢!

最佳答案

你可以使用 awk 来实现这一点。例如,当您的数据位于名为 in.txt 的文件中时:

 awk '{c[NR]=$1; asort(c); if (NR%2) {print c[(NR+1)/2]} else {print (c[(NR/2)]+c[(NR/2)+1]) / 2.0}}' < in.txt

输出结果

1
1.5
2
18.5
35

关于linux - 使用移动窗口 bash 脚本的中值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075157/

相关文章:

linux - 在包含正斜杠的文件中使用 awk

linux - mmap和munmap,有时可以访问未映射的区域

linux - 无法从 guest 虚拟机上的 tomcat 获取 http(nat 连接)

bash - 将 2 个文件的内容与 md5sum 进行比较

python - 从 shell 脚本 cron 调用 python 脚本

android - 如何跟踪室内位置?

.net-3.5 - Async/await for Compact Framework v3.5 - 手动实现

linux - 如何远程停止ffmpeg?

bash - 如何将bash变量传递给字符串?

visual-studio - 可以在一台计算机上部署到 Windows Phone,但不能在另一台计算机上部署到 Windows Phone?