sed:无法向stdout中写入26个项目:管道损坏 [英] sed: couldn't write 26 items to stdout: Broken pipe

查看:60
本文介绍了sed:无法向stdout中写入26个项目:管道损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下命令:

cat input.txt | awk '{print $1, $6}' | sort -n | uniq -c | sort -nr | sed 's/"//g'| head -10

我得到了期望的输出,但是我得到了这个错误

i get the desired output, but i get this error

sed: couldn't write 26 items to stdout: Broken pipe

其中input.txt类似于:

where input.txt is something like:

192.168.2.20 - - [28/Jul/2006:10:27:10 -0300] "GET /cgi-bin/try/ HTTP/1.0" 200 3395
127.0.0.1 - - [28/Jul/2006:10:22:04 -0300] "GET / HTTP/1.0" 200 2216

我想念什么

推荐答案

正如@KamilCuk在评论中所说,这是因为 head -10 仅从管道中读取了前10行(加上可能有一些输入缓冲),然后将其关闭;如果输入足够大,则会在 sed 将所有内容写入管道之前发生(管道的缓冲区不足以吸收多余的空间).因此,是否发生这种情况取决于输入的大小,操作系统及其参数(确定管道的特性), sed 在其输出下降时的行为等.只需稍微改变一下内容即可足以避免该问题,例如:

As @KamilCuk said in a comment, this is happening because head -10 only reads the first 10 lines from the pipeline (plus maybe some input buffering), and then closes it; if the input is big enough, this happens before sed has written everything into the pipe (and the pipe's buffer isn't big enough to absorb the extra). So whether this happens or not depends on the input size, OS and its parameters (which determine the pipe's characteristics), sed's behavior on having its output dropped, etc. Just changing things up a bit may be enough to avoid the problem, for example:

...sort -nr | tr -d '"' | head -10       # use `tr` instead of `sed` -- it may behave differently
...sort -nr | head -10 | sed 's/"//g'    # swap `head` and `sed` -- now `sort`'s output is dropped

以下是避免出现的错误:

And here's one that will avoid the error:

...sort -nr | sed '11,$ d; s/"//g'

此方法的工作方式是告诉 sed 在输入("$")的末尾丢弃第11行,但是由于它会在 读取它们后丢弃它们(而是与从未读过它们(如 head -10 )一样, sort 的整个输出都会被读取,并且不会发生错误.

The way this works is it tells sed to discard lines 11 through the end of input ("$"), but since it discards them after reading them (rather than never reading them in the first place, like head -10), sort's entire output gets read and no error occurs.

BTW在管道的开头使用 cat 是没有用的;您应该让 awk 直接读取文件,如下所示:

BTW, as @triplee pointed out, using cat at the beginning of the pipeline is useless; you should have awk read the file directly, like this:

awk '{print $1, $6}' input.txt | ...

这篇关于sed:无法向stdout中写入26个项目:管道损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆