6

I have an Executable which shows progress bar on terminal. when i redirect the output to a file in th ebelow manner

the below command is embedded in a script file

usr/bin/exec >> log.txt

this is terminal out put

Progress [====================================>                          ]  58%

But the log.txt looks some thing like this

Progress [=>                          ]  0
Progress [====>                          ]  5
Progress [========>                          ]  10

can i redirect the output in same way as it is shown on terminal.

user2598064
  • 157
  • 1
  • 13

2 Answers2

4

A related answer on a different site, suggests the col command.

To suppress overwritten output on standard out prior to writing to the file:

usr/bin/exec | col -b >> log.txt

To view the contents of a file while suppressing overwritten characters:

col -b < log.txt | less

Your progress bar probably works by using a carriage return to signal that the current line should be overwritten. The col utility buffers the output so that it can print only the last character for each position.

Community
  • 1
  • 1
teichert
  • 3,963
  • 1
  • 31
  • 37
1

There's no way to answer this with certainty without knowing exactly what your progress-bar program is doing, but...

The way such a progress bar is typically done is by outputting the current progress update, but then putting an ascii CR (carriage return) at the end of the line without an LF (line feed). This resets the display cursor to the beginning of the line without moving the cursor down to the next line so that each subsequent progress update overwrites the previous one. So probably, if you're redirecting that to a file, you'll have the same content in the file.

If you were to view such a file with vi, you'll actually see the embedded CRs, indicated as "^M" (but note that LF characters are simply interpreted as end-of-line and aren't actually displayed). If you view the file with another editor, it might interpret CR characters as a valid end-of-line indicator and show you each line as a separate line with or without LF characters.

You don't say how you're displaying the log.txt file. I would expect that if the program were using the embedded CR method, then using "cat log.txt" would show the same output as in interactive use: that is, each progress update "line" would overwrite the previous one and only the last one would be visible at the end.

It's also possible that the program outputting the progress bar is sensitive to whether it's displaying to a terminal device or not: i.e. it might be calling isatty() and modifying its output accordingly.

It might be instructive to run this command on your final log.txt file to see the actual binary content:

od -tx1z log.txt

A CR (aka ^M) will show up as "0d". An LF (aka ^J) will show up as 0a.

Gil Hamilton
  • 11,973
  • 28
  • 51