Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 通过sed的管道故障_Bash_Shell_Sed_Pipe - Fatal编程技术网

Bash 通过sed的管道故障

Bash 通过sed的管道故障,bash,shell,sed,pipe,Bash,Shell,Sed,Pipe,我在通过sed时遇到问题。一旦我将输出传输到sed,我就不能将sed的输出传输到其他地方 wget -r -nv http://127.0.0.1:3000/test.html 产出: 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.tx

我在通过sed时遇到问题。一旦我将输出传输到sed,我就不能将sed的输出传输到其他地方

wget -r -nv http://127.0.0.1:3000/test.html
产出:

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]
http://127.0.0.1:3000/test.html
http://127.0.0.1:3000/robots.txt
http://127.0.0.1:3000/shop
我通过sed管道输出,以获得一个干净的URL列表:

wget -r -nv http://127.0.0.1:3000/test.html 2>&1 | grep --line-buffered -v ERROR | sed 's/^.*URL:\([^ ]*\).*/\1/g'
产出:

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]
http://127.0.0.1:3000/test.html
http://127.0.0.1:3000/robots.txt
http://127.0.0.1:3000/shop
然后我想将输出转储到文件,因此我执行以下操作:

wget -r -nv http://127.0.0.1:3000/test.html 2>&1 | grep --line-buffered -v ERROR | sed 's/^.*URL:\([^ ]*\).*/\1/g' > /tmp/DUMP_FILE
我在几秒钟后中断进程并检查文件,但文件是空的

有趣的是,以下结果没有输出(与上面相同,但通过cat输出):


为什么我不能将sed的输出通过管道传输到另一个程序,如cat?

当sed写入另一个进程或文件时,它会缓冲数据


尝试将
--unbuffered
选项添加到sed。

您也可以使用awk。由于您的URL显示在字段3中,因此您可以使用$3,也可以删除grep

awk '!/ERROR/{sub("URL:","",$3);print $3}' file

sed应该可以很好地处理管道,例如:echo“foo”| sed的s/foo/bar/g'>/tmp/foo适合我。在sed中添加-u选项是否会产生影响?或者,在检查文件之前,尝试让流程完成。sed可能只是对结果进行了太多的内部缓冲。为什么不从今天开始呢?:)