bash 仅将 STDOUT 的最后一行重定向到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4821731/
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
Redirect only the last line of STDOUT to a file
提问by Matthias Guenther
I'm compiling Scala code and write the output console output in file. I only want to save the last line of the STDOUT in a file. Here is the command:
我正在编译 Scala 代码并将输出控制台输出写入文件。我只想将 STDOUT 的最后一行保存在一个文件中。这是命令:
scalac -Xplugin:divbyzero.jar Example.scala >> output.txt
The output of scalac -Xplugin:divbyzero.jar Example.scala is:
scalac -Xplugin:divbyzero.jar Example.scala 的输出是:
helex@mg:~/git-repositories/my_plugin$ scalac -Xplugin:divbyzero.jar Example.scala | tee -a output.txt
You have overwritten the standard meaning
Literal:()
rhs type: Int(1)
Constant Type: Constant(1)
We have a literal constant
List(localhost.Low)
Constant Type: Constant(1)
Literal:1
rhs type: Int(2)
Constant Type: Constant(2)
We have a literal constant
List(localhost.High)
Constant Type: Constant(2)
Literal:2
rhs type: Boolean(true)
Constant Type: Constant(true)
We have a literal constant
List(localhost.High)
Constant Type: Constant(true)
Literal:true
LEVEL: H
LEVEL: H
okay
LEVEL: H
okay
false
symboltable: Map(a -> 219 | Int | object TestIfConditionWithElseAccept2 | normalTermination | L, c -> 221 | Boolean | object TestIfConditionWithElseAccept2 | normalTermination | H, b -> 220 | Int | object TestIfConditionWithElseAccept2 | normalTermination | H)
pc: Set(L, H)
And I only want to save pc: Set(L, H) in the output file and not the rest. With the help of which command I can achieve my goal?
我只想在输出文件中保存 pc: Set(L, H) 而不是其余的。在哪个命令的帮助下我可以实现我的目标?
回答by Daniel DiPaolo
Just pipe stdout through tail -n 1to your file
只需通过管道标准输出tail -n 1到您的文件
回答by miku
回答by ephemient
scalac ... | awk 'END{print>>"output.txt"}1'
This will pipe everything through to stdout andappend the last line to output.txt.
这将管道通过一切到标准输出,并追加的最后一行到output.txt。
回答by Paused until further notice.
In Bash and other shells that support process substitution:
在 Bash 和其他支持进程替换的 shell 中:
command | tee >(tail -n 1 > outputfile)
will send the complete output to stdout and the last line of the output to the file. You can do it like this to append the last line to the file instead of overwriting it:
将完整的输出发送到 stdout 并将输出的最后一行发送到文件。您可以这样做以将最后一行附加到文件而不是覆盖它:
command | tee >(tail -n 1 >> outputfile)
回答by Warnaud
Just a small precision regarding this tail command. If the program output on standard error, you have to redirect it
只是关于这个 tail 命令的一个小精度。如果程序输出标准错误,则必须重定向它
Example:
例子:
apachectl -t 2>&1 | tail -n 1
Redirections: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

