【问题标题】:Combining summary statistics from multiple input files in Bash在 Bash 中组合来自多个输入文件的汇总统计信息
【发布时间】:2021-09-18 18:14:53
【问题描述】:

我想根据多个文件中的数据为“Mary”生成一些摘要统计信息。

input1.txt 看起来像

Jose 88518 95 75 95 62 100 78 68 
Alex 97502 84 79 80 73 88 95 79 85 93 
Mary 98765 80 75 100 51 83 75 99 50 75 89 94
...

input2.txt 看起来像

Jack 32954 100 98 95 100 93 100 99 98 100 100
Mary 98765 85 83 96 77 81 84 98 75 87
Lisa 83746 100 100 100 100 99 100 98 100 100 100
...

在 Bash 中为 input1.txt 运行以下单行代码:

awk '/Mary/{for(n=3;n<=NF;n++) print $n}' input1.txt | Rscript -e 'summary (as.numeric (readLines ("stdin")))'

结果是:

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  50.00   75.00   80.00   79.18   91.50  100.00

为 input2.txt 运行以下代码:

awk '/Mary/{for(n=3;n<=NF;n++) print $n}' input2.txt | Rscript -e 'summary (as.numeric (readLines ("stdin")))'

结果是:

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 75.00   81.00   84.00   85.11   87.00   98.00 

我如何编写一个单行解决方案,将每个数据文件中的“Mary”统计信息合并到一个报告中,从而产生类似于以下内容的结果?

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.   
 50.00   75.00   80.00   79.18   91.50  100.00
 75.00   81.00   84.00   85.11   87.00   98.00      

【问题讨论】:

  • 这个问题正在meta讨论。

标签: bash awk one-liner


【解决方案1】:

我认为您需要使用 bash for 循环。

for file in $(ls input*.txt); do awk '/Mary/{for(n=3;n<=NF;n++) print $n}' $file | Rscript -e 'summary (as.numeric (readLines ("stdin")))'; done

您现在可能会以两个标题结束,但由于我们无法了解标题的创建方式,因此很难提出建议。 Min. 1st Qu. Median Mean 3rd Qu. Max.

【讨论】:

  • 这行不通。我相信这个解决方案在结合两个文件中玛丽的数字后会产生一行统计数据。预期结果是两行统计数据,其中每一行从每个输入文件中 Mary 的数据生成统计数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多