python - 从一个文件的提取内容创建文件

标签 python bash matplotlib awk

我有一个大文件,其中包含基于所用进程基准 案例数量的信息。所有这些信息都在同一个文件中一个接一个地出现。

    --
# Benchmarking Allgather
# #processes = 8
# ( 3592 additional processes waiting in MPI_Barrier)
#----------------------------------------------------------------
       #bytes #repetitions  t_min[usec]  t_max[usec]  t_avg[usec]
            0         1000         0.05         0.05         0.05
            1         1000         1.77         2.07         1.97
            2         1000         1.79         2.08         1.97
            4         1000         1.79         2.07         1.98
            8         1000         1.82         2.12         2.01
--
# Benchmarking Allgather
# #processes = 16
# ( 3584 additional processes waiting in MPI_Barrier)
#----------------------------------------------------------------
       #bytes #repetitions  t_min[usec]  t_max[usec]  t_avg[usec]
            0         1000         0.05         0.05         0.05
            1         1000         2.34         2.85         2.73
            2         1000         2.36         2.87         2.74
            4         1000         2.38         2.90         2.76
            8         1000         2.42         2.95         2.79

为了快速绘制信息,我计划为每个独立内容创建一个文件,例如,根据上面给出的信息,我将创建两个名为“Allgather_8”和“Allgather_16”的文件,这些文件的预期内容将是:

$cat Allgather_8
  #bytes #repetitions  t_min[usec]  t_max[usec]  t_avg[usec]
            0         1000         0.05         0.05         0.05
            1         1000         1.77         2.07         1.97
            2         1000         1.79         2.08         1.97
            4         1000         1.79         2.07         1.98
            8         1000         1.82         2.12         2.01
$cat Allgather_16
 #bytes #repetitions  t_min[usec]  t_max[usec]  t_avg[usec]
            0         1000         0.05         0.05         0.05
            1         1000         2.34         2.85         2.73
            2         1000         2.36         2.87         2.74
            4         1000         2.38         2.90         2.76
            8         1000         2.42         2.95         2.79

然后我可以使用 gnuplotmatplotlib 绘制它。

到目前为止我尝试了什么:

我一直在使用 grep 和 awk 来提取内容,这适用于独立的部分,但我不知道如何自动执行此操作。

有什么想法吗?

最佳答案

awk '
/Benchmarking/ { close(out); out = $NF }
/#processes/   { out = out "_" $NF }
/^[[:space:]]/ { print > out }
' file 

关于python - 从一个文件的提取内容创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44462845/

相关文章:

bash - bash脚本在多个docker-compose环境中运行测试

linux -\n 在 linux 文件中不起作用

python - Matplotlib:避免 X 轴拥塞

python - 在 Python 中加速 Stanford 依赖解析

python - 如何在 QTabWidget 中设置背景颜色/图像特定选项卡?

python - 无法在 anaconda 3 (python 3), linux 中安装 pygame

python - 尝试安装 mysql 时获取 "ERROR: Command errored out with exit status 1:"

bash - 如何在 UNIX 中将 "19-FEB-12"之类的字符串转换为纪元日期

python - 循环绘图,仅获取最后一个绘图

python - 如何将图例添加到 matplotlib 事件图中?