我有一个包含三列(ID 号、x、y)的文件
ifile.txt
1 32.2 21.4
4 33.2 43.5
5 21.3 45.6
12 22.3 32.5
32 21.5 56.3
43 33.4 23.4
44 23.3 22.3
55 22.5 32.4
Run Code Online (Sandbox Code Playgroud)
我想在第 2 列和第 3 列上做一个循环,这样读起来就像
for x=32.2 and y=21.4; do execute a fortran program
for x=33.2 and y=43.5; do execute the same program
and so on
Run Code Online (Sandbox Code Playgroud)
尽管我的以下脚本正在运行,但我需要以有效的方式使用它。
s1=1 #serial number
s2=$(wc -l < ifile.txt) #total number to be loop
while [ $s1 -le $s2 ]
do
x=$(awk 'NR=='$s1' {print $2}' ifile.txt)
y=$(awk 'NR=='$s1' {print $3}' ifile.txt)
cat << EOF > myprog.f
...
take value of x and y
...
EOF
ifort myprog.f
./a.out
(( s1++ ))
done
Run Code Online (Sandbox Code Playgroud)
请注意:myprog.f 是在 cat 程序中编写的。例如,
cat << EOF > myprog.f
....
....
take value of x and y
....
....
EOF
Run Code Online (Sandbox Code Playgroud)
在 bash 中读取文件的简单方法是
while read -r _ x y; do
echo "x is $x, y is $y"
# your Fortran code execution
done < ifile.txt
x is 32.2, y is 21.4
x is 33.2, y is 43.5
x is 21.3, y is 45.6
x is 22.3, y is 32.5
x is 21.5, y is 56.3
x is 33.4, y is 23.4
x is 23.3, y is 22.3
x is 22.5, y is 32.4
Run Code Online (Sandbox Code Playgroud)