Reputation: 1063
Im using a cool program in terminal but it's pre-compiled... Luckily all I need is the output of this system, but the way I need it is tricky... I need it to run normally but output the last line of text in the window to a text file. I jabs been looking around but people only make it so that I can log the whole thing, not just the last line.
it is a compiled unix executable that can't be run with something like that because it needs to keep running and won't stop until stopped, and that didn't work
Upvotes: 0
Views: 1135
Reputation: 287765
Use tail
to get only the last line:
cool-program | tail -n 1 > text-file
If cool-program
runs (and outputs) forever, redirect its output and tail then:
cool-program > log
# Later, when you're interested in the current last line:
tail -n 1 log
Upvotes: 2
Reputation: 129735
You can use the tail
command to only capture the last n
lines of a file or a stream. For example, to run ./myProgram
but only display the last line of output:
./myProgram | tail -n 1
This pipes (|
) the output of your program into the input of tail
, which discards everything except the last n
lines.
To save that output to a file instead, you can redirect (>
) tail
's output to a file:
./myProgram | tail -n 1 > out.txt
Upvotes: 1