仅将STDOUT的最后一行重定向到文件

Mat*_*her 9 unix bash shell

我正在编译Scala代码并在文件中写出输出控制台输出.我只想在文件中保存STDOUT的最后一行.这是命令:

scalac -Xplugin:divbyzero.jar Example.scala >> output.txt
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我只想保存pc:在输出文件中设置(L,H)而不是其余部分.在哪个命令的帮助下,我可以实现我的目标?

Dan*_*olo 28

只需将标准输出tail -n 1到您的文件即可


mik*_*iku 6

你可以使用tail:

scalac -Xplugin:divbyzero.jar Example.scala | tail -1 >> output.txt
Run Code Online (Sandbox Code Playgroud)


eph*_*ent 6

scalac ... | awk 'END{print>>"output.txt"}1'
Run Code Online (Sandbox Code Playgroud)

这会将所有内容传递给stdout ,并将最后一行追加到output.txt.


Pau*_*ce. 6

在 Bash 和其他支持进程替换的 shell 中:

command | tee  >(tail -n 1 > outputfile)
Run Code Online (Sandbox Code Playgroud)

将完整的输出发送到 stdout 并将输出的最后一行发送到文件。您可以这样做以将最后一行附加到文件而不是覆盖它:

command | tee  >(tail -n 1 >> outputfile)
Run Code Online (Sandbox Code Playgroud)