Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect only the last line of STDOUT to a file

Tags:

bash

shell

unix

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?

like image 684
Matthias Guenther Avatar asked Jan 27 '11 20:01

Matthias Guenther


People also ask

How do I redirect output from stdout to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

How do you select the last line in Linux?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.

What does 2 &1 at the end of a command do?

The 1 denotes standard output (stdout). The 2 denotes standard error (stderr). So 2>&1 says to send standard error to where ever standard output is being redirected as well.

What is the last line of output on running the code?

The grader compiles a program and runs it. If the program fails to compile or fails to run (e.g. due to a segmentation fault) the grade should be a fixed small number, e.g. 5. Otherwise, the grade is the last line output by the program.


1 Answers

Just pipe stdout through tail -n 1 to your file

like image 164
Daniel DiPaolo Avatar answered Oct 18 '22 15:10

Daniel DiPaolo