从 Awk 中读取 stderr
我想将 SSH 调试信息与其他输入分开(并记录)。但是,如果我只是将 stderr 重定向到日志文件,则可能会合并 SSH 的输出和主机上远程进程的输出(可能向 stderr 发送一些内容):
$ ssh -v somemachine 2> file.log
因此,我想过滤只输出那些与“debug1”匹配的行:
$ ssh -v somemachine | awk '/debug1/ {print > "file.log"; next} {print}'
到目前为止还不错,但是 ssh 的调试输出会发送到 stderr。所以……
$ ssh -v somemachine 2>& | awk '/debug1/ {print > "file.log"; next} {print}'
又失败了!我不想混合标准输出和标准错误。坏的!
像我这样的孩子会做什么?我本来打算走命名管道或类似的路线,但实际上,我需要知道的是如何让 awk 仅匹配来自 stderr 的模式。
I want to keep SSH debug info separate (and logged) from other input. However, if I simply redirect stderr to a log file, I risk combining output from SSH and output from the remote process on the host machine (that might send something to stderr):
$ ssh -v somemachine 2> file.log
So, I want to filter out only those lines that match "debug1":
$ ssh -v somemachine | awk '/debug1/ {print > "file.log"; next} {print}'
Good so far, BUT ssh's debug output goes to stderr. So...
$ ssh -v somemachine 2>& | awk '/debug1/ {print > "file.log"; next} {print}'
Foiled again! I don't want to mix stdout and stderr. BAD!
What does a kid like me do? I was about to go the route of named pipes or some such wildness, but really, all I need to know is how to get awk to match patterns from stderr ONLY.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
awk 实际上无法看到来自 stderr 的输入 - 管道仅管道 stdout。为了做你想做的事,你将不得不考虑重定向。如果您不关心标准输出上的内容,答案非常简单:
如果您关心标准输出上的内容,则必须做一些更复杂的事情。我相信这会起作用:
按顺序,这些重定向:
PS 这个解决方案是 sh/bash 特定的。 Jonathan Leffler 表示它也应该与 ksh 一起使用。如果您使用不同的 shell,您可以尝试找到语法来对其进行相同的重定向 - 但如果该不同的 shell 是 csh/tcsh,您可能就完蛋了。众所周知,cshell 不擅长处理 stdout/stderr。
Awk can't actually see the input from stderr - the pipe only pipes stdout. You're going to have to muck about with redirection in order to do what you want. The answer is really simple if you don't care about what's on stdout:
If you care about what's on stdout, you'll have to do something more complex. I believe that this will work:
In order, those redirects:
P.S. This solution is sh/bash-specific. Jonathan Leffler says it should work with ksh as well. If you're using a different shell, you can try to find the syntax to do the same redirects with it - but if that different shell is csh/tcsh, you're probably just screwed. The cshells are notoriously bad at handling stdout/stderr.
尝试一下(无子 shell):
或
参见此。
Give this a try (no subshells):
or
See this.
多谢..!我将 ls 与 awk 一起使用,但无法重定向。
我用过:
现在:
Thanks a lot..! I used ls with awk and was not able to redirect.
I used:
Now: