Redirect only last line of STDOUT to file

I compile Scala code and write the output of the output console to a file. I want to save only the last line of STDOUT in a file. Here is the command:

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

Output file 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 just want to save pc: Set (L, H) in the output file, and not the rest. With which team can I achieve my goal?

+4
source share
5 answers

Just pass stdout via tail -n 1 to your file

+17
source

You can use tail :

 scalac -Xplugin:divbyzero.jar Example.scala | tail -1 >> output.txt 
+6
source
 scalac ... | awk 'END{print>>"output.txt"}1' 

This will pass everything through stdout and add the last line to output.txt.

+6
source

In Bash and other shells that support process overrides:

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

will send the full output to stdout and the last line of output to a file. You can do this to add the last line to the file, rather than overwriting it:

 command | tee >(tail -n 1 >> outputfile) 
+4
source

Just a little accuracy regarding this tail command. If the program displays a standard error, you need to redirect it

Example:

 apachectl -t 2>&1 | tail -n 1 

Redirects: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

+1
source

Source: https://habr.com/ru/post/1337384/


All Articles