2017-06-22 29 views
2

我试图stdin只从1到1000从文件(output.txt)到while循环的行。bash命令重定向标准输入的while循环(使用sed)

我已经试过这样的事情:

#!/bin/bash 
while read -r line; do 
    echo "$line" 
done < (sed -n 1,1000p data/output.txt) 
+0

它会因为你是在'bash中运行这个是明智的,明确设置解释, '作为'#!/ bin/bash'或者它安装在你的机器上的任何地方 – Inian

回答

2

刚试过:

#!/bin/bash 
while read -r line; do 
    echo "$line" 
done < <(sed -n 1,1000p data/output.txt) 

增加另一个角度支架“<”的伎俩......如果有人能解释这可能是有趣的。

由于

+1

http://tldp.org/LDP/abs/html/process-sub.html –

+0

搜索'bash' [进程替换](http ://wiki.bash-hackers.org/syntax/expansion/proc_subst)和[input re-direction](http://wiki.bash-hackers.org/syntax/redirection#redirecting_input) – Inian

2

部分<(),被称为进程替换,它可以代替在一个命令的文件名。

fifos也可以用来做同样的事情。

mkfifo myfifo 

sed -n 1,1000p data/output.txt > myfifo & 

while read -r line; do 
    echo "$line" 
done < myfifo 
+0

谢谢!链接更多信息:https://linux.die.net/man/3/mkfifo – Jonathan

0

您似乎想要将输出从一个命令输出到另一个命令。 如果是的话,使用管道:

sed -n 1,1000p data/output.txt | while read -r line; do echo "$line"; done 

或者,使用正确的工具,合适的工作:

head -1000 data/output.txt | while read -r ; do something; done