You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bash中编写单行命令合并多文件中Mary的汇总统计结果

Here's a single Bash command that combines Mary's summary statistics from multiple files into your desired report format:

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

How it works:

  1. Capture Input1's full summary: We run your existing command for input1.txt and store the entire output (header + stats line) in a variable input1_output. This avoids reprocessing input1.txt twice.
  2. Print Input1's summary: Echo the stored variable to output the header and the first stats line.
  3. Process Input2's stats: Run the same command for input2.txt, but use tail -n1 to only capture the second line (the stats values, skipping the duplicate header), then print it.

If you have more than two files (e.g., input3.txt, input4.txt), you can extend this with a loop to avoid repeating the command for each file:

header=$(awk '/Mary/{for(n=3;n<=NF;n++) print $n}' input1.txt | Rscript -e 'summary(as.numeric(readLines("stdin")))' | head -n1); echo "$header"; for f in input1.txt input2.txt input3.txt; do awk '/Mary/{for(n=3;n<=NF;n++) print $n}' "$f" | Rscript -e 'summary(as.numeric(readLines("stdin")))' | tail -n1; done

This approach ensures the header is only printed once, followed by the summary stats line for each file containing Mary's data.

内容的提问来源于stack exchange,提问作者LockhartTech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:17:48