Draw command output on one line

Is it possible to get the output of a command - for example tar- to write each line of output to only one line?

Usage example:

tar -options -f dest source | [insert trickery here]

and each processed file will be displayed on the output without moving the screen: each output will overwrite the last one. It can be done?


Edit: we have a working answer, but let's look at it further: How about the same, but more than 5 lines? You see a scroll that does not affect the rest of the terminal. I think I have an answer, but I would like to see what you guys think.

+5
source share
3 answers

Replace newlines with carriage returns.

 tar -options -f dest source | cut -b1-$(tput cols) | sed -u 'i\\o033[2K' | stdbuf -o0 tr '\n' '\r'; echo

Explanation:

  • cut -b1-$(tput cols): tar, , . , , , .

  • sed -u 'i\\o033[2K': . -u sed . stdbuf -oL sed 'i\\033[2K' .

  • stdbuf -o0 tr '\n' '\r': tr . Stdbuf , ; \n, .

  • echo: , .

, :

x=0; 
echo -e '\e[s'; 
tar -options -f dest source | while read line; do
      echo -en "\e[u" 
      if [ $x gt 0 ]; then echo -en "\e["$x"B"; fi;
      echo -en "\e[2K"
      echo -n $line | cut -b1-$(tput cols);
      let "x = ($x+1)%5";
done; echo;

. :

echo -e '\e[s'; tar -options -f dest source | while read line; do echo -en "\e[u\e2K"; echo -n $line | cut -b1-$(tput cols); done; echo

, VT100.

+6
tar -options -f dest source | cut -b1-$(tput cols) | perl -ne 's/^/\e[2K/; s/\n/\r/; print' ;echo

:

  • | cut -b1-$(tput cols) , .
  • (In perl -ne) s/^/\e[2K/ , "" . , , , .
  • (In perl -ne) s/\n/\r/ , tr. perl, .

PS : " ". . (1) , . (2) , , .

+4

Dave/tripleee ( ), , :

tar [opts] [args] | perl -e '$| = 1; while (<>) { s/\n/\r/; print; } print "\n"'

$| perl print, , , (), , bash . ( , , , .)

tr, , tr ( - ) stdout.

: , . , , , . ( , ) :

tar [opts] [args] | perl -e '$| = 1; $f = "%-" . `tput cols` . "s\r"; $f =~ s/\n//; while (<>) {s/\n//; printf $f, $_;} print "\n"'

(You can also get the terminal width in more than perl-y ways, as described here , but I did not want to depend on CPAN modules.

+3
source

All Articles