如何在单个循环的每次迭代中从多个文件的每一个中读取一行? [英] How to read a line from each of several files in each iteration of a single loop?
问题描述
我需要在一个循环中读取多个文件.我有一个带有X坐标的文件,一个带有Y坐标的文件,一个带有这些坐标的字符的文件.
I need to read from multiple file in one loop. I have one file with X coords, one file with Y coords and one file with chars on those coords.
现在我使用paste
将这些文件放在一起,然后在while循环中使用cut
这样将它们分开:
For now I use paste
to put these files together, and then in a while loop use cut
to separate them like this:
paste x-file y-file char-file | while -r line; do
Y=$(echo "$line" | cut -d\ -f1)
X=$(echo "$line" | cut -d\ -f2)
CHAR=$(echo "$line" | cut -d\ -f3)
done
但是问题是它真的很慢(一次又一次地调用cut
).
but the problem with this is that it's really slow (calling cut
again and again).
在加快速度的同时,如何在每个循环中在$Y
,$X
和$CHAR
中具有适当的值?
How should I do this to have in each loop proper values in $Y
, $X
and $CHAR
while speeding things up?
推荐答案
我不确定在没有paste
的情况下怎么做,但是您可以避免cut
在一个read
中分配所有变量:
I'm not sure how to do it without paste
, but you can avoid cut
assigning all variables in one read
:
while read -r X Y CHAR; do
echo "X = $X; Y = $Y; CHAR = $CHAR";
done < "$file"
这篇关于如何在单个循环的每次迭代中从多个文件的每一个中读取一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!