Home Save last line of output to log file in linux
Reply: 2

Save last line of output to log file in linux

Velucio Published in 2018-02-13 18:40:24Z

Is there any way to always save only the last "console" output to log file?

For example executing this

python3 program.py > log.txt

It will always store all the console prints to log.txt. Even if inside python print has \r.

Any suggestions?

EDIT. Just to be clear, I want to log the last of the output BUT I don't want to lose any data. For example, using tail indeed work but it will log the data only after the execution is over. I want to save the last line run time.

Picaud Vincent
Picaud Vincent Reply to 2018-02-13 21:14:04Z

I think something like

python3 program.py | tail -1 > log.txt 

should work.

Update: after your post "EDIT", if I understand well, you want to keep

python3 program.py > log.txt

to save all your logs, but in the same time, you want to see the last line. Is it correct?

In that case you can do from another terminal:

watch -n 2 tail -n 1 log.txt

This will print the last line of log.txt, in real time (refreshed every 2sec, but this is configurable, man watch)

Gerik Reply to 2018-02-13 19:01:34Z

If you do have an explicit string you can capture you might try:

python3 program.py | grep "string" > log.txt
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313029 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO