Okay, so what I'm trying to do is print out a percentage complete to my command line, now, I would like this to simply 'update' the number shown on the screen. So somehow go back to the beginning of the line and change it.
For example the windows relog.exe command-line utility (which can convert a .blg file to a .csv file) does this. If you run it, it will display a percentage complete.
Now this is probably written in C++. I don't know if this is possible in perl as well ?
In C and C++, the trick is to print char #13. Maybe it can work in Perl.
Depending on what you'd like to do,
pv
might solve your problem. It can wrap any script that takes a file as input, and add a progress bar.For example
pv
is packaged for RedHat/CentOS and Ubuntu at least. More information: http://www.ivarch.com/programs/pv.shtmlOtherwise I'd use CPAN, e.g. Term::ProgressBar.
You can also use \b to move back one character:
Use "\r" or "\015" octal (aka "Return caret" aka "Carriage Return" character originating from typewriter days :)
Just don't forget to print at least as many characters as the longest string already printed to overwrite any old characters (as you can see in example above, the failure to do so will keep old characters).
Another thing to be aware of is, as Michael pointed in the commment, the autoflush needs to be turned on while these printings happen, so that the output doesn't wait for newline character at the very end of the processing.
UPDATE: Please note that 013 octal character recommended in another answer is actually a Vertical Tab: