将标准输出的最后一行重定向到文件中。

14

我在编译Scala代码并将控制台输出写入文件。我只想保存标准输出的最后一行到文件中。这是命令:

scalac -Xplugin:divbyzero.jar Example.scala >> output.txt

使用 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)

我只想将 Set(L, H) 这一部分保存到输出文件中,而不是保存全部内容。请问我可以使用哪个命令实现我的目标?

我只希望在输出文件中保存 Set(L, H) ,而不是其他内容。我该使用哪个命令才能达到这个目的?

5个回答

37

只需将标准输出通过 tail -n 1 传输到你的文件中即可


tail -1同样有效。 - Martin Braun

9
在Bash和其他支持进程替换的shell中:
command | tee  >(tail -n 1 > outputfile)

这将把完整的输出发送到stdout,将输出的最后一行写入文件。您可以像这样操作以将最后一行附加到文件而不是覆盖它:

command | tee  >(tail -n 1 >> outputfile)

8

您可以使用 tail 命令:

scalac -Xplugin:divbyzero.jar Example.scala | tail -1 >> output.txt

7
scalac ... | awk 'END{print>>"output.txt"}1'

这会将所有内容导出到标准输出,并将最后一行追加到output.txt中。

1
这个解决方案很巧妙,它回显输出并将最后一行发送到输出文件。如果您不关心查看输出:scalac ... | awk 'END{print}' >> output.txt - glenn jackman
通常,要获取最后几行,tail是“事实上”要使用的工具,特别是如果文件/输出非常大。在这种情况下,它仍然可以使用。 - kurumi

1

问题明确询问如何将**stdout**的最后一行写入文件中... 在这里您可以写入stderr的最后一行。 - donkopotamus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接