我需要在一个循环中读取多个文件.我有一个带有X坐标的文件,一个带有Y坐标的文件和一个带有这些坐标上的字符的文件.
现在我paste
用来将这些文件放在一起,然后在while循环中使用cut
它们将它们分开:
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
Run Code Online (Sandbox Code Playgroud)
但问题是它真的很慢(cut
一次又一次地打电话).
我应该如何做到这一点,在每个循环中有适当的值$Y
,$X
并$CHAR
加快速度?
您可以read
从不同的文件描述符中调用三次.
# Use && to stop reading once the shortest file is consumed
while read -u 3 -r X &&
read -u 4 -r Y &&
read -u 5 -r CHAR; do
...
done 3< X.txt 4< Y.txt 5< CHAR.txt
Run Code Online (Sandbox Code Playgroud)
(-u
是一个bash
扩展名,用于清晰.对于POSIX兼容性,每个调用看起来都像read -r X <&3
.)