【发布时间】:2015-09-25 02:11:17
【问题描述】:
我有一个包含三列(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
我想使用 ID 号作为循环,这样读起来就像
when ID=1, then x=32.2, y=21.4
when ID=4, then x=33.2, y=43.5
and so on
我做了如下,但是看起来很漫长。
id1=1 #ID number
id2=$(wc -l < ifile.txt) #total number of IDs
while [ $id1 -le $id2 ]
do
ID=$(awk 'NR=='$id1' {print $1}' ifile.txt)
x=$(awk 'NR=='$id1' {print $2}' ifile.txt)
y=$(awk 'NR=='$id1' {print $3}' ifile.txt)
#
#here I want to do some computation using ID, x and y
#e.g. with each ID, I will execute a program by changing its corresponding x and y values
#for instance
echo $ID $x $y
(( id1++ ))
done
我需要缩短它和一个方便的方法。
【问题讨论】:
标签: linux shell loops unix awk