I have a Python CLI program that runs for a long time and has a sort of progressbar, which basically prints some text in the loop without "\n" at the end, at the next iteration it prints "\r" to erase the line, prints some text again and so on:
while some_condition:
print "\rprocessed {} out of {}".format(done_counter, all_counter),
It perfectly works in the console but when I redirect stdout to a file no surprise that I get lot's of "processed ... ... ^M". What I want is to get clean, "rendered" representation of the file as it would be seen in the console, with all that "^M" processed out. I don't want to just remove these control characters but rather apply them to text.
As a workaround currently I do this: tail -n<NUM_LINES> screenlog_file
with <NUM_FILES>
large enough, then select "rendered" text with mouse, copy that ant paste to new file. I wonder if there is any more convenient way to accomplish that.
EDIT
Owing to @Archemar, found this. It solves my current problem since I can modify the code. However any ideas/workarounds using only linux utils are highly appreciated.
tty()
equivalent, so you can print progress only if in a tty.less -r
orless -R
, but stripping the junk out of the log file (e.g. so you can use it as input to another program) is a much harder task. i can't find any right now but IIRC there have been a few similar questions about cleaning up ascript
logfile.\r
usage heresed 's,.*\r\(.\),\1,'
or maybeawk -F$'\r' '{print $NF}'
should do it. But in general showing text as your terminal emulator rendered it means running it through your terminal emulator,xterm -e cat file /dev/tty
will at least not litter your main scrollback buffer.