3

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.

5
  • 2
    It might be best adress in stack overflow, maybe python have tty() equivalent, so you can print progress only if in a tty.
    – Archemar
    Oct 23, 2015 at 8:27
  • @Archemar good point, found the Pythonic workaround. Since I have the code, I can patch it. However I believe that the question in general is of interest when dealing with a CLI program that can't be changed. Still thanks for your idea:)
    – DimG
    Oct 23, 2015 at 8:42
  • 1
    If you just want to view the log file, you can use less -r or less -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 a script logfile.
    – cas
    Oct 23, 2015 at 8:47
  • e.g. see unix.stackexchange.com/questions/14684/…
    – cas
    Oct 23, 2015 at 8:55
  • Given the \r usage here sed 's,.*\r\(.\),\1,' or maybe awk -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.
    – jthill
    Oct 23, 2015 at 12:31

1 Answer 1

0

For this sort of application, I run the program using script (to avoid changing its behavior when redirecting the output to a file), and then filter the resulting typescript file using a sed script. See for example, my answer in Can I programmatically “burn in” ANSI control codes to a file using unix utils?, discussing script2log.

There are other examples, such as those mentioned in comments. However the ones mentioned do not address this particular case (where carriage return is used to overstrike text), but only remove control characters.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .