使命令输出显示在单行上 [英] Make output of command appear on single line

查看:57
本文介绍了使命令输出显示在单行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取命令的输出-例如 tar -将输出的每一行仅写入一行?



示例用法:

  tar -options -f dest source | [在此处插入诡计] 

,输出将显示正在处理的每个文件,而无需移动屏幕:每个输出覆盖最后一个。可以做到吗?






编辑:我们似乎有一个可行的答案,但请进一步讲:
如何做,但超过5行呢?您会看到滚动输出不会影响终端的其余部分。我想我已经找到了答案,但是我想看看你们的想法。

解决方案

将换行符替换为

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

说明:




  • cut -b1-$(tput cols):如果tar长于终端宽度,则截断tar的输出。


  • sed -u'i\\ 033o033 [2K':在每行的开头插入一行空白。 sed的 -u 选项将其置于非缓冲模式。 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源|边读边;做
echo -en \e [u
if [$ x gt 0];然后回显-en \e [ $ x B; fi;
echo -en \e [2K
echo -n $ line |削减-b1-$(tput cols);
让 x =($ x + 1)%5;
完成;回声;

可以将所有内容随意粘贴到一行上。实际上,这为原始问题提供了另一种解决方案:

  echo -e’\e [s]; tar -options -f dest源|边读边;做echo -en \e [u\e2K; echo -n $ line |削减-b1-$(tput cols);完成echo 

除了VT100代码外,它完全不依赖任何内容。


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

Example usage:

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

and the output would show every file being processed without making the screen move: each output overwrites the last one. Can it be done?


Edit: we seem to have a working answer, but lets take it further: How about doing the same, but over 5 lines? You see a scrolling output that doesn't affect the rest of the terminal. I think I've got an answer, but I'd like to see what you guys think.

解决方案

Replace the 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): Truncates the output of tar if it is longer than the terminal is wide. Depending on how little you want your terminal to move, it isn't strictly necessary.

  • sed -u 'i\\o033[2K': Inserts a line blank at the beginning of each line. The -u option to sed puts it in unbuffered mode. stdbuf -oL sed 'i\\033[2K' would work equally as well.

  • stdbuf -o0 tr '\n' '\r': Uses tr to exchange newlines with carriage returns. Stdbuf makes sure that the output is unbuffered; without the \n's, on a line buffered terminal, we'd see no output.

  • echo: Outputs a final newline, so that the terminal prompt doesn't eat up the final line.

For the problem your edit proposes:

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;

Feel free to smush all that onto one line. This actually yields an alternative solution for the original problem:

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

which neatly relies on nothing except VT100 codes.

这篇关于使命令输出显示在单行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆