如何在Shell中创建具有多个列的循环? [英] How to make a loop with multiple columns in shell?

查看:91
本文介绍了如何在Shell中创建具有多个列的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列(ID号,x,y)的文件

I have a file with three columns (ID number, 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

我想在第2列和第3列上进行循环,这样看起来就像

I would like to make a loop over column 2 and 3 so that is will read like

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

尽管我的以下脚本可以运行,但是我需要一种有效的方式.

Though my following script is working, but I need it in an efficient way.

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

请注意:myprog.f是在cat程序中编写的.例如,

Kindly Note: myprog.f is written within a cat program. for example,

cat << EOF > myprog.f
....
....
take value of x and y
....
....
EOF

推荐答案

在bash中读取文件的简单方法是

Simple way to read a file in bash is

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

这篇关于如何在Shell中创建具有多个列的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆