bash 通过 sed 管道的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2427338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble with piping through sed
提问by Joel
I am having trouble piping through sed. Once I have piped output to sed, I cannot pipe the output of sed elsewhere.
我在通过 sed 进行管道传输时遇到问题。一旦我将输出通过管道传输到 sed,我就无法将 sed 的输出通过管道传输到其他地方。
wget -r -nv http://127.0.0.1:3000/test.html
Outputs:
输出:
2010-03-12 04:41:48 URL:http://127.0.0.1:3000/test.html [99/99] -> "127.0.0.1:3000/test.html" [1]
2010-03-12 04:41:48 URL:http://127.0.0.1:3000/robots.txt [83/83] -> "127.0.0.1:3000/robots.txt" [1]
2010-03-12 04:41:48 URL:http://127.0.0.1:3000/shop [22818/22818] -> "127.0.0.1:3000/shop.29" [1]
I pipe the output through sed to get a clean list of URLs:
我通过 sed 管道输出以获得一个干净的 URL 列表:
wget -r -nv http://127.0.0.1:3000/test.html 2>&1 | grep --line-buffered -v ERROR | sed 's/^.*URL:\([^ ]*\).*//g'
Outputs:
输出:
http://127.0.0.1:3000/test.html
http://127.0.0.1:3000/robots.txt
http://127.0.0.1:3000/shop
I would like to then dump the output to file, so I do this:
我想然后将输出转储到文件,所以我这样做:
wget -r -nv http://127.0.0.1:3000/test.html 2>&1 | grep --line-buffered -v ERROR | sed 's/^.*URL:\([^ ]*\).*//g' > /tmp/DUMP_FILE
I interrupt the process after a few seconds and check the file, yet it is empty.
几秒钟后我中断该过程并检查文件,但它是空的。
Interesting, the following yields no output (same as above, but piping sed output through cat):
有趣的是,以下没有输出(与上面相同,但通过 cat 管道 sed 输出):
wget -r -nv http://127.0.0.1:3000/test.html 2>&1 | grep --line-buffered -v ERROR | sed 's/^.*URL:\([^ ]*\).*//g' | cat
Why can I not pipe the output of sed to another program like cat?
为什么我不能将 sed 的输出通过管道传输到另一个程序,比如 cat?
回答by R Samuel Klatchko
When sed is writing to another process or to a file, it will buffer data.
当 sed 写入另一个进程或文件时,它会缓冲数据。
Try adding the --unbufferedoptions to sed.
尝试将--unbuffered选项添加到 sed。
回答by ghostdog74
you can also use awk. since your URL appears in field 3, you can use $3, and you can remove the grep as well.
你也可以使用awk。由于您的 URL 出现在字段 3 中,您可以使用 $3,并且您也可以删除 grep。
awk '!/ERROR/{sub("URL:","",);print }' file

