Score:0

How to create a file with the following structure?

fj flag

Consider the following rows:

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

where i ranges from 0 to 100000.

I would like to make the file composed of such rows, i.e.

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
...

Could you please tell me how can I do this?

My approach starts from creating a script file in which I type,

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

But clearly there is something wrong, as i stays as i in the generated file, and the number of strings is much smaller than expected (4*100000).

Score:3
cn flag

You want to keep the number of occurrences inside the loop to a minimum (open and closing file.txt 100K x4 will really stress your system without any good reason).

#!/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
fj flag
Thanks! However, i stays as i inside the string.
bac0n avatar
cn flag
I don't follow?
fj flag
my output is 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 E i -1 -1.0000000000000000e+00 -1.0000000000000000e+00 -1.0000000000000000e+00 0 0 1 0 0 0 0
fj flag
I.e., i is not replaced with the integer number.
bac0n avatar
cn flag
didn't see it `"'E $i -1 -1.0000000000000000e+00..."` is this the only `i`
bac0n avatar
cn flag
this will go from 0 to 999999 you could also do `i=1` and use `<=` inside the while loop to get the range {1.100000}

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.