thornton102 has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks, This is embarrassing, but I've never had a need for it until now. I humbly ask: I need to print a number at the bottom of the Unix shell, then update it in the very same position to show progress. My program's going through a 700MB text file and I need to give the user some feedback. Thank you for enlightening me between fits of laughter.
  • Comment on updating something in the same position

Replies are listed 'Best First'.
Re: updating something in the same position
by Fletch (Bishop) on Aug 03, 2010 at 14:24 UTC

    The crude solution is to just use an ASCII carriage return ("\cM") then print over the previous output (you want to use a fixed width format with leading or trailing spaces as appropriate so that the prior output is "overwritten" each pass). Fancier solutions would use Curses or call out so something like dialog.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Excellent! Thank you!
Re: updating something in the same position
by Fox (Pilgrim) on Aug 03, 2010 at 14:35 UTC
    I always use "\r\e[0J", which will return the cursor to the start of the line and erase the characters on it.

    but if you want more control try Term::ANSIScreen
Re: updating something in the same position
by Your Mother (Archbishop) on Aug 03, 2010 at 15:50 UTC

    I have been using Time::Progress for quite awhile in a few long (10s to a few hours) running tools to keep users calm about what's going on and I like it quite well. E.g.,

    use warnings; use strict; use Time::Progress; my $progress = Time::Progress->new(); $progress->attr( min => 0, max => 100_000 ); for my $count ( 0 .. 100_000 ) { print $progress->report( "%60b %p\r", $count ); } print "\nDone!\n";