2017-06-22 87 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