Matthias Guenther
Matthias Guenther

Reputation: 1683

Redirect only the last line of STDOUT to a file

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:

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

The output of scalac -Xplugin:divbyzero.jar Example.scala is:

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?

Upvotes: 14

Views: 26243

Answers (5)

Warnaud
Warnaud

Reputation: 199

Just a small precision regarding this tail command. If the program output on standard error, you have to redirect it

Example:

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

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

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360485

In Bash and other shells that support process substitution:

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:

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

Upvotes: 9

ephemient
ephemient

Reputation: 204926

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

This will pipe everything through to stdout and append the last line to output.txt.

Upvotes: 7

miku
miku

Reputation: 188134

You could use tail:

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

Upvotes: 8

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56408

Just pipe stdout through tail -n 1 to your file

Upvotes: 38

Related Questions