Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: print back to beginning of line

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 ?

like image 968
Pmarcoen Avatar asked May 05 '10 15:05

Pmarcoen


People also ask

What is $@ in Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

How do I print a statement in Perl?

print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) are used as a delimiter to this operator.

How do I add a line in Perl script?

you can use the backslash to get to a newline if you close the quote before the backslash and reopen on the new line: eg, perl -e '$x;'\[ENTER]'$y' ... that works because bash ignores the newline and concatenates the two quotes into one string.


1 Answers

Use "\r" or "\015" octal (aka "Return caret" aka "Carriage Return" character originating from typewriter days :)

> perl5.8 -e 'print "11111\r222\r3\n";'
32211
> perl5.8 -e 'print "11111\015222\0153\n";'  
32211

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:

> perl5.8 -e 'print "11111\013222\0133\n";'    
11111
     222
        3
like image 193
DVK Avatar answered Oct 24 '22 22:10

DVK