2012-05-14 61 views
1
while read line;do 
read header_line < headers.txt 
task 1 
task 2 
done < temp1.txt 

我想被读取从文件中读取行线后同时headers.txt和temp1.txt。所以,例如,如果我从temp1.txt读取一行,我还应该从headers.txt中读取一行。这段代码的问题在于它从temp1.txt中逐行读取行,但它始终从headers.txt读取相同的行。我希望文件指针在读取header_line后移动到下一行。我如何去做呢?移动文件指针从文件

回答

1

可能是你可以替换该行

read header_line < headers.txt 

有:

sed -n '${count}p' hearders.txt 

其中数累加在每一次迭代while循环,应初始化为1

+0

如果我想将sed输出存储在变量中?并将计数变量自动增加?即时通讯unix中的新手:D @皮特 – user1356163

+0

sed:命令乱码:$ {count} p – user1356163

+0

sed -n“$ {count} p”headers.txt。应该是双引号 – user1356163

3

使用一个额外的文件描述符,然后在do

#!/bin/bash 

# open extra file descriptors for input 
exec 3< headers.txt 

while read -r line; read -r -u 3 header_line 
do 
    echo "[$header_line] [$line]" 
done < temp1.txt 

# close the extra file descriptor 
exec 3<&-