如何创建具有以下结构的文件?

如何创建具有以下结构的文件?

考虑以下行:

E i -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0
U GEV MM
V -1 0 0 0 0 0 0 1 0
P 1 535 0.0000000000000000e+00 0.0000000000000000e+00 1.3330000000000000e+00 1.036606429653994e+01 1.02800000000000000e+01 0 0 0 0 0

其中 i 的范围是 0 至 100000。

我想让文件由这样的行组成,即

E 1 -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0
U GEV MM
V -1 0 0 0 0 0 0 1 0
P 1 535 0.0000000000000000e+00 0.0000000000000000e+00 1.3330000000000000e+00 1.036606429653994e+01 1.02800000000000000e+01 0 0 0 0 0
E 1 -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0
U GEV MM
V -1 0 0 0 0 0 0 1 0
P 1 535 0.0000000000000000e+00 0.0000000000000000e+00 1.3330000000000000e+00 1.036606429653994e+01 1.02800000000000000e+01 0 0 0 0 0
E 2 -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0
    U GEV MM
    V -1 0 0 0 0 0 0 1 0
    P 1 535 0.0000000000000000e+00 0.0000000000000000e+00 1.3330000000000000e+00 1.036606429653994e+01 1.02800000000000000e+01 0 0 0 0 0
...

你能告诉我怎样做吗?

我的方法是从创建一个脚本文件开始,在其中输入,

touch file.txt
for i in {0.. 100000 }; 
do echo 'E i -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0' >> file.txt;
echo 'U GEV MM' >> file.txt
echo '-1 0 0 0 0 0 0 1 0' >> file.txt
echo 'P 1 535 0.0000000000000000e+00 0.0000000000000000e+00 1.3330000000000000e+00 1.036606429653994e+01 1.02800000000000000e+01 0 0 0 0 0' >> file.txt
done

但显然这里有些错误,因为在生成的文件中 i 仍然是 i ,而且字符串的数量比预期的要小得多(4*100000)。

答案1

您希望将循环内的发生次数保持在最低限度(打开和关闭 file.txt 100K x4 确实会毫无理由地给您的系统带来压力)。

#!/bin/bash

declare -a a
i=0
while ((i++ < 100000)); do
    a+=("E $i -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0" 'U GEV MM' '-1 0 0 0 0 0 0 1 0' 'P 1 535 0.0000000000000000e+00 0.0000000000000000e+00 1.3330000000000000e+00 1.036606429653994e+01 1.02800000000000000e+01 0 0 0 0 0')
done
printf '%s\n%s\n%s\n' "${a[@]}" > file.txt

相关内容