sed and tac: write error: Invalid argument

sed and tac: write error: Invalid argument

我在 Windows 上使用 GNU Coreutils。我想从日志文件中获取最后一个错误行

SED 正则表达式工作正常(但在文件中找到第一个条目)

c:\path\sed -n "/70\-Error/{p;q}" MyFile.log

TAC 命令

c:\path\tac MyFile.log

成功反转整个文件,没有错误

但是当我将两者结合起来时,我从文件输出中得到了最后一个匹配行,正如预期的那样,但也有一个错误:

c:\path\tac MyFile.log | c:\path\sed -n "/70\-Error/{p;q}"

给予

14/02/2018 10:19:03 [9] 70-Error: Errorcode: Query returned error 524 (Query timeout)
c:\path\tac: write error: Invalid argument

编辑:

sed -n "p" MyFile.log

给出输出

sed -n "p" MyFile.log | cat

也给出输出,没有错误

sed -n "p" MyFile.log | sed -n "/70\-Error/{p;q}"

给出第一个匹配行和错误:

30/01/2018 10:30:28 [7] 70-Error: Action failed ....
sed: couldn't write 225 items to stdout: Broken pipe

使用 CAT 代替 TAC:

cat MyFile.log | sed -n "/70\-Error/{p;q}"

首先给出匹配行,然后给出错误 - 看起来与 TAC 场景相同:

30/01/2018 10:30:28 [7] 70-Error: Action failed ...
cat: write error: Invalid argument

其他测试:

sed -n "/fe80::a828:2146:58d1:28df/{p}" MyFile.log

很好但是

sed -n "/fe80::a828:2146:58d1:28df/{p}" MyFile.log | tac

没有输出,只有这个错误:

sed: couldn't flush stdout: Invalid argument

我认为问题可能与以下事实有关:MyFile.log 具有完整路径,包括空格,因此被引用 - 所以我将 MyFile.log 复制到与 GNU 工具相同的文件夹中一切都在当前文件夹中,没有复杂的文件名……但都是一样的。

sed -n "p" MyFile.log | tac

从文件中以相反的顺序给出(第一个)25 行,然后:

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

可能是某种内存/缓存大小限制?

sed --unbuffered

没有任何改变。

MyFile.log 是 4MB 和 27,000 行

EDIT2:

tac MyFile.log | sed "/70\-Error/h;$!d;x"

工作正常,即使引用 MyFile.log 包含空格

这可能对你有用 (GNU sed):

sed '/70\-Error/h;$!d;x' file

这会将错误消息复制到保留区 space 并在文件末尾打印出来。

N.B。使用 \ 表示 \,因为单个反斜杠是 sed 中的引号元字符。

要打印最后一条错误消息之后的所有行,请使用:

sed '/70-Error/h;//!H;$!d;x' file

要打印最后一条错误消息之后的选定行数,例如 3,请使用:

sed '/70-Error/h;//!H;$!d;x;s/\(\(\n[^\n]*\)\{3\}\).*//' file

或者,更养眼:

sed -r '/70-Error/h;//!H;$!d;x;s/((\n[^\n]*){3}).*//' file